@@ -18,6 +18,7 @@ public protocol CodeiumSuggestionServiceType {
1818 func notifyOpenTextDocument( fileURL: URL , content: String ) async throws
1919 func notifyChangeTextDocument( fileURL: URL , content: String ) async throws
2020 func notifyCloseTextDocument( fileURL: URL ) async throws
21+ func cancelRequest( ) async
2122}
2223
2324enum CodeiumError : Error , LocalizedError {
@@ -43,6 +44,7 @@ public class CodeiumSuggestionService {
4344 var server : CodeiumLSP ?
4445 var heartbeatTask : Task < Void , Error > ?
4546 var requestCounter : UInt64 = 0
47+ var cancellationCounter : UInt64 = 0
4648 let openedDocumentPool = OpenedDocumentPool ( )
4749 let onServiceLaunched : ( ) -> Void
4850
@@ -118,6 +120,7 @@ public class CodeiumSuggestionService {
118120 self ? . server = nil
119121 self ? . heartbeatTask? . cancel ( )
120122 self ? . requestCounter = 0
123+ self ? . cancellationCounter = 0
121124 Logger . codeium. info ( " Language server is terminated, will be restarted when needed. " )
122125 }
123126
@@ -253,8 +256,16 @@ extension CodeiumSuggestionService: CodeiumSuggestionServiceType {
253256 )
254257 }
255258 ) )
259+
260+ if request. requestBody. metadata. request_id <= cancellationCounter {
261+ throw CancellationError ( )
262+ }
256263
257264 let result = try await ( try await setupServerIfNeeded ( ) ) . sendRequest ( request)
265+
266+ if request. requestBody. metadata. request_id <= cancellationCounter {
267+ throw CancellationError ( )
268+ }
258269
259270 return result. completionItems? . filter { item in
260271 if ignoreSpaceOnlySuggestions {
@@ -280,6 +291,10 @@ extension CodeiumSuggestionService: CodeiumSuggestionServiceType {
280291 )
281292 } ?? [ ]
282293 }
294+
295+ public func cancelRequest( ) async {
296+ cancellationCounter = requestCounter
297+ }
283298
284299 public func notifyAccepted( _ suggestion: CodeSuggestion ) async {
285300 _ = try ? await ( try setupServerIfNeeded ( ) )
0 commit comments