Skip to content

Commit 40d3ac2

Browse files
committed
Add SuggestionServiceEventHandler
1 parent 3587ef4 commit 40d3ac2

File tree

6 files changed

+33
-12
lines changed

6 files changed

+33
-12
lines changed

Core/Sources/SuggestionService/SuggestionService.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import ProExtension
1717
public protocol SuggestionServiceType: SuggestionServiceProvider {}
1818

1919
public actor SuggestionService: SuggestionServiceType {
20-
typealias Middleware = SuggestionServiceMiddleware
21-
typealias EventHandler = SuggestionServiceEventHandler
20+
public typealias Middleware = SuggestionServiceMiddleware
21+
public typealias EventHandler = SuggestionServiceEventHandler
2222
public var configuration: SuggestionProvider.SuggestionServiceConfiguration {
2323
get async { await suggestionProvider.configuration }
2424
}

Tool/Sources/SuggestionBasic/CodeSuggestion.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ public struct CodeSuggestion: Codable, Equatable {
66
id: String,
77
text: String,
88
position: CursorPosition,
9-
range: CursorRange
9+
range: CursorRange,
10+
middlewareComments: [String] = [],
11+
metadata: [String: String] = [:]
1012
) {
1113
self.text = text
1214
self.position = position
1315
self.id = id
1416
self.range = range
15-
middlewareComments = []
17+
self.middlewareComments = middlewareComments
18+
self.metadata = metadata
1619
}
1720

1821
public static func == (lhs: CodeSuggestion, rhs: CodeSuggestion) -> Bool {
@@ -32,5 +35,7 @@ public struct CodeSuggestion: Codable, Equatable {
3235
public var range: CursorRange
3336
/// A place to store comments inserted by middleware for debugging use.
3437
@FallbackDecoding<EmptyArray> public var middlewareComments: [String]
38+
/// A place to store extra data.
39+
@FallbackDecoding<EmptyDictionary> public var metadata: [String: String]
3540
}
3641

Tool/Sources/SuggestionProvider/PostProcessingSuggestionServiceMiddleware.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ public struct PostProcessingSuggestionServiceMiddleware: SuggestionServiceMiddle
2121
}
2222

2323
static func removeTrailingWhitespacesAndNewlines(_ suggestion: inout CodeSuggestion) {
24-
var text = suggestion.text[...]
25-
while let last = text.last, last.isNewline || last.isWhitespace {
26-
text = text.dropLast(1)
27-
}
28-
suggestion.text = String(text)
24+
suggestion.text = suggestion.text.removedTrailingWhitespacesAndNewlines()
2925
}
3026

3127
static func checkIfSuggestionHasNoEffect(
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Foundation
2+
3+
public extension String {
4+
func removedTrailingWhitespacesAndNewlines() -> String {
5+
var text = self[...]
6+
while let last = text.last, last.isNewline || last.isWhitespace {
7+
text = text.dropLast(1)
8+
}
9+
return String(text)
10+
}
11+
}

Tool/Sources/SuggestionProvider/SuggestionServiceEventHandler.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import CopilotForXcodeKit
12
import Foundation
23
import SuggestionBasic
3-
import CopilotForXcodeKit
44

55
public protocol SuggestionServiceEventHandler {
6-
func didAccept(_ suggestion: CodeSuggestion, workspaceInfo: WorkspaceInfo)
7-
func didReject(_ suggestion: CodeSuggestion, workspaceInfo: WorkspaceInfo)
6+
func didAccept(_ suggestion: SuggestionBasic.CodeSuggestion, workspaceInfo: WorkspaceInfo)
7+
func didReject(_ suggestions: [SuggestionBasic.CodeSuggestion], workspaceInfo: WorkspaceInfo)
88
}
99

1010
public enum SuggestionServiceEventHandlerContainer {
@@ -19,4 +19,9 @@ public enum SuggestionServiceEventHandlerContainer {
1919
public static func addHandler(_ handler: SuggestionServiceEventHandler) {
2020
customHandlers.append(handler)
2121
}
22+
23+
public static func addHandlers(_ handlers: [SuggestionServiceEventHandler]) {
24+
customHandlers.append(contentsOf: handlers)
25+
}
2226
}
27+

Tool/Sources/SuggestionProvider/SuggestionServiceMiddleware.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public enum SuggestionServiceMiddlewareContainer {
2727
public static func addMiddleware(_ middleware: SuggestionServiceMiddleware) {
2828
customMiddlewares.append(middleware)
2929
}
30+
31+
public static func addMiddlewares(_ middlewares: [SuggestionServiceMiddleware]) {
32+
customMiddlewares.append(contentsOf: middlewares)
33+
}
3034
}
3135

3236
public struct DisabledLanguageSuggestionServiceMiddleware: SuggestionServiceMiddleware {

0 commit comments

Comments
 (0)