1- import AppKit
21import Foundation
32import Preferences
43import SuggestionModel
4+ import SuggestionProvider
55import UserDefaultsObserver
66
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 (
17- fileURL: URL ,
18- content: String ,
19- cursorPosition: CursorPosition ,
20- tabSize: Int ,
21- indentSize: Int ,
22- usesTabsForIndentation: Bool ,
23- ignoreSpaceOnlySuggestions: Bool
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 ]
37-
38- func notifyAccepted( _ suggestion: CodeSuggestion ) async
39- func notifyRejected( _ suggestions: [ CodeSuggestion ] ) async
40- func notifyOpenTextDocument( fileURL: URL , content: String ) async throws
41- func notifyChangeTextDocument( fileURL: URL , content: String ) async throws
42- func notifyCloseTextDocument( fileURL: URL ) async throws
43- func notifySaveTextDocument( fileURL: URL ) async throws
44- func cancelRequest( ) async
45- func terminate( ) async
46- }
47-
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-
70- protocol SuggestionServiceProvider : SuggestionServiceType { }
7+ public protocol SuggestionServiceType : SuggestionServiceProvider { }
718
729public 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)
10+ var middlewares : [ SuggestionServiceMiddleware ] {
11+ SuggestionServiceMiddlewareContainer . middlewares
8512 }
8613
8714 let projectRootURL : URL
88- let onServiceLaunched : ( SuggestionServiceType ) -> Void
15+ let onServiceLaunched : ( SuggestionServiceProvider ) -> Void
8916 let providerChangeObserver = UserDefaultsObserver (
9017 object: UserDefaults . shared,
9118 forKeyPaths: [ UserDefaultPreferenceKeys ( ) . suggestionFeatureProvider. key] ,
@@ -98,7 +25,10 @@ public actor SuggestionService: SuggestionServiceType {
9825 UserDefaults . shared. value ( for: \. suggestionFeatureProvider)
9926 }
10027
101- public init ( projectRootURL: URL , onServiceLaunched: @escaping ( SuggestionServiceType ) -> Void ) {
28+ public init (
29+ projectRootURL: URL ,
30+ onServiceLaunched: @escaping ( SuggestionServiceProvider ) -> Void
31+ ) {
10232 self . projectRootURL = projectRootURL
10333 self . onServiceLaunched = onServiceLaunched
10434
@@ -131,12 +61,12 @@ public actor SuggestionService: SuggestionServiceType {
13161}
13262
13363public extension SuggestionService {
134- func getSuggestions(
64+ func getSuggestions(
13565 _ request: SuggestionRequest
13666 ) async throws -> [ SuggestionModel . CodeSuggestion ] {
13767 var getSuggestion = suggestionProvider. getSuggestions
138-
139- for middleware in Self . middlewares. reversed ( ) {
68+
69+ for middleware in middlewares. reversed ( ) {
14070 getSuggestion = { [ getSuggestion] request in
14171 try await middleware. getSuggestion ( request, next: getSuggestion)
14272 }
0 commit comments