Files
sourcekit-lsp/Sources/SourceKitLSP/TestHooks.swift
Alex Hoppen 1e409c97e2 Implicitly cancel text document requests when the document is edited or closed
As a user makes an edit to a file, these requests are most likely no longer relevant. It also makes sure that a long-running sourcekitd request can't block the entire language server if the client does not cancel all requests. For example, consider the following sequence of requests:
 - `textDocument/semanticTokens/full` for document A
 - `textDocument/didChange` for document A
 - `textDocument/formatting` for document A

If the editor is not cancelling the semantic tokens request on edit (like VS Code does), then the `didChange` notification is blocked on the semantic tokens request finishing. Hence, we also can't run the `textDocument/formatting` request. Cancelling the semantic tokens on the edit fixes the issue.

rdar://133987424
2024-08-15 15:32:26 -07:00

47 lines
1.6 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import BuildSystemIntegration
import Foundation
import LanguageServerProtocol
import SKSupport
import SemanticIndex
import struct TSCBasic.AbsolutePath
import struct TSCBasic.RelativePath
/// Closures can be used to inspect or modify internal behavior in SourceKit-LSP.
public struct TestHooks: Sendable {
package var indexTestHooks: IndexTestHooks
package var swiftpmTestHooks: SwiftPMTestHooks
/// A hook that will be executed before a request is handled.
///
/// This allows requests to be artificially delayed.
package var handleRequest: (@Sendable (any RequestType) async -> Void)?
public init() {
self.init(indexTestHooks: IndexTestHooks(), swiftpmTestHooks: SwiftPMTestHooks(), handleRequest: nil)
}
package init(
indexTestHooks: IndexTestHooks = IndexTestHooks(),
swiftpmTestHooks: SwiftPMTestHooks = SwiftPMTestHooks(),
handleRequest: (@Sendable (any RequestType) async -> Void)? = nil
) {
self.indexTestHooks = indexTestHooks
self.swiftpmTestHooks = swiftpmTestHooks
self.handleRequest = handleRequest
}
}