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