12
12
13
13
import BuildServerIntegration
14
14
import Foundation
15
- import LanguageServerProtocol
15
+ package import LanguageServerProtocol
16
16
import LanguageServerProtocolExtensions
17
17
import LanguageServerProtocolJSONRPC
18
18
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
22
23
import TSCExtensions
23
- import ToolchainRegistry
24
+ package import ToolchainRegistry
24
25
25
26
#if canImport(DocCDocumentation)
26
27
import DocCDocumentation
@@ -38,7 +39,7 @@ import WinSDK
38
39
/// ``ClangLanguageServerShim`` conforms to ``MessageHandler`` to receive
39
40
/// requests and notifications **from** clangd, not from the editor, and it will
40
41
/// forward these requests and notifications to the editor.
41
- actor ClangLanguageService : LanguageService , MessageHandler {
42
+ package actor ClangLanguageService : LanguageService , MessageHandler {
42
43
/// The queue on which all messages that originate from clangd are handled.
43
44
///
44
45
/// These are requests and notifications sent *from* clangd, not replies from
@@ -144,12 +145,12 @@ actor ClangLanguageService: LanguageService, MessageHandler {
144
145
return ClangBuildSettings ( settings, clangPath: clangPath)
145
146
}
146
147
147
- nonisolated func canHandle( workspace: Workspace , toolchain: Toolchain ) -> Bool {
148
+ package nonisolated func canHandle( workspace: Workspace , toolchain: Toolchain ) -> Bool {
148
149
// We launch different clangd instance for each workspace because clangd doesn't have multi-root workspace support.
149
150
return workspace === self . workspace. value && self . clangdPath == toolchain. clangd
150
151
}
151
152
152
- func addStateChangeHandler( handler: @escaping ( LanguageServerState , LanguageServerState ) -> Void ) {
153
+ package func addStateChangeHandler( handler: @escaping ( LanguageServerState , LanguageServerState ) -> Void ) {
153
154
self . stateChangeHandlers. append ( handler)
154
155
}
155
156
@@ -244,7 +245,7 @@ actor ClangLanguageService: LanguageService, MessageHandler {
244
245
/// sending a notification that's intended for the editor.
245
246
///
246
247
/// 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 ) {
248
249
logger. info (
249
250
"""
250
251
Received notification from clangd:
@@ -267,7 +268,7 @@ actor ClangLanguageService: LanguageService, MessageHandler {
267
268
/// sending a notification that's intended for the editor.
268
269
///
269
270
/// 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 > (
271
272
_ params: R ,
272
273
id: RequestID ,
273
274
reply: @Sendable @escaping ( LSPResult < R . Response > ) -> Void
@@ -316,7 +317,7 @@ actor ClangLanguageService: LanguageService, MessageHandler {
316
317
return nil
317
318
}
318
319
319
- func crash( ) {
320
+ package func crash( ) {
320
321
clangdProcess? . terminateImmediately ( )
321
322
}
322
323
}
@@ -366,7 +367,7 @@ extension ClangLanguageService {
366
367
367
368
extension ClangLanguageService {
368
369
369
- func initialize( _ initialize: InitializeRequest ) async throws -> InitializeResult {
370
+ package func initialize( _ initialize: InitializeRequest ) async throws -> InitializeResult {
370
371
// Store the initialize request so we can replay it in case clangd crashes
371
372
self . initializeRequest = initialize
372
373
@@ -417,7 +418,7 @@ extension ClangLanguageService {
417
418
clangd. send ( notification)
418
419
}
419
420
420
- func reopenDocument( _ notification: ReopenTextDocumentNotification ) { }
421
+ package func reopenDocument( _ notification: ReopenTextDocumentNotification ) { }
421
422
422
423
package func changeDocument(
423
424
_ notification: DidChangeTextDocumentNotification ,
@@ -481,49 +482,51 @@ extension ClangLanguageService {
481
482
return try await forwardRequestToClangd ( req)
482
483
}
483
484
484
- func completion( _ req: CompletionRequest ) async throws -> CompletionList {
485
+ package func completion( _ req: CompletionRequest ) async throws -> CompletionList {
485
486
return try await forwardRequestToClangd ( req)
486
487
}
487
488
488
- func completionItemResolve( _ req: CompletionItemResolveRequest ) async throws -> CompletionItem {
489
+ package func completionItemResolve( _ req: CompletionItemResolveRequest ) async throws -> CompletionItem {
489
490
return try await forwardRequestToClangd ( req)
490
491
}
491
492
492
- func hover( _ req: HoverRequest ) async throws -> HoverResponse ? {
493
+ package func hover( _ req: HoverRequest ) async throws -> HoverResponse ? {
493
494
return try await forwardRequestToClangd ( req)
494
495
}
495
496
496
497
#if canImport(DocCDocumentation)
497
- func doccDocumentation( _ req: DoccDocumentationRequest ) async throws -> DoccDocumentationResponse {
498
+ package func doccDocumentation( _ req: DoccDocumentationRequest ) async throws -> DoccDocumentationResponse {
498
499
guard let sourceKitLSPServer else {
499
500
throw ResponseError . unknown ( " Connection to the editor closed " )
500
501
}
501
502
502
- let snapshot = try sourceKitLSPServer. documentManager. latestSnapshot ( req. textDocument. uri)
503
+ let snapshot = try await sourceKitLSPServer. documentManager. latestSnapshot ( req. textDocument. uri)
503
504
throw ResponseError . requestFailed ( doccDocumentationError: . unsupportedLanguage( snapshot. language) )
504
505
}
505
506
#endif
506
507
507
- func symbolInfo( _ req: SymbolInfoRequest ) async throws -> [ SymbolDetails ] {
508
+ package func symbolInfo( _ req: SymbolInfoRequest ) async throws -> [ SymbolDetails ] {
508
509
return try await forwardRequestToClangd ( req)
509
510
}
510
511
511
- func documentSymbolHighlight( _ req: DocumentHighlightRequest ) async throws -> [ DocumentHighlight ] ? {
512
+ package func documentSymbolHighlight( _ req: DocumentHighlightRequest ) async throws -> [ DocumentHighlight ] ? {
512
513
return try await forwardRequestToClangd ( req)
513
514
}
514
515
515
- func documentSymbol( _ req: DocumentSymbolRequest ) async throws -> DocumentSymbolResponse ? {
516
+ package func documentSymbol( _ req: DocumentSymbolRequest ) async throws -> DocumentSymbolResponse ? {
516
517
return try await forwardRequestToClangd ( req)
517
518
}
518
519
519
- func documentColor( _ req: DocumentColorRequest ) async throws -> [ ColorInformation ] {
520
+ package func documentColor( _ req: DocumentColorRequest ) async throws -> [ ColorInformation ] {
520
521
guard self . capabilities? . colorProvider? . isSupported ?? false else {
521
522
return [ ]
522
523
}
523
524
return try await forwardRequestToClangd ( req)
524
525
}
525
526
526
- func documentSemanticTokens( _ req: DocumentSemanticTokensRequest ) async throws -> DocumentSemanticTokensResponse ? {
527
+ package func documentSemanticTokens(
528
+ _ req: DocumentSemanticTokensRequest
529
+ ) async throws -> DocumentSemanticTokensResponse ? {
527
530
guard var response = try await forwardRequestToClangd ( req) else {
528
531
return nil
529
532
}
@@ -533,7 +536,7 @@ extension ClangLanguageService {
533
536
return response
534
537
}
535
538
536
- func documentSemanticTokensDelta(
539
+ package func documentSemanticTokensDelta(
537
540
_ req: DocumentSemanticTokensDeltaRequest
538
541
) async throws -> DocumentSemanticTokensDeltaResponse ? {
539
542
guard var response = try await forwardRequestToClangd ( req) else {
@@ -558,7 +561,7 @@ extension ClangLanguageService {
558
561
return response
559
562
}
560
563
561
- func documentSemanticTokensRange(
564
+ package func documentSemanticTokensRange(
562
565
_ req: DocumentSemanticTokensRangeRequest
563
566
) async throws -> DocumentSemanticTokensResponse ? {
564
567
guard var response = try await forwardRequestToClangd ( req) else {
@@ -570,49 +573,49 @@ extension ClangLanguageService {
570
573
return response
571
574
}
572
575
573
- func colorPresentation( _ req: ColorPresentationRequest ) async throws -> [ ColorPresentation ] {
576
+ package func colorPresentation( _ req: ColorPresentationRequest ) async throws -> [ ColorPresentation ] {
574
577
guard self . capabilities? . colorProvider? . isSupported ?? false else {
575
578
return [ ]
576
579
}
577
580
return try await forwardRequestToClangd ( req)
578
581
}
579
582
580
- func documentFormatting( _ req: DocumentFormattingRequest ) async throws -> [ TextEdit ] ? {
583
+ package func documentFormatting( _ req: DocumentFormattingRequest ) async throws -> [ TextEdit ] ? {
581
584
return try await forwardRequestToClangd ( req)
582
585
}
583
586
584
- func documentRangeFormatting( _ req: DocumentRangeFormattingRequest ) async throws -> [ TextEdit ] ? {
587
+ package func documentRangeFormatting( _ req: DocumentRangeFormattingRequest ) async throws -> [ TextEdit ] ? {
585
588
return try await forwardRequestToClangd ( req)
586
589
}
587
590
588
- func documentOnTypeFormatting( _ req: DocumentOnTypeFormattingRequest ) async throws -> [ TextEdit ] ? {
591
+ package func documentOnTypeFormatting( _ req: DocumentOnTypeFormattingRequest ) async throws -> [ TextEdit ] ? {
589
592
return try await forwardRequestToClangd ( req)
590
593
}
591
594
592
- func codeAction( _ req: CodeActionRequest ) async throws -> CodeActionRequestResponse ? {
595
+ package func codeAction( _ req: CodeActionRequest ) async throws -> CodeActionRequestResponse ? {
593
596
return try await forwardRequestToClangd ( req)
594
597
}
595
598
596
- func inlayHint( _ req: InlayHintRequest ) async throws -> [ InlayHint ] {
599
+ package func inlayHint( _ req: InlayHintRequest ) async throws -> [ InlayHint ] {
597
600
return try await forwardRequestToClangd ( req)
598
601
}
599
602
600
- func documentDiagnostic( _ req: DocumentDiagnosticsRequest ) async throws -> DocumentDiagnosticReport {
603
+ package func documentDiagnostic( _ req: DocumentDiagnosticsRequest ) async throws -> DocumentDiagnosticReport {
601
604
return try await forwardRequestToClangd ( req)
602
605
}
603
606
604
- func codeLens( _ req: CodeLensRequest ) async throws -> [ CodeLens ] {
607
+ package func codeLens( _ req: CodeLensRequest ) async throws -> [ CodeLens ] {
605
608
return try await forwardRequestToClangd ( req) ?? [ ]
606
609
}
607
610
608
- func foldingRange( _ req: FoldingRangeRequest ) async throws -> [ FoldingRange ] ? {
611
+ package func foldingRange( _ req: FoldingRangeRequest ) async throws -> [ FoldingRange ] ? {
609
612
guard self . capabilities? . foldingRangeProvider? . isSupported ?? false else {
610
613
return nil
611
614
}
612
615
return try await forwardRequestToClangd ( req)
613
616
}
614
617
615
- func openGeneratedInterface(
618
+ package func openGeneratedInterface(
616
619
document: DocumentURI ,
617
620
moduleName: String ,
618
621
groupName: String ? ,
@@ -621,19 +624,86 @@ extension ClangLanguageService {
621
624
throw ResponseError . unknown ( " unsupported method " )
622
625
}
623
626
624
- func indexedRename( _ request: IndexedRenameRequest ) async throws -> WorkspaceEdit ? {
627
+ package func indexedRename( _ request: IndexedRenameRequest ) async throws -> WorkspaceEdit ? {
625
628
return try await forwardRequestToClangd ( request)
626
629
}
627
630
628
631
// MARK: - Other
629
632
630
- func executeCommand( _ req: ExecuteCommandRequest ) async throws -> LSPAny ? {
633
+ package func executeCommand( _ req: ExecuteCommandRequest ) async throws -> LSPAny ? {
631
634
return try await forwardRequestToClangd ( req)
632
635
}
633
636
634
- func getReferenceDocument( _ req: GetReferenceDocumentRequest ) async throws -> GetReferenceDocumentResponse {
637
+ package func getReferenceDocument( _ req: GetReferenceDocumentRequest ) async throws -> GetReferenceDocumentResponse {
635
638
throw ResponseError . unknown ( " unsupported method " )
636
639
}
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
+ }
637
707
}
638
708
639
709
/// Clang build settings derived from a `FileBuildSettingsChange`.
0 commit comments