Skip to content

Commit c692a83

Browse files
committed
Construct LanguageServiceRegistry outside of SourceKitLSP module
1 parent 4f792e8 commit c692a83

10 files changed

+68
-55
lines changed

Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ var targets: [Target] = [
3939
dependencies: [
4040
"BuildServerIntegration",
4141
"Diagnose",
42+
"InProcessClient",
4243
"LanguageServerProtocol",
4344
"LanguageServerProtocolExtensions",
4445
"LanguageServerProtocolJSONRPC",

Sources/InProcessClient/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
add_library(InProcessClient STATIC
2-
InProcessSourceKitLSPClient.swift)
2+
InProcessSourceKitLSPClient.swift
3+
LanguageServiceRegistry+staticallyKnownServices.swift)
34

45
set_target_properties(InProcessClient PROPERTIES
56
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})

Sources/InProcessClient/InProcessSourceKitLSPClient.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public final class InProcessSourceKitLSPClient: Sendable {
6060
self.server = SourceKitLSPServer(
6161
client: serverToClientConnection,
6262
toolchainRegistry: toolchainRegistry,
63-
languageServerRegistry: LanguageServiceRegistry(),
63+
languageServerRegistry: .staticallyKnownServices,
6464
options: options,
6565
hooks: hooks,
6666
onExit: {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import LanguageServerProtocol
2+
package import SourceKitLSP
3+
4+
extension LanguageServiceRegistry {
5+
/// All types conforming to `LanguageService` that are known at compile time.
6+
package static let staticallyKnownServices = {
7+
var registry = LanguageServiceRegistry()
8+
registry.register(ClangLanguageService.self, for: [.c, .cpp, .objective_c, .objective_cpp])
9+
registry.register(SwiftLanguageService.self, for: [.swift])
10+
registry.register(DocumentationLanguageService.self, for: [.markdown, .tutorial])
11+
return registry
12+
}()
13+
}

Sources/SKTestSupport/TestSourceKitLSPClient.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ package final class TestSourceKitLSPClient: MessageHandler, Sendable {
164164
server = SourceKitLSPServer(
165165
client: serverToClientConnection,
166166
toolchainRegistry: toolchainRegistry,
167-
languageServerRegistry: LanguageServiceRegistry(),
167+
languageServerRegistry: .staticallyKnownServices,
168168
options: options,
169169
hooks: hooks,
170170
onExit: {

Sources/SourceKitLSP/Clang/ClangLanguageService.swift

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212

1313
import BuildServerIntegration
1414
import Foundation
15-
import LanguageServerProtocol
15+
package import LanguageServerProtocol
1616
import LanguageServerProtocolExtensions
1717
import LanguageServerProtocolJSONRPC
1818
import SKLogging
19-
import SKOptions
20-
import SwiftExtensions
21-
import SwiftSyntax
19+
package import SKOptions
20+
package import SwiftExtensions
21+
package import SwiftSyntax
2222
import TSCExtensions
23-
import ToolchainRegistry
23+
package import ToolchainRegistry
2424

2525
#if canImport(DocCDocumentation)
2626
import DocCDocumentation
@@ -38,7 +38,7 @@ import WinSDK
3838
/// ``ClangLanguageServerShim`` conforms to ``MessageHandler`` to receive
3939
/// requests and notifications **from** clangd, not from the editor, and it will
4040
/// forward these requests and notifications to the editor.
41-
actor ClangLanguageService: LanguageService, MessageHandler {
41+
package actor ClangLanguageService: LanguageService, MessageHandler {
4242
/// The queue on which all messages that originate from clangd are handled.
4343
///
4444
/// These are requests and notifications sent *from* clangd, not replies from
@@ -144,12 +144,12 @@ actor ClangLanguageService: LanguageService, MessageHandler {
144144
return ClangBuildSettings(settings, clangPath: clangPath)
145145
}
146146

147-
nonisolated func canHandle(workspace: Workspace, toolchain: Toolchain) -> Bool {
147+
package nonisolated func canHandle(workspace: Workspace, toolchain: Toolchain) -> Bool {
148148
// We launch different clangd instance for each workspace because clangd doesn't have multi-root workspace support.
149149
return workspace === self.workspace.value && self.clangdPath == toolchain.clangd
150150
}
151151

152-
func addStateChangeHandler(handler: @escaping (LanguageServerState, LanguageServerState) -> Void) {
152+
package func addStateChangeHandler(handler: @escaping (LanguageServerState, LanguageServerState) -> Void) {
153153
self.stateChangeHandlers.append(handler)
154154
}
155155

@@ -244,7 +244,7 @@ actor ClangLanguageService: LanguageService, MessageHandler {
244244
/// sending a notification that's intended for the editor.
245245
///
246246
/// We should either handle it ourselves or forward it to the editor.
247-
nonisolated func handle(_ params: some NotificationType) {
247+
package nonisolated func handle(_ params: some NotificationType) {
248248
logger.info(
249249
"""
250250
Received notification from clangd:
@@ -267,7 +267,7 @@ actor ClangLanguageService: LanguageService, MessageHandler {
267267
/// sending a notification that's intended for the editor.
268268
///
269269
/// We should either handle it ourselves or forward it to the client.
270-
nonisolated func handle<R: RequestType>(
270+
package nonisolated func handle<R: RequestType>(
271271
_ params: R,
272272
id: RequestID,
273273
reply: @Sendable @escaping (LSPResult<R.Response>) -> Void
@@ -316,7 +316,7 @@ actor ClangLanguageService: LanguageService, MessageHandler {
316316
return nil
317317
}
318318

319-
func crash() {
319+
package func crash() {
320320
clangdProcess?.terminateImmediately()
321321
}
322322
}
@@ -366,7 +366,7 @@ extension ClangLanguageService {
366366

367367
extension ClangLanguageService {
368368

369-
func initialize(_ initialize: InitializeRequest) async throws -> InitializeResult {
369+
package func initialize(_ initialize: InitializeRequest) async throws -> InitializeResult {
370370
// Store the initialize request so we can replay it in case clangd crashes
371371
self.initializeRequest = initialize
372372

@@ -417,7 +417,7 @@ extension ClangLanguageService {
417417
clangd.send(notification)
418418
}
419419

420-
func reopenDocument(_ notification: ReopenTextDocumentNotification) {}
420+
package func reopenDocument(_ notification: ReopenTextDocumentNotification) {}
421421

422422
package func changeDocument(
423423
_ notification: DidChangeTextDocumentNotification,
@@ -481,20 +481,20 @@ extension ClangLanguageService {
481481
return try await forwardRequestToClangd(req)
482482
}
483483

484-
func completion(_ req: CompletionRequest) async throws -> CompletionList {
484+
package func completion(_ req: CompletionRequest) async throws -> CompletionList {
485485
return try await forwardRequestToClangd(req)
486486
}
487487

488-
func completionItemResolve(_ req: CompletionItemResolveRequest) async throws -> CompletionItem {
488+
package func completionItemResolve(_ req: CompletionItemResolveRequest) async throws -> CompletionItem {
489489
return try await forwardRequestToClangd(req)
490490
}
491491

492-
func hover(_ req: HoverRequest) async throws -> HoverResponse? {
492+
package func hover(_ req: HoverRequest) async throws -> HoverResponse? {
493493
return try await forwardRequestToClangd(req)
494494
}
495495

496496
#if canImport(DocCDocumentation)
497-
func doccDocumentation(_ req: DoccDocumentationRequest) async throws -> DoccDocumentationResponse {
497+
package func doccDocumentation(_ req: DoccDocumentationRequest) async throws -> DoccDocumentationResponse {
498498
guard let sourceKitLSPServer else {
499499
throw ResponseError.unknown("Connection to the editor closed")
500500
}
@@ -504,26 +504,28 @@ extension ClangLanguageService {
504504
}
505505
#endif
506506

507-
func symbolInfo(_ req: SymbolInfoRequest) async throws -> [SymbolDetails] {
507+
package func symbolInfo(_ req: SymbolInfoRequest) async throws -> [SymbolDetails] {
508508
return try await forwardRequestToClangd(req)
509509
}
510510

511-
func documentSymbolHighlight(_ req: DocumentHighlightRequest) async throws -> [DocumentHighlight]? {
511+
package func documentSymbolHighlight(_ req: DocumentHighlightRequest) async throws -> [DocumentHighlight]? {
512512
return try await forwardRequestToClangd(req)
513513
}
514514

515-
func documentSymbol(_ req: DocumentSymbolRequest) async throws -> DocumentSymbolResponse? {
515+
package func documentSymbol(_ req: DocumentSymbolRequest) async throws -> DocumentSymbolResponse? {
516516
return try await forwardRequestToClangd(req)
517517
}
518518

519-
func documentColor(_ req: DocumentColorRequest) async throws -> [ColorInformation] {
519+
package func documentColor(_ req: DocumentColorRequest) async throws -> [ColorInformation] {
520520
guard self.capabilities?.colorProvider?.isSupported ?? false else {
521521
return []
522522
}
523523
return try await forwardRequestToClangd(req)
524524
}
525525

526-
func documentSemanticTokens(_ req: DocumentSemanticTokensRequest) async throws -> DocumentSemanticTokensResponse? {
526+
package func documentSemanticTokens(
527+
_ req: DocumentSemanticTokensRequest
528+
) async throws -> DocumentSemanticTokensResponse? {
527529
guard var response = try await forwardRequestToClangd(req) else {
528530
return nil
529531
}
@@ -533,7 +535,7 @@ extension ClangLanguageService {
533535
return response
534536
}
535537

536-
func documentSemanticTokensDelta(
538+
package func documentSemanticTokensDelta(
537539
_ req: DocumentSemanticTokensDeltaRequest
538540
) async throws -> DocumentSemanticTokensDeltaResponse? {
539541
guard var response = try await forwardRequestToClangd(req) else {
@@ -558,7 +560,7 @@ extension ClangLanguageService {
558560
return response
559561
}
560562

561-
func documentSemanticTokensRange(
563+
package func documentSemanticTokensRange(
562564
_ req: DocumentSemanticTokensRangeRequest
563565
) async throws -> DocumentSemanticTokensResponse? {
564566
guard var response = try await forwardRequestToClangd(req) else {
@@ -570,49 +572,49 @@ extension ClangLanguageService {
570572
return response
571573
}
572574

573-
func colorPresentation(_ req: ColorPresentationRequest) async throws -> [ColorPresentation] {
575+
package func colorPresentation(_ req: ColorPresentationRequest) async throws -> [ColorPresentation] {
574576
guard self.capabilities?.colorProvider?.isSupported ?? false else {
575577
return []
576578
}
577579
return try await forwardRequestToClangd(req)
578580
}
579581

580-
func documentFormatting(_ req: DocumentFormattingRequest) async throws -> [TextEdit]? {
582+
package func documentFormatting(_ req: DocumentFormattingRequest) async throws -> [TextEdit]? {
581583
return try await forwardRequestToClangd(req)
582584
}
583585

584-
func documentRangeFormatting(_ req: DocumentRangeFormattingRequest) async throws -> [TextEdit]? {
586+
package func documentRangeFormatting(_ req: DocumentRangeFormattingRequest) async throws -> [TextEdit]? {
585587
return try await forwardRequestToClangd(req)
586588
}
587589

588-
func documentOnTypeFormatting(_ req: DocumentOnTypeFormattingRequest) async throws -> [TextEdit]? {
590+
package func documentOnTypeFormatting(_ req: DocumentOnTypeFormattingRequest) async throws -> [TextEdit]? {
589591
return try await forwardRequestToClangd(req)
590592
}
591593

592-
func codeAction(_ req: CodeActionRequest) async throws -> CodeActionRequestResponse? {
594+
package func codeAction(_ req: CodeActionRequest) async throws -> CodeActionRequestResponse? {
593595
return try await forwardRequestToClangd(req)
594596
}
595597

596-
func inlayHint(_ req: InlayHintRequest) async throws -> [InlayHint] {
598+
package func inlayHint(_ req: InlayHintRequest) async throws -> [InlayHint] {
597599
return try await forwardRequestToClangd(req)
598600
}
599601

600-
func documentDiagnostic(_ req: DocumentDiagnosticsRequest) async throws -> DocumentDiagnosticReport {
602+
package func documentDiagnostic(_ req: DocumentDiagnosticsRequest) async throws -> DocumentDiagnosticReport {
601603
return try await forwardRequestToClangd(req)
602604
}
603605

604-
func codeLens(_ req: CodeLensRequest) async throws -> [CodeLens] {
606+
package func codeLens(_ req: CodeLensRequest) async throws -> [CodeLens] {
605607
return try await forwardRequestToClangd(req) ?? []
606608
}
607609

608-
func foldingRange(_ req: FoldingRangeRequest) async throws -> [FoldingRange]? {
610+
package func foldingRange(_ req: FoldingRangeRequest) async throws -> [FoldingRange]? {
609611
guard self.capabilities?.foldingRangeProvider?.isSupported ?? false else {
610612
return nil
611613
}
612614
return try await forwardRequestToClangd(req)
613615
}
614616

615-
func openGeneratedInterface(
617+
package func openGeneratedInterface(
616618
document: DocumentURI,
617619
moduleName: String,
618620
groupName: String?,
@@ -621,21 +623,21 @@ extension ClangLanguageService {
621623
throw ResponseError.unknown("unsupported method")
622624
}
623625

624-
func indexedRename(_ request: IndexedRenameRequest) async throws -> WorkspaceEdit? {
626+
package func indexedRename(_ request: IndexedRenameRequest) async throws -> WorkspaceEdit? {
625627
return try await forwardRequestToClangd(request)
626628
}
627629

628630
// MARK: - Other
629631

630-
func executeCommand(_ req: ExecuteCommandRequest) async throws -> LSPAny? {
632+
package func executeCommand(_ req: ExecuteCommandRequest) async throws -> LSPAny? {
631633
return try await forwardRequestToClangd(req)
632634
}
633635

634-
func getReferenceDocument(_ req: GetReferenceDocumentRequest) async throws -> GetReferenceDocumentResponse {
636+
package func getReferenceDocument(_ req: GetReferenceDocumentRequest) async throws -> GetReferenceDocumentResponse {
635637
throw ResponseError.unknown("unsupported method")
636638
}
637639

638-
func rename(_ renameRequest: RenameRequest) async throws -> (edits: WorkspaceEdit, usr: String?) {
640+
package func rename(_ renameRequest: RenameRequest) async throws -> (edits: WorkspaceEdit, usr: String?) {
639641
async let edits = forwardRequestToClangd(renameRequest)
640642
let symbolInfoRequest = SymbolInfoRequest(
641643
textDocument: renameRequest.textDocument,
@@ -645,7 +647,11 @@ extension ClangLanguageService {
645647
return (try await edits ?? WorkspaceEdit(), symbolDetail?.usr)
646648
}
647649

648-
func editsToRename(
650+
package func syntacticDocumentTests(for uri: DocumentURI, in workspace: Workspace) async -> [AnnotatedTestItem]? {
651+
return nil
652+
}
653+
654+
package func editsToRename(
649655
locations renameLocations: [RenameLocation],
650656
in snapshot: DocumentSnapshot,
651657
oldName oldCrossLanguageName: CrossLanguageName,

Sources/SourceKitLSP/LanguageServiceRegistry.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,9 @@ import SKLogging
1919
package struct LanguageServiceRegistry {
2020
private var byLanguage: [Language: LanguageService.Type] = [:]
2121

22-
package init() {
23-
self.register(ClangLanguageService.self, for: [.c, .cpp, .objective_c, .objective_cpp])
24-
self.register(SwiftLanguageService.self, for: [.swift])
25-
self.register(DocumentationLanguageService.self, for: [.markdown, .tutorial])
26-
}
22+
package init() {}
2723

28-
private mutating func register(_ languageService: LanguageService.Type, for languages: [Language]) {
24+
package mutating func register(_ languageService: LanguageService.Type, for languages: [Language]) {
2925
for language in languages {
3026
if let existingLanguageService = byLanguage[language] {
3127
logger.fault(

Sources/SourceKitLSP/TestDiscovery.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -623,9 +623,3 @@ extension SwiftLanguageService {
623623
return (xctestSymbols + swiftTestingSymbols).sorted { $0.testItem.location < $1.testItem.location }
624624
}
625625
}
626-
627-
extension ClangLanguageService {
628-
package func syntacticDocumentTests(for uri: DocumentURI, in workspace: Workspace) async -> [AnnotatedTestItem]? {
629-
return nil
630-
}
631-
}

Sources/sourcekit-lsp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ add_executable(sourcekit-lsp
44
target_link_libraries(sourcekit-lsp PRIVATE
55
BuildServerIntegration
66
Diagnose
7+
InProcessClient
78
LanguageServerProtocol
89
LanguageServerProtocolExtensions
910
LanguageServerProtocolJSONRPC

Sources/sourcekit-lsp/SourceKitLSP.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import Csourcekitd // Not needed here, but fixes debugging...
1616
import Diagnose
1717
import Dispatch
1818
import Foundation
19+
import InProcessClient
1920
import LanguageServerProtocol
2021
import LanguageServerProtocolExtensions
2122
import LanguageServerProtocolJSONRPC
@@ -294,7 +295,7 @@ struct SourceKitLSP: AsyncParsableCommand {
294295
let server = SourceKitLSPServer(
295296
client: clientConnection,
296297
toolchainRegistry: ToolchainRegistry(installPath: Bundle.main.bundleURL),
297-
languageServerRegistry: LanguageServiceRegistry(),
298+
languageServerRegistry: .staticallyKnownServices,
298299
options: globalConfigurationOptions,
299300
hooks: Hooks(),
300301
onExit: {

0 commit comments

Comments
 (0)