@@ -4,16 +4,36 @@ import Preferences
44import SuggestionModel
55import 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+
2870protocol SuggestionServiceProvider : SuggestionServiceType { }
2971
3072public 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
77133public 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 {
0 commit comments