Skip to content

Commit daab571

Browse files
committed
Merge branch 'feature/suggestion-cheatsheet-2' into develop
2 parents 7fa80a3 + 5116c1d commit daab571

File tree

5 files changed

+134
-62
lines changed

5 files changed

+134
-62
lines changed

Pro

Submodule Pro updated from 72fd941 to 7796423

Tool/Sources/SuggestionService/CodeiumSuggestionProvider.swift

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,17 @@ actor CodeiumSuggestionProvider: SuggestionServiceProvider {
2828
}
2929

3030
extension CodeiumSuggestionProvider {
31-
func getSuggestions(
32-
fileURL: URL,
33-
content: String,
34-
cursorPosition: SuggestionModel.CursorPosition,
35-
tabSize: Int,
36-
indentSize: Int,
37-
usesTabsForIndentation: Bool,
38-
ignoreSpaceOnlySuggestions: Bool
39-
) async throws -> [SuggestionModel.CodeSuggestion] {
40-
try await (try createCodeiumServiceIfNeeded()).getCompletions(
41-
fileURL: fileURL,
42-
content: content,
43-
cursorPosition: cursorPosition,
44-
tabSize: tabSize,
45-
indentSize: indentSize,
46-
usesTabsForIndentation: usesTabsForIndentation,
47-
ignoreSpaceOnlySuggestions: ignoreSpaceOnlySuggestions
31+
func getSuggestions(_ request: SuggestionRequest) async throws
32+
-> [SuggestionModel.CodeSuggestion]
33+
{
34+
try await (createCodeiumServiceIfNeeded()).getCompletions(
35+
fileURL: request.fileURL,
36+
content: request.content,
37+
cursorPosition: request.cursorPosition,
38+
tabSize: request.tabSize,
39+
indentSize: request.indentSize,
40+
usesTabsForIndentation: request.usesTabsForIndentation,
41+
ignoreSpaceOnlySuggestions: request.ignoreSpaceOnlySuggestions
4842
)
4943
}
5044

@@ -70,12 +64,12 @@ extension CodeiumSuggestionProvider {
7064
}
7165

7266
func notifySaveTextDocument(fileURL: URL) async throws {}
73-
67+
7468
func cancelRequest() async {
7569
await (try? createCodeiumServiceIfNeeded())?
7670
.cancelRequest()
7771
}
78-
72+
7973
func terminate() async {
8074
(try? createCodeiumServiceIfNeeded())?.terminate()
8175
}

Tool/Sources/SuggestionService/GitHubCopilotSuggestionProvider.swift

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,17 @@ actor GitHubCopilotSuggestionProvider: SuggestionServiceProvider {
2626
}
2727

2828
extension GitHubCopilotSuggestionProvider {
29-
func getSuggestions(
30-
fileURL: URL,
31-
content: String,
32-
cursorPosition: SuggestionModel.CursorPosition,
33-
tabSize: Int,
34-
indentSize: Int,
35-
usesTabsForIndentation: Bool,
36-
ignoreSpaceOnlySuggestions: Bool
37-
) async throws -> [SuggestionModel.CodeSuggestion] {
38-
try await (try createGitHubCopilotServiceIfNeeded()).getCompletions(
39-
fileURL: fileURL,
40-
content: content,
41-
cursorPosition: cursorPosition,
42-
tabSize: tabSize,
43-
indentSize: indentSize,
44-
usesTabsForIndentation: usesTabsForIndentation,
45-
ignoreSpaceOnlySuggestions: ignoreSpaceOnlySuggestions,
29+
func getSuggestions(_ request: SuggestionRequest) async throws
30+
-> [SuggestionModel.CodeSuggestion]
31+
{
32+
try await (createGitHubCopilotServiceIfNeeded()).getCompletions(
33+
fileURL: request.fileURL,
34+
content: request.content,
35+
cursorPosition: request.cursorPosition,
36+
tabSize: request.tabSize,
37+
indentSize: request.indentSize,
38+
usesTabsForIndentation: request.usesTabsForIndentation,
39+
ignoreSpaceOnlySuggestions: request.ignoreSpaceOnlySuggestions,
4640
ignoreTrailingNewLinesAndSpaces: UserDefaults.shared
4741
.value(for: \.gitHubCopilotIgnoreTrailingNewLines)
4842
)

Tool/Sources/SuggestionService/SuggestionService.swift

Lines changed: 68 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,36 @@ import Preferences
44
import SuggestionModel
55
import UserDefaultsObserver
66

7-
public protocol SuggestionServiceType {
8-
func getSuggestions(
7+
public struct SuggestionRequest {
8+
public var fileURL: URL
9+
public var content: String
10+
public var cursorPosition: CursorPosition
11+
public var tabSize: Int
12+
public var indentSize: Int
13+
public var usesTabsForIndentation: Bool
14+
public var ignoreSpaceOnlySuggestions: Bool
15+
16+
public init(
917
fileURL: URL,
1018
content: String,
1119
cursorPosition: CursorPosition,
1220
tabSize: Int,
1321
indentSize: Int,
1422
usesTabsForIndentation: Bool,
1523
ignoreSpaceOnlySuggestions: Bool
16-
) async throws -> [CodeSuggestion]
24+
) {
25+
self.fileURL = fileURL
26+
self.content = content
27+
self.cursorPosition = cursorPosition
28+
self.tabSize = tabSize
29+
self.indentSize = indentSize
30+
self.usesTabsForIndentation = usesTabsForIndentation
31+
self.ignoreSpaceOnlySuggestions = ignoreSpaceOnlySuggestions
32+
}
33+
}
34+
35+
public protocol SuggestionServiceType {
36+
func getSuggestions(_ request: SuggestionRequest) async throws -> [CodeSuggestion]
1737

1838
func notifyAccepted(_ suggestion: CodeSuggestion) async
1939
func notifyRejected(_ suggestions: [CodeSuggestion]) async
@@ -25,9 +45,45 @@ public protocol SuggestionServiceType {
2545
func terminate() async
2646
}
2747

48+
public extension SuggestionServiceType {
49+
func getSuggestions(
50+
fileURL: URL,
51+
content: String,
52+
cursorPosition: CursorPosition,
53+
tabSize: Int,
54+
indentSize: Int,
55+
usesTabsForIndentation: Bool,
56+
ignoreSpaceOnlySuggestions: Bool
57+
) async throws -> [CodeSuggestion] {
58+
return try await getSuggestions(.init(
59+
fileURL: fileURL,
60+
content: content,
61+
cursorPosition: cursorPosition,
62+
tabSize: tabSize,
63+
indentSize: indentSize,
64+
usesTabsForIndentation: usesTabsForIndentation,
65+
ignoreSpaceOnlySuggestions: ignoreSpaceOnlySuggestions
66+
))
67+
}
68+
}
69+
2870
protocol SuggestionServiceProvider: SuggestionServiceType {}
2971

3072
public actor SuggestionService: SuggestionServiceType {
73+
static var builtInMiddlewares: [SuggestionServiceMiddleware] = [
74+
DisabledLanguageSuggestionServiceMiddleware(),
75+
]
76+
77+
static var customMiddlewares: [SuggestionServiceMiddleware] = []
78+
79+
static var middlewares: [SuggestionServiceMiddleware] {
80+
builtInMiddlewares + customMiddlewares
81+
}
82+
83+
public static func addMiddleware(_ middleware: SuggestionServiceMiddleware) {
84+
customMiddlewares.append(middleware)
85+
}
86+
3187
let projectRootURL: URL
3288
let onServiceLaunched: (SuggestionServiceType) -> Void
3389
let providerChangeObserver = UserDefaultsObserver(
@@ -75,31 +131,18 @@ public actor SuggestionService: SuggestionServiceType {
75131
}
76132

77133
public extension SuggestionService {
78-
func getSuggestions(
79-
fileURL: URL,
80-
content: String,
81-
cursorPosition: SuggestionModel.CursorPosition,
82-
tabSize: Int,
83-
indentSize: Int,
84-
usesTabsForIndentation: Bool,
85-
ignoreSpaceOnlySuggestions: Bool
134+
func getSuggestions(
135+
_ request: SuggestionRequest
86136
) async throws -> [SuggestionModel.CodeSuggestion] {
87-
let language = languageIdentifierFromFileURL(fileURL)
88-
if UserDefaults.shared.value(for: \.suggestionFeatureDisabledLanguageList)
89-
.contains(where: { $0 == language.rawValue })
90-
{
91-
return []
137+
var getSuggestion = suggestionProvider.getSuggestions
138+
139+
for middleware in Self.middlewares.reversed() {
140+
getSuggestion = { [getSuggestion] request in
141+
try await middleware.getSuggestion(request, next: getSuggestion)
142+
}
92143
}
93144

94-
return try await suggestionProvider.getSuggestions(
95-
fileURL: fileURL,
96-
content: content,
97-
cursorPosition: cursorPosition,
98-
tabSize: tabSize,
99-
indentSize: indentSize,
100-
usesTabsForIndentation: usesTabsForIndentation,
101-
ignoreSpaceOnlySuggestions: ignoreSpaceOnlySuggestions
102-
)
145+
return try await getSuggestion(request)
103146
}
104147

105148
func notifyAccepted(_ suggestion: SuggestionModel.CodeSuggestion) async {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import Foundation
2+
import SuggestionModel
3+
import Logger
4+
5+
public protocol SuggestionServiceMiddleware {
6+
typealias Next = (SuggestionRequest) async throws -> [CodeSuggestion]
7+
8+
func getSuggestion(_ request: SuggestionRequest, next: Next) async throws -> [CodeSuggestion]
9+
}
10+
11+
struct DisabledLanguageSuggestionServiceMiddleware: SuggestionServiceMiddleware {
12+
func getSuggestion(_ request: SuggestionRequest, next: Next) async throws -> [CodeSuggestion] {
13+
let language = languageIdentifierFromFileURL(request.fileURL)
14+
if UserDefaults.shared.value(for: \.suggestionFeatureDisabledLanguageList)
15+
.contains(where: { $0 == language.rawValue })
16+
{
17+
#if DEBUG
18+
Logger.service.info("Suggestion service is disabled for \(language).")
19+
#endif
20+
return []
21+
}
22+
23+
return try await next(request)
24+
}
25+
}
26+
27+
public struct DebugSuggestionServiceMiddleware: SuggestionServiceMiddleware {
28+
public init() {}
29+
30+
public func getSuggestion(_ request: SuggestionRequest, next: Next) async throws -> [CodeSuggestion] {
31+
Logger.service.debug("""
32+
Get suggestion for \(request.fileURL) at \(request.cursorPosition)
33+
""")
34+
let suggestions = try await next(request)
35+
Logger.service.debug("""
36+
Receive \(suggestions.count) suggestions for \(request.fileURL) at \(request.cursorPosition)
37+
""")
38+
39+
return suggestions
40+
}
41+
}

0 commit comments

Comments
 (0)