@@ -19,83 +19,92 @@ public protocol CopilotSuggestionServiceType {
1919 cursorPosition: CursorPosition ,
2020 tabSize: Int ,
2121 indentSize: Int ,
22- usesTabsForIndentation: Bool
22+ usesTabsForIndentation: Bool ,
23+ ignoreSpaceOnlySuggestions: Bool
2324 ) async throws -> [ CopilotCompletion ]
2425 func notifyAccepted( _ completion: CopilotCompletion ) async
2526 func notifyRejected( _ completions: [ CopilotCompletion ] ) async
2627}
2728
29+ protocol CopilotLSP {
30+ func sendRequest< E: CopilotRequestType > ( _ endpoint: E ) async throws -> E . Response
31+ }
32+
2833public class CopilotBaseService {
2934 let projectRootURL : URL
35+ var server : CopilotLSP
36+
37+ init ( designatedServer: CopilotLSP ) {
38+ projectRootURL = URL ( fileURLWithPath: " / " )
39+ server = designatedServer
40+ }
3041
3142 init ( projectRootURL: URL ) {
3243 self . projectRootURL = projectRootURL
33- }
44+ server = {
45+ let supportURL = FileManager . default. urls (
46+ for: . applicationSupportDirectory,
47+ in: . userDomainMask
48+ ) . first!. appendingPathComponent ( " com.intii.CopilotForXcode " )
49+ if !FileManager. default. fileExists ( atPath: supportURL. path) {
50+ try ? FileManager . default
51+ . createDirectory ( at: supportURL, withIntermediateDirectories: false )
52+ }
53+ let executionParams = {
54+ let nodePath = UserDefaults . shared. string ( forKey: SettingsKey . nodePath) ?? " "
55+ return Process . ExecutionParameters (
56+ path: " /usr/bin/env " ,
57+ arguments: [
58+ nodePath. isEmpty ? " node " : nodePath,
59+ Bundle . main. url (
60+ forResource: " agent " ,
61+ withExtension: " js " ,
62+ subdirectory: " copilot/dist "
63+ ) !. path,
64+ " --stdio " ,
65+ ] ,
66+ environment: [
67+ " PATH " : " /usr/bin:/usr/local/bin " ,
68+ ] ,
69+ currentDirectoryURL: supportURL
70+ )
71+ } ( )
72+ let localServer = LocalProcessServer ( executionParameters: executionParams)
73+ localServer. logMessages = false
74+ let server = InitializingServer ( server: localServer)
75+ server. notificationHandler = { _, respond in
76+ respond ( nil )
77+ }
3478
35- lazy var server : InitializingServer = {
36- let supportURL = FileManager . default. urls (
37- for: . applicationSupportDirectory,
38- in: . userDomainMask
39- ) . first!. appendingPathComponent ( " com.intii.CopilotForXcode " )
40- if !FileManager. default. fileExists ( atPath: supportURL. path) {
41- try ? FileManager . default
42- . createDirectory ( at: supportURL, withIntermediateDirectories: false )
43- }
44- let executionParams = {
45- let nodePath = UserDefaults . shared. string ( forKey: SettingsKey . nodePath) ?? " "
46- return Process . ExecutionParameters (
47- path: " /usr/bin/env " ,
48- arguments: [
49- nodePath. isEmpty ? " node " : nodePath,
50- Bundle . main. url (
51- forResource: " agent " ,
52- withExtension: " js " ,
53- subdirectory: " copilot/dist "
54- ) !. path,
55- " --stdio " ,
56- ] ,
57- environment: [
58- " PATH " : " /usr/bin:/usr/local/bin " ,
59- ] ,
60- currentDirectoryURL: supportURL
61- )
62- } ( )
63- let localServer = LocalProcessServer ( executionParameters: executionParams)
64- localServer. logMessages = false
65- let server = InitializingServer ( server: localServer)
66- server. notificationHandler = { _, respond in
67- respond ( nil )
68- }
79+ server. initializeParamsProvider = {
80+ let capabilities = ClientCapabilities (
81+ workspace: nil ,
82+ textDocument: nil ,
83+ window: nil ,
84+ general: nil ,
85+ experimental: nil
86+ )
6987
70- let projectRoot = self . projectRootURL
71- server. initializeParamsProvider = {
72- let capabilities = ClientCapabilities (
73- workspace: nil ,
74- textDocument: nil ,
75- window: nil ,
76- general: nil ,
77- experimental: nil
78- )
79-
80- return InitializeParams (
81- processId: Int ( ProcessInfo . processInfo. processIdentifier) ,
82- clientInfo: . init( name: " Copilot for Xcode " ) ,
83- locale: nil ,
84- rootPath: projectRoot. path,
85- rootUri: projectRoot. path,
86- initializationOptions: nil ,
87- capabilities: capabilities,
88- trace: nil ,
89- workspaceFolders: nil
90- )
91- }
88+ return InitializeParams (
89+ processId: Int ( ProcessInfo . processInfo. processIdentifier) ,
90+ clientInfo: . init( name: " Copilot for Xcode " ) ,
91+ locale: nil ,
92+ rootPath: projectRootURL. path,
93+ rootUri: projectRootURL. path,
94+ initializationOptions: nil ,
95+ capabilities: capabilities,
96+ trace: nil ,
97+ workspaceFolders: nil
98+ )
99+ }
92100
93- server. notificationHandler = { _, respond in
94- respond ( nil )
95- }
101+ server. notificationHandler = { _, respond in
102+ respond ( nil )
103+ }
96104
97- return server
98- } ( )
105+ return server
106+ } ( )
107+ }
99108}
100109
101110public final class CopilotAuthService : CopilotBaseService , CopilotAuthServiceType {
@@ -136,14 +145,19 @@ public final class CopilotSuggestionService: CopilotBaseService, CopilotSuggesti
136145 override public init ( projectRootURL: URL = URL ( fileURLWithPath: " / " ) ) {
137146 super. init ( projectRootURL: projectRootURL)
138147 }
148+
149+ override init ( designatedServer: CopilotLSP ) {
150+ super. init ( designatedServer: designatedServer)
151+ }
139152
140153 public func getCompletions(
141154 fileURL: URL ,
142155 content: String ,
143156 cursorPosition: CursorPosition ,
144157 tabSize: Int ,
145158 indentSize: Int ,
146- usesTabsForIndentation: Bool
159+ usesTabsForIndentation: Bool ,
160+ ignoreSpaceOnlySuggestions: Bool
147161 ) async throws -> [ CopilotCompletion ] {
148162 guard let languageId = languageIdentifierFromFileURL ( fileURL) else { return [ ] }
149163
@@ -173,7 +187,14 @@ public final class CopilotSuggestionService: CopilotBaseService, CopilotSuggesti
173187 relativePath: relativePath,
174188 languageId: languageId,
175189 position: cursorPosition
176- ) ) ) . completions
190+ ) ) )
191+ . completions
192+ . filter { completion in
193+ if ignoreSpaceOnlySuggestions {
194+ return !completion. text. allSatisfy { $0. isWhitespace || $0. isNewline }
195+ }
196+ return true
197+ }
177198
178199 return completions
179200 }
@@ -191,7 +212,7 @@ public final class CopilotSuggestionService: CopilotBaseService, CopilotSuggesti
191212 }
192213}
193214
194- extension InitializingServer {
215+ extension InitializingServer : CopilotLSP {
195216 func sendRequest< E: CopilotRequestType > ( _ endpoint: E ) async throws -> E . Response {
196217 try await sendRequest ( endpoint. request)
197218 }
0 commit comments