@@ -144,8 +144,9 @@ package actor SourceKitLSPServer {
144
144
}
145
145
}
146
146
147
- /// For all currently handled text document requests a mapping from the document to the corresponding request ID.
148
- private var inProgressTextDocumentRequests : [ DocumentURI : Set < RequestID > ] = [ : ]
147
+ /// For all currently handled text document requests a mapping from the document to the corresponding request ID and
148
+ /// the method of the request (ie. the value of `TextDocumentRequest.method`).
149
+ private var inProgressTextDocumentRequests : [ DocumentURI : [ ( id: RequestID , requestMethod: String ) ] ] = [ : ]
149
150
150
151
var onExit : ( ) -> Void
151
152
@@ -544,18 +545,26 @@ package actor SourceKitLSPServer {
544
545
// MARK: - MessageHandler
545
546
546
547
extension SourceKitLSPServer : QueueBasedMessageHandler {
548
+ private enum ImplicitTextDocumentRequestCancellationReason {
549
+ case documentChanged
550
+ case documentClosed
551
+ }
552
+
547
553
package nonisolated func didReceive( notification: some NotificationType ) {
548
554
let textDocumentUri : DocumentURI
555
+ let cancellationReason : ImplicitTextDocumentRequestCancellationReason
549
556
switch notification {
550
557
case let params as DidChangeTextDocumentNotification :
551
558
textDocumentUri = params. textDocument. uri
559
+ cancellationReason = . documentChanged
552
560
case let params as DidCloseTextDocumentNotification :
553
561
textDocumentUri = params. textDocument. uri
562
+ cancellationReason = . documentClosed
554
563
default :
555
564
return
556
565
}
557
566
textDocumentTrackingQueue. async ( priority: . high) {
558
- await self . cancelTextDocumentRequests ( for: textDocumentUri)
567
+ await self . cancelTextDocumentRequests ( for: textDocumentUri, reason : cancellationReason )
559
568
}
560
569
}
561
570
@@ -576,11 +585,18 @@ extension SourceKitLSPServer: QueueBasedMessageHandler {
576
585
///
577
586
/// - Important: Should be invoked on `textDocumentTrackingQueue` to ensure that new text document requests are
578
587
/// registered before a notification that triggers cancellation might come in.
579
- private func cancelTextDocumentRequests( for uri: DocumentURI ) {
588
+ private func cancelTextDocumentRequests( for uri: DocumentURI , reason : ImplicitTextDocumentRequestCancellationReason ) {
580
589
guard self . options. cancelTextDocumentRequestsOnEditAndCloseOrDefault else {
581
590
return
582
591
}
583
- for requestID in self . inProgressTextDocumentRequests [ uri, default: [ ] ] {
592
+ for (requestID, requestMethod) in self . inProgressTextDocumentRequests [ uri, default: [ ] ] {
593
+ if reason == . documentChanged && requestMethod == CompletionRequest . method {
594
+ // As the user types, we filter the code completion results. Cancelling the completion request on every
595
+ // keystroke means that we will never build the initial list of completion results for this code
596
+ // completion session if building that list takes longer than the user's typing cadence (eg. for global
597
+ // completions) and we will thus not show any completions.
598
+ continue
599
+ }
584
600
logger. info ( " Implicitly cancelling request \( requestID) " )
585
601
self . messageHandlingHelper. cancelRequest ( id: requestID)
586
602
}
@@ -627,8 +643,8 @@ extension SourceKitLSPServer: QueueBasedMessageHandler {
627
643
628
644
/// - Important: Should be invoked on `textDocumentTrackingQueue` to ensure that new text document requests are
629
645
/// registered before a notification that triggers cancellation might come in.
630
- private func registerInProgressTextDocumentRequest( _ request: any TextDocumentRequest , id: RequestID ) {
631
- self . inProgressTextDocumentRequests [ request. textDocument. uri, default: [ ] ] . insert ( id )
646
+ private func registerInProgressTextDocumentRequest< T : TextDocumentRequest > ( _ request: T , id: RequestID ) {
647
+ self . inProgressTextDocumentRequests [ request. textDocument. uri, default: [ ] ] . append ( ( id : id , requestMethod : T . method ) )
632
648
}
633
649
634
650
package func handle< Request: RequestType > (
@@ -638,7 +654,7 @@ extension SourceKitLSPServer: QueueBasedMessageHandler {
638
654
) async {
639
655
defer {
640
656
if let request = params as? any TextDocumentRequest {
641
- self . inProgressTextDocumentRequests [ request. textDocument. uri, default: [ ] ] . remove ( id )
657
+ self . inProgressTextDocumentRequests [ request. textDocument. uri, default: [ ] ] . removeAll { $0 . id == id }
642
658
}
643
659
}
644
660
0 commit comments