Files
sourcekit-lsp/Sources/DocumentationLanguageService/DocumentationLanguageService.swift
Alex Hoppen 4040a5e4ac Add default implementations to LanguageService for methods that are not expected to be implemented by all language services
With the introduction of secondary language services, we don’t expect every language service to implement every request anymore. To simplify the addition of language services like `DocumentationLanguageService` add default implementations for methods that satisfy the following criteria:
 - `SourceKitLSPServer` does not expect side effects to happen when they are called
 - The method can throw or there is a reasonable default value
 - It is reasonable to expect that not all language services need to implement it
2025-08-26 16:10:36 +02:00

100 lines
3.1 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 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 Foundation
package import IndexStoreDB
package import LanguageServerProtocol
package import SKOptions
package import SourceKitLSP
import SwiftExtensions
package import SwiftSyntax
package import ToolchainRegistry
package actor DocumentationLanguageService: LanguageService, Sendable {
/// The ``SourceKitLSPServer`` instance that created this `DocumentationLanguageService`.
private(set) weak var sourceKitLSPServer: SourceKitLSPServer?
let documentationManager: DocCDocumentationManager
var documentManager: DocumentManager {
get throws {
guard let sourceKitLSPServer else {
throw ResponseError.unknown("Connection to the editor closed")
}
return sourceKitLSPServer.documentManager
}
}
package static var experimentalCapabilities: [String: LSPAny] {
return [
DoccDocumentationRequest.method: .dictionary(["version": .int(1)])
]
}
package init(
sourceKitLSPServer: SourceKitLSPServer,
toolchain: Toolchain,
options: SourceKitLSPOptions,
hooks: Hooks,
workspace: Workspace
) async throws {
self.sourceKitLSPServer = sourceKitLSPServer
self.documentationManager = DocCDocumentationManager(buildServerManager: workspace.buildServerManager)
}
package nonisolated func canHandle(workspace: Workspace, toolchain: Toolchain) -> Bool {
return true
}
package func initialize(
_ initialize: InitializeRequest
) async throws -> InitializeResult {
return InitializeResult(
capabilities: ServerCapabilities()
)
}
package func shutdown() async {
// Nothing to tear down
}
package func addStateChangeHandler(
handler: @escaping @Sendable (LanguageServerState, LanguageServerState) -> Void
) async {
// There is no underlying language server with which to report state
}
package func openDocument(
_ notification: DidOpenTextDocumentNotification,
snapshot: DocumentSnapshot
) async {
// The DocumentationLanguageService does not do anything with document events
}
package func closeDocument(_ notification: DidCloseTextDocumentNotification) async {
// The DocumentationLanguageService does not do anything with document events
}
package func reopenDocument(_ notification: ReopenTextDocumentNotification) async {
// The DocumentationLanguageService does not do anything with document events
}
package func changeDocument(
_ notification: DidChangeTextDocumentNotification,
preEditSnapshot: DocumentSnapshot,
postEditSnapshot: DocumentSnapshot,
edits: [SwiftSyntax.SourceEdit]
) async {
// The DocumentationLanguageService does not do anything with document events
}
}