@@ -52,6 +52,7 @@ enum GitHubCopilotError: Error, LocalizedError {
5252public class GitHubCopilotBaseService {
5353 let projectRootURL : URL
5454 var server : GitHubCopilotLSP
55+ var localProcessServer : CopilotLocalProcessServer ?
5556
5657 init ( designatedServer: GitHubCopilotLSP ) {
5758 projectRootURL = URL ( fileURLWithPath: " / " )
@@ -60,7 +61,7 @@ public class GitHubCopilotBaseService {
6061
6162 init ( projectRootURL: URL ) throws {
6263 self . projectRootURL = projectRootURL
63- server = try {
64+ let ( server, localServer ) = try {
6465 let urls = try GitHubCopilotBaseService . createFoldersIfNeeded ( )
6566 var userEnvPath = ProcessInfo . processInfo. userEnvironment [ " PATH " ] ?? " "
6667 if userEnvPath. isEmpty {
@@ -124,6 +125,7 @@ public class GitHubCopilotBaseService {
124125 } ( )
125126 }
126127 let localServer = CopilotLocalProcessServer ( executionParameters: executionParams)
128+
127129 localServer. logMessages = UserDefaults . shared. value ( for: \. gitHubCopilotVerboseLog)
128130 localServer. notificationHandler = { _, respond in
129131 respond ( . timeout)
@@ -156,8 +158,11 @@ public class GitHubCopilotBaseService {
156158 )
157159 }
158160
159- return server
161+ return ( server, localServer )
160162 } ( )
163+
164+ self . server = server
165+ self . localProcessServer = localServer
161166 }
162167
163168 public static func createFoldersIfNeeded( ) throws -> (
@@ -238,6 +243,8 @@ public final class GitHubCopilotAuthService: GitHubCopilotBaseService,
238243public final class GitHubCopilotSuggestionService : GitHubCopilotBaseService ,
239244 GitHubCopilotSuggestionServiceType
240245{
246+ private var ongoingTasks = Set < Task < [ CodeSuggestion ] , Error > > ( )
247+
241248 override public init ( projectRootURL: URL = URL ( fileURLWithPath: " / " ) ) throws {
242249 try super. init ( projectRootURL: projectRootURL)
243250 }
@@ -272,27 +279,37 @@ public final class GitHubCopilotSuggestionService: GitHubCopilotBaseService,
272279 return filePath
273280 } ( )
274281
275- let completions = try await server
276- . sendRequest ( GitHubCopilotRequest . GetCompletionsCycling ( doc: . init(
277- source: content,
278- tabSize: tabSize,
279- indentSize: indentSize,
280- insertSpaces: !usesTabsForIndentation,
281- path: fileURL. path,
282- uri: fileURL. path,
283- relativePath: relativePath,
284- languageId: languageId,
285- position: cursorPosition
286- ) ) )
287- . completions
288- . filter { completion in
289- if ignoreSpaceOnlySuggestions {
290- return !completion. text. allSatisfy { $0. isWhitespace || $0. isNewline }
282+ ongoingTasks. forEach { $0. cancel ( ) }
283+ ongoingTasks. removeAll ( )
284+ await localProcessServer? . cancelOngoingTasks ( )
285+
286+ let task = Task {
287+ let completions = try await server
288+ . sendRequest ( GitHubCopilotRequest . GetCompletionsCycling ( doc: . init(
289+ source: content,
290+ tabSize: tabSize,
291+ indentSize: indentSize,
292+ insertSpaces: !usesTabsForIndentation,
293+ path: fileURL. path,
294+ uri: fileURL. path,
295+ relativePath: relativePath,
296+ languageId: languageId,
297+ position: cursorPosition
298+ ) ) )
299+ . completions
300+ . filter { completion in
301+ if ignoreSpaceOnlySuggestions {
302+ return !completion. text. allSatisfy { $0. isWhitespace || $0. isNewline }
303+ }
304+ return true
291305 }
292- return true
293- }
306+ try Task . checkCancellation ( )
307+ return completions
308+ }
309+
310+ ongoingTasks. insert ( task)
294311
295- return completions
312+ return try await task . value
296313 }
297314
298315 public func notifyAccepted( _ completion: CodeSuggestion ) async {
0 commit comments