@@ -357,7 +357,7 @@ package actor SourceKitLSPServer {
357
357
358
358
// This should be created as soon as we receive an open call, even if the document
359
359
// isn't yet ready.
360
- guard let languageService = workspace. documentService. value [ doc] else {
360
+ guard let languageService = workspace. documentService ( for : doc) else {
361
361
return
362
362
}
363
363
@@ -377,7 +377,7 @@ package actor SourceKitLSPServer {
377
377
guard let workspace = await self . workspaceForDocument ( uri: request. textDocument. uri) else {
378
378
throw ResponseError . workspaceNotOpen ( request. textDocument. uri)
379
379
}
380
- guard let languageService = workspace. documentService. value [ doc] else {
380
+ guard let languageService = workspace. documentService ( for : doc) else {
381
381
throw ResponseError . unknown ( " No language service for ' \( request. textDocument. uri) ' found " )
382
382
}
383
383
return try await requestHandler ( request, workspace, languageService)
@@ -400,7 +400,7 @@ package actor SourceKitLSPServer {
400
400
guard let workspace = await self . workspaceForDocument ( uri: documentUri) else {
401
401
continue
402
402
}
403
- guard workspace. documentService. value [ documentUri] === languageService else {
403
+ guard workspace. documentService ( for : documentUri) === languageService else {
404
404
continue
405
405
}
406
406
guard let snapshot = try ? self . documentManager. latestSnapshot ( documentUri) else {
@@ -518,7 +518,7 @@ package actor SourceKitLSPServer {
518
518
_ language: Language ,
519
519
in workspace: Workspace
520
520
) async -> LanguageService ? {
521
- if let service = workspace. documentService. value [ uri] {
521
+ if let service = workspace. documentService ( for : uri) {
522
522
return service
523
523
}
524
524
@@ -536,17 +536,7 @@ package actor SourceKitLSPServer {
536
536
"""
537
537
)
538
538
539
- return workspace. documentService. withLock { documentService in
540
- if let concurrentlySetService = documentService [ uri] {
541
- // Since we await the construction of `service`, another call to this
542
- // function might have happened and raced us, setting
543
- // `workspace.documentServices[uri]`. If this is the case, return the
544
- // existing value and discard the service that we just retrieved.
545
- return concurrentlySetService
546
- }
547
- documentService [ uri] = service
548
- return service
549
- }
539
+ return workspace. setDocumentService ( for: uri, service)
550
540
}
551
541
}
552
542
@@ -733,6 +723,8 @@ extension SourceKitLSPServer: MessageHandler {
733
723
await request. reply { try await executeCommand ( request. params) }
734
724
case let request as RequestAndReply < FoldingRangeRequest > :
735
725
await self . handleRequest ( for: request, requestHandler: self . foldingRange)
726
+ case let request as RequestAndReply < GetReferenceDocumentRequest > :
727
+ await request. reply { try await getReferenceDocument ( request. params) }
736
728
case let request as RequestAndReply < HoverRequest > :
737
729
await self . handleRequest ( for: request, requestHandler: self . hover)
738
730
case let request as RequestAndReply < ImplementationRequest > :
@@ -804,7 +796,7 @@ extension SourceKitLSPServer: BuildSystemDelegate {
804
796
continue
805
797
}
806
798
807
- guard let service = await self . workspaceForDocument ( uri: uri) ? . documentService. value [ uri] else {
799
+ guard let service = await self . workspaceForDocument ( uri: uri) ? . documentService ( for : uri) else {
808
800
continue
809
801
}
810
802
@@ -828,7 +820,7 @@ extension SourceKitLSPServer: BuildSystemDelegate {
828
820
}
829
821
for uri in self . affectedOpenDocumentsForChangeSet ( changedFilesForWorkspace, self . documentManager) {
830
822
logger. log ( " Dependencies updated for opened file \( uri. forLogging) " )
831
- if let service = workspace. documentService. value [ uri] {
823
+ if let service = workspace. documentService ( for : uri) {
832
824
await service. documentDependenciesUpdated ( uri)
833
825
}
834
826
}
@@ -963,6 +955,8 @@ extension SourceKitLSPServer {
963
955
//
964
956
// The below is a workaround for the vscode-swift extension since it cannot set client capabilities.
965
957
// It passes "workspace/peekDocuments" through the `initializationOptions`.
958
+ //
959
+ // Similarly, for "workspace/getReferenceDocument".
966
960
var clientCapabilities = req. capabilities
967
961
if case . dictionary( let initializationOptions) = req. initializationOptions {
968
962
if let peekDocuments = initializationOptions [ " workspace/peekDocuments " ] {
@@ -974,6 +968,15 @@ extension SourceKitLSPServer {
974
968
}
975
969
}
976
970
971
+ if let getReferenceDocument = initializationOptions [ " workspace/getReferenceDocument " ] {
972
+ if case . dictionary( var experimentalCapabilities) = clientCapabilities. experimental {
973
+ experimentalCapabilities [ " workspace/getReferenceDocument " ] = getReferenceDocument
974
+ clientCapabilities. experimental = . dictionary( experimentalCapabilities)
975
+ } else {
976
+ clientCapabilities. experimental = . dictionary( [ " workspace/getReferenceDocument " : getReferenceDocument] )
977
+ }
978
+ }
979
+
977
980
// The client announces what CodeLenses it supports, and the LSP will only return
978
981
// ones found in the supportedCommands dictionary.
979
982
if let codeLens = initializationOptions [ " textDocument/codeLens " ] ,
@@ -1140,6 +1143,7 @@ extension SourceKitLSPServer {
1140
1143
" workspace/tests " : . dictionary( [ " version " : . int( 2 ) ] ) ,
1141
1144
" textDocument/tests " : . dictionary( [ " version " : . int( 2 ) ] ) ,
1142
1145
" workspace/triggerReindex " : . dictionary( [ " version " : . int( 1 ) ] ) ,
1146
+ " workspace/getReferenceDocument " : . dictionary( [ " version " : . int( 1 ) ] ) ,
1143
1147
] )
1144
1148
)
1145
1149
}
@@ -1339,7 +1343,7 @@ extension SourceKitLSPServer {
1339
1343
)
1340
1344
return
1341
1345
}
1342
- await workspace. documentService. value [ uri] ? . reopenDocument ( notification)
1346
+ await workspace. documentService ( for : uri) ? . reopenDocument ( notification)
1343
1347
}
1344
1348
1345
1349
func closeDocument( _ notification: DidCloseTextDocumentNotification , workspace: Workspace ) async {
@@ -1353,7 +1357,7 @@ extension SourceKitLSPServer {
1353
1357
1354
1358
await workspace. buildSystemManager. unregisterForChangeNotifications ( for: uri)
1355
1359
1356
- await workspace. documentService. value [ uri] ? . closeDocument ( notification)
1360
+ await workspace. documentService ( for : uri) ? . closeDocument ( notification)
1357
1361
}
1358
1362
1359
1363
func changeDocument( _ notification: DidChangeTextDocumentNotification ) async {
@@ -1379,7 +1383,7 @@ extension SourceKitLSPServer {
1379
1383
// Already logged failure
1380
1384
return
1381
1385
}
1382
- await workspace. documentService. value [ uri] ? . changeDocument (
1386
+ await workspace. documentService ( for : uri) ? . changeDocument (
1383
1387
notification,
1384
1388
preEditSnapshot: preEditSnapshot,
1385
1389
postEditSnapshot: postEditSnapshot,
@@ -1642,7 +1646,7 @@ extension SourceKitLSPServer {
1642
1646
guard let workspace = await workspaceForDocument ( uri: uri) else {
1643
1647
throw ResponseError . workspaceNotOpen ( uri)
1644
1648
}
1645
- guard let languageService = workspace. documentService. value [ uri] else {
1649
+ guard let languageService = workspace. documentService ( for : uri) else {
1646
1650
return nil
1647
1651
}
1648
1652
@@ -1653,6 +1657,21 @@ extension SourceKitLSPServer {
1653
1657
return try await languageService. executeCommand ( executeCommand)
1654
1658
}
1655
1659
1660
+ func getReferenceDocument( _ req: GetReferenceDocumentRequest ) async throws -> GetReferenceDocumentResponse {
1661
+ let referenceDocumentURL = try ReferenceDocumentURL ( from: req. uri)
1662
+ let primaryFileURI = referenceDocumentURL. primaryFile
1663
+
1664
+ guard let workspace = await workspaceForDocument ( uri: primaryFileURI) else {
1665
+ throw ResponseError . workspaceNotOpen ( primaryFileURI)
1666
+ }
1667
+
1668
+ guard let languageService = workspace. documentService ( for: primaryFileURI) else {
1669
+ throw ResponseError . unknown ( " No Language Service for URI: \( primaryFileURI) " )
1670
+ }
1671
+
1672
+ return try await languageService. getReferenceDocument ( req)
1673
+ }
1674
+
1656
1675
func codeAction(
1657
1676
_ req: CodeActionRequest ,
1658
1677
workspace: Workspace ,
0 commit comments