Skip to content

Commit 750ddf7

Browse files
authored
Merge pull request #2228 from ahoppen/move-clanglanguageservice
Move ClangLanguageService to its own module
2 parents 56a91e9 + 634ec92 commit 750ddf7

21 files changed

+569
-473
lines changed

Package.swift

Lines changed: 23 additions & 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",
@@ -126,6 +127,27 @@ var targets: [Target] = [
126127
dependencies: []
127128
),
128129

130+
// MARK: ClangLanguageService
131+
132+
.target(
133+
name: "ClangLanguageService",
134+
dependencies: [
135+
"BuildServerIntegration",
136+
"DocCDocumentation",
137+
"LanguageServerProtocol",
138+
"LanguageServerProtocolExtensions",
139+
"LanguageServerProtocolJSONRPC",
140+
"SKLogging",
141+
"SKOptions",
142+
"SourceKitLSP",
143+
"SwiftExtensions",
144+
"ToolchainRegistry",
145+
"TSCExtensions",
146+
] + swiftSyntaxDependencies(["SwiftSyntax"]),
147+
exclude: ["CMakeLists.txt"],
148+
swiftSettings: globalSwiftSettings
149+
),
150+
129151
// MARK: CompletionScoring
130152

131153
.target(
@@ -240,6 +262,7 @@ var targets: [Target] = [
240262
name: "InProcessClient",
241263
dependencies: [
242264
"BuildServerIntegration",
265+
"ClangLanguageService",
243266
"LanguageServerProtocol",
244267
"SKLogging",
245268
"SKOptions",

Sources/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ add_subdirectory(BuildServerProtocol)
55
add_subdirectory(BuildServerIntegration)
66
add_subdirectory(CAtomics)
77
add_subdirectory(CCompletionScoring)
8+
add_subdirectory(ClangLanguageService)
89
add_subdirectory(CompletionScoring)
910
add_subdirectory(Csourcekitd)
1011
add_subdirectory(Diagnose)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
add_library(ClangLanguageService STATIC
2+
ClangLanguageService.swift
3+
SemanticTokenTranslator.swift
4+
)
5+
6+
set_target_properties(ClangLanguageService PROPERTIES
7+
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY}
8+
)
9+
10+
target_link_libraries(ClangLanguageService PUBLIC
11+
LanguageServerProtocol
12+
SKOptions
13+
SourceKitLSP
14+
SwiftExtensions
15+
ToolchainRegistry
16+
SwiftSyntax::SwiftSyntax
17+
)
18+
19+
target_link_libraries(ClangLanguageService PRIVATE
20+
BuildServerIntegration
21+
LanguageServerProtocolExtensions
22+
LanguageServerProtocolJSONRPC
23+
SKLogging
24+
TSCExtensions
25+
)

Sources/SourceKitLSP/Clang/ClangLanguageService.swift renamed to Sources/ClangLanguageService/ClangLanguageService.swift

Lines changed: 108 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@
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 SourceKitLSP
21+
package import SwiftExtensions
22+
package import SwiftSyntax
2223
import TSCExtensions
23-
import ToolchainRegistry
24+
package import ToolchainRegistry
2425

2526
#if canImport(DocCDocumentation)
2627
import DocCDocumentation
@@ -38,7 +39,7 @@ import WinSDK
3839
/// ``ClangLanguageServerShim`` conforms to ``MessageHandler`` to receive
3940
/// requests and notifications **from** clangd, not from the editor, and it will
4041
/// forward these requests and notifications to the editor.
41-
actor ClangLanguageService: LanguageService, MessageHandler {
42+
package actor ClangLanguageService: LanguageService, MessageHandler {
4243
/// The queue on which all messages that originate from clangd are handled.
4344
///
4445
/// These are requests and notifications sent *from* clangd, not replies from
@@ -144,12 +145,12 @@ actor ClangLanguageService: LanguageService, MessageHandler {
144145
return ClangBuildSettings(settings, clangPath: clangPath)
145146
}
146147

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

152-
func addStateChangeHandler(handler: @escaping (LanguageServerState, LanguageServerState) -> Void) {
153+
package func addStateChangeHandler(handler: @escaping (LanguageServerState, LanguageServerState) -> Void) {
153154
self.stateChangeHandlers.append(handler)
154155
}
155156

@@ -244,7 +245,7 @@ actor ClangLanguageService: LanguageService, MessageHandler {
244245
/// sending a notification that's intended for the editor.
245246
///
246247
/// We should either handle it ourselves or forward it to the editor.
247-
nonisolated func handle(_ params: some NotificationType) {
248+
package nonisolated func handle(_ params: some NotificationType) {
248249
logger.info(
249250
"""
250251
Received notification from clangd:
@@ -267,7 +268,7 @@ actor ClangLanguageService: LanguageService, MessageHandler {
267268
/// sending a notification that's intended for the editor.
268269
///
269270
/// We should either handle it ourselves or forward it to the client.
270-
nonisolated func handle<R: RequestType>(
271+
package nonisolated func handle<R: RequestType>(
271272
_ params: R,
272273
id: RequestID,
273274
reply: @Sendable @escaping (LSPResult<R.Response>) -> Void
@@ -316,7 +317,7 @@ actor ClangLanguageService: LanguageService, MessageHandler {
316317
return nil
317318
}
318319

319-
func crash() {
320+
package func crash() {
320321
clangdProcess?.terminateImmediately()
321322
}
322323
}
@@ -366,7 +367,7 @@ extension ClangLanguageService {
366367

367368
extension ClangLanguageService {
368369

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

@@ -417,7 +418,7 @@ extension ClangLanguageService {
417418
clangd.send(notification)
418419
}
419420

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

422423
package func changeDocument(
423424
_ notification: DidChangeTextDocumentNotification,
@@ -481,49 +482,51 @@ extension ClangLanguageService {
481482
return try await forwardRequestToClangd(req)
482483
}
483484

484-
func completion(_ req: CompletionRequest) async throws -> CompletionList {
485+
package func completion(_ req: CompletionRequest) async throws -> CompletionList {
485486
return try await forwardRequestToClangd(req)
486487
}
487488

488-
func completionItemResolve(_ req: CompletionItemResolveRequest) async throws -> CompletionItem {
489+
package func completionItemResolve(_ req: CompletionItemResolveRequest) async throws -> CompletionItem {
489490
return try await forwardRequestToClangd(req)
490491
}
491492

492-
func hover(_ req: HoverRequest) async throws -> HoverResponse? {
493+
package func hover(_ req: HoverRequest) async throws -> HoverResponse? {
493494
return try await forwardRequestToClangd(req)
494495
}
495496

496497
#if canImport(DocCDocumentation)
497-
func doccDocumentation(_ req: DoccDocumentationRequest) async throws -> DoccDocumentationResponse {
498+
package func doccDocumentation(_ req: DoccDocumentationRequest) async throws -> DoccDocumentationResponse {
498499
guard let sourceKitLSPServer else {
499500
throw ResponseError.unknown("Connection to the editor closed")
500501
}
501502

502-
let snapshot = try sourceKitLSPServer.documentManager.latestSnapshot(req.textDocument.uri)
503+
let snapshot = try await sourceKitLSPServer.documentManager.latestSnapshot(req.textDocument.uri)
503504
throw ResponseError.requestFailed(doccDocumentationError: .unsupportedLanguage(snapshot.language))
504505
}
505506
#endif
506507

507-
func symbolInfo(_ req: SymbolInfoRequest) async throws -> [SymbolDetails] {
508+
package func symbolInfo(_ req: SymbolInfoRequest) async throws -> [SymbolDetails] {
508509
return try await forwardRequestToClangd(req)
509510
}
510511

511-
func documentSymbolHighlight(_ req: DocumentHighlightRequest) async throws -> [DocumentHighlight]? {
512+
package func documentSymbolHighlight(_ req: DocumentHighlightRequest) async throws -> [DocumentHighlight]? {
512513
return try await forwardRequestToClangd(req)
513514
}
514515

515-
func documentSymbol(_ req: DocumentSymbolRequest) async throws -> DocumentSymbolResponse? {
516+
package func documentSymbol(_ req: DocumentSymbolRequest) async throws -> DocumentSymbolResponse? {
516517
return try await forwardRequestToClangd(req)
517518
}
518519

519-
func documentColor(_ req: DocumentColorRequest) async throws -> [ColorInformation] {
520+
package func documentColor(_ req: DocumentColorRequest) async throws -> [ColorInformation] {
520521
guard self.capabilities?.colorProvider?.isSupported ?? false else {
521522
return []
522523
}
523524
return try await forwardRequestToClangd(req)
524525
}
525526

526-
func documentSemanticTokens(_ req: DocumentSemanticTokensRequest) async throws -> DocumentSemanticTokensResponse? {
527+
package func documentSemanticTokens(
528+
_ req: DocumentSemanticTokensRequest
529+
) async throws -> DocumentSemanticTokensResponse? {
527530
guard var response = try await forwardRequestToClangd(req) else {
528531
return nil
529532
}
@@ -533,7 +536,7 @@ extension ClangLanguageService {
533536
return response
534537
}
535538

536-
func documentSemanticTokensDelta(
539+
package func documentSemanticTokensDelta(
537540
_ req: DocumentSemanticTokensDeltaRequest
538541
) async throws -> DocumentSemanticTokensDeltaResponse? {
539542
guard var response = try await forwardRequestToClangd(req) else {
@@ -558,7 +561,7 @@ extension ClangLanguageService {
558561
return response
559562
}
560563

561-
func documentSemanticTokensRange(
564+
package func documentSemanticTokensRange(
562565
_ req: DocumentSemanticTokensRangeRequest
563566
) async throws -> DocumentSemanticTokensResponse? {
564567
guard var response = try await forwardRequestToClangd(req) else {
@@ -570,49 +573,49 @@ extension ClangLanguageService {
570573
return response
571574
}
572575

573-
func colorPresentation(_ req: ColorPresentationRequest) async throws -> [ColorPresentation] {
576+
package func colorPresentation(_ req: ColorPresentationRequest) async throws -> [ColorPresentation] {
574577
guard self.capabilities?.colorProvider?.isSupported ?? false else {
575578
return []
576579
}
577580
return try await forwardRequestToClangd(req)
578581
}
579582

580-
func documentFormatting(_ req: DocumentFormattingRequest) async throws -> [TextEdit]? {
583+
package func documentFormatting(_ req: DocumentFormattingRequest) async throws -> [TextEdit]? {
581584
return try await forwardRequestToClangd(req)
582585
}
583586

584-
func documentRangeFormatting(_ req: DocumentRangeFormattingRequest) async throws -> [TextEdit]? {
587+
package func documentRangeFormatting(_ req: DocumentRangeFormattingRequest) async throws -> [TextEdit]? {
585588
return try await forwardRequestToClangd(req)
586589
}
587590

588-
func documentOnTypeFormatting(_ req: DocumentOnTypeFormattingRequest) async throws -> [TextEdit]? {
591+
package func documentOnTypeFormatting(_ req: DocumentOnTypeFormattingRequest) async throws -> [TextEdit]? {
589592
return try await forwardRequestToClangd(req)
590593
}
591594

592-
func codeAction(_ req: CodeActionRequest) async throws -> CodeActionRequestResponse? {
595+
package func codeAction(_ req: CodeActionRequest) async throws -> CodeActionRequestResponse? {
593596
return try await forwardRequestToClangd(req)
594597
}
595598

596-
func inlayHint(_ req: InlayHintRequest) async throws -> [InlayHint] {
599+
package func inlayHint(_ req: InlayHintRequest) async throws -> [InlayHint] {
597600
return try await forwardRequestToClangd(req)
598601
}
599602

600-
func documentDiagnostic(_ req: DocumentDiagnosticsRequest) async throws -> DocumentDiagnosticReport {
603+
package func documentDiagnostic(_ req: DocumentDiagnosticsRequest) async throws -> DocumentDiagnosticReport {
601604
return try await forwardRequestToClangd(req)
602605
}
603606

604-
func codeLens(_ req: CodeLensRequest) async throws -> [CodeLens] {
607+
package func codeLens(_ req: CodeLensRequest) async throws -> [CodeLens] {
605608
return try await forwardRequestToClangd(req) ?? []
606609
}
607610

608-
func foldingRange(_ req: FoldingRangeRequest) async throws -> [FoldingRange]? {
611+
package func foldingRange(_ req: FoldingRangeRequest) async throws -> [FoldingRange]? {
609612
guard self.capabilities?.foldingRangeProvider?.isSupported ?? false else {
610613
return nil
611614
}
612615
return try await forwardRequestToClangd(req)
613616
}
614617

615-
func openGeneratedInterface(
618+
package func openGeneratedInterface(
616619
document: DocumentURI,
617620
moduleName: String,
618621
groupName: String?,
@@ -621,19 +624,86 @@ extension ClangLanguageService {
621624
throw ResponseError.unknown("unsupported method")
622625
}
623626

624-
func indexedRename(_ request: IndexedRenameRequest) async throws -> WorkspaceEdit? {
627+
package func indexedRename(_ request: IndexedRenameRequest) async throws -> WorkspaceEdit? {
625628
return try await forwardRequestToClangd(request)
626629
}
627630

628631
// MARK: - Other
629632

630-
func executeCommand(_ req: ExecuteCommandRequest) async throws -> LSPAny? {
633+
package func executeCommand(_ req: ExecuteCommandRequest) async throws -> LSPAny? {
631634
return try await forwardRequestToClangd(req)
632635
}
633636

634-
func getReferenceDocument(_ req: GetReferenceDocumentRequest) async throws -> GetReferenceDocumentResponse {
637+
package func getReferenceDocument(_ req: GetReferenceDocumentRequest) async throws -> GetReferenceDocumentResponse {
635638
throw ResponseError.unknown("unsupported method")
636639
}
640+
641+
package func rename(_ renameRequest: RenameRequest) async throws -> (edits: WorkspaceEdit, usr: String?) {
642+
async let edits = forwardRequestToClangd(renameRequest)
643+
let symbolInfoRequest = SymbolInfoRequest(
644+
textDocument: renameRequest.textDocument,
645+
position: renameRequest.position
646+
)
647+
let symbolDetail = try await forwardRequestToClangd(symbolInfoRequest).only
648+
return (try await edits ?? WorkspaceEdit(), symbolDetail?.usr)
649+
}
650+
651+
package func syntacticDocumentTests(for uri: DocumentURI, in workspace: Workspace) async -> [AnnotatedTestItem]? {
652+
return nil
653+
}
654+
655+
package func editsToRename(
656+
locations renameLocations: [RenameLocation],
657+
in snapshot: DocumentSnapshot,
658+
oldName oldCrossLanguageName: CrossLanguageName,
659+
newName newCrossLanguageName: CrossLanguageName
660+
) async throws -> [TextEdit] {
661+
let positions = [
662+
snapshot.uri: renameLocations.compactMap { snapshot.position(of: $0) }
663+
]
664+
guard
665+
let oldName = oldCrossLanguageName.clangName,
666+
let newName = newCrossLanguageName.clangName
667+
else {
668+
throw ResponseError.unknown(
669+
"Failed to rename \(snapshot.uri.forLogging) because the clang name for rename is unknown"
670+
)
671+
}
672+
let request = IndexedRenameRequest(
673+
textDocument: TextDocumentIdentifier(snapshot.uri),
674+
oldName: oldName,
675+
newName: newName,
676+
positions: positions
677+
)
678+
do {
679+
let edits = try await forwardRequestToClangd(request)
680+
return edits?.changes?[snapshot.uri] ?? []
681+
} catch {
682+
logger.error("Failed to get indexed rename edits: \(error.forLogging)")
683+
return []
684+
}
685+
}
686+
687+
package func prepareRename(
688+
_ request: PrepareRenameRequest
689+
) async throws -> (prepareRename: PrepareRenameResponse, usr: String?)? {
690+
guard let prepareRename = try await forwardRequestToClangd(request) else {
691+
return nil
692+
}
693+
let symbolInfo = try await forwardRequestToClangd(
694+
SymbolInfoRequest(textDocument: request.textDocument, position: request.position)
695+
)
696+
return (prepareRename, symbolInfo.only?.usr)
697+
}
698+
699+
package func editsToRenameParametersInFunctionBody(
700+
snapshot: DocumentSnapshot,
701+
renameLocation: RenameLocation,
702+
newName: CrossLanguageName
703+
) async -> [TextEdit] {
704+
// When renaming a clang function name, we don't need to rename any references to the arguments.
705+
return []
706+
}
637707
}
638708

639709
/// Clang build settings derived from a `FileBuildSettingsChange`.

0 commit comments

Comments
 (0)