@@ -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
}
@@ -964,6 +956,8 @@ extension SourceKitLSPServer {
964
956
//
965
957
// The below is a workaround for the vscode-swift extension since it cannot set client capabilities.
966
958
// It passes "workspace/peekDocuments" through the `initializationOptions`.
959
+ //
960
+ // Similarly, for "workspace/getReferenceDocument".
967
961
var clientCapabilities = req. capabilities
968
962
if case . dictionary( let initializationOptions) = req. initializationOptions {
969
963
if let peekDocuments = initializationOptions [ " workspace/peekDocuments " ] {
@@ -975,6 +969,15 @@ extension SourceKitLSPServer {
975
969
}
976
970
}
977
971
972
+ if let getReferenceDocument = initializationOptions [ " workspace/getReferenceDocument " ] {
973
+ if case . dictionary( var experimentalCapabilities) = clientCapabilities. experimental {
974
+ experimentalCapabilities [ " workspace/getReferenceDocument " ] = getReferenceDocument
975
+ clientCapabilities. experimental = . dictionary( experimentalCapabilities)
976
+ } else {
977
+ clientCapabilities. experimental = . dictionary( [ " workspace/getReferenceDocument " : getReferenceDocument] )
978
+ }
979
+ }
980
+
978
981
// The client announces what CodeLenses it supports, and the LSP will only return
979
982
// ones found in the supportedCommands dictionary.
980
983
if let codeLens = initializationOptions [ " textDocument/codeLens " ] ,
@@ -1143,6 +1146,7 @@ extension SourceKitLSPServer {
1143
1146
" workspace/tests " : . dictionary( [ " version " : . int( 2 ) ] ) ,
1144
1147
" textDocument/tests " : . dictionary( [ " version " : . int( 2 ) ] ) ,
1145
1148
" workspace/triggerReindex " : . dictionary( [ " version " : . int( 1 ) ] ) ,
1149
+ " workspace/getReferenceDocument " : . dictionary( [ " version " : . int( 1 ) ] ) ,
1146
1150
] )
1147
1151
)
1148
1152
}
@@ -1342,7 +1346,7 @@ extension SourceKitLSPServer {
1342
1346
)
1343
1347
return
1344
1348
}
1345
- await workspace. documentService. value [ uri] ? . reopenDocument ( notification)
1349
+ await workspace. documentService ( for : uri) ? . reopenDocument ( notification)
1346
1350
}
1347
1351
1348
1352
func closeDocument( _ notification: DidCloseTextDocumentNotification , workspace: Workspace ) async {
@@ -1356,7 +1360,7 @@ extension SourceKitLSPServer {
1356
1360
1357
1361
await workspace. buildSystemManager. unregisterForChangeNotifications ( for: uri)
1358
1362
1359
- await workspace. documentService. value [ uri] ? . closeDocument ( notification)
1363
+ await workspace. documentService ( for : uri) ? . closeDocument ( notification)
1360
1364
}
1361
1365
1362
1366
func changeDocument( _ notification: DidChangeTextDocumentNotification ) async {
@@ -1382,7 +1386,7 @@ extension SourceKitLSPServer {
1382
1386
// Already logged failure
1383
1387
return
1384
1388
}
1385
- await workspace. documentService. value [ uri] ? . changeDocument (
1389
+ await workspace. documentService ( for : uri) ? . changeDocument (
1386
1390
notification,
1387
1391
preEditSnapshot: preEditSnapshot,
1388
1392
postEditSnapshot: postEditSnapshot,
@@ -1645,7 +1649,7 @@ extension SourceKitLSPServer {
1645
1649
guard let workspace = await workspaceForDocument ( uri: uri) else {
1646
1650
throw ResponseError . workspaceNotOpen ( uri)
1647
1651
}
1648
- guard let languageService = workspace. documentService. value [ uri] else {
1652
+ guard let languageService = workspace. documentService ( for : uri) else {
1649
1653
return nil
1650
1654
}
1651
1655
@@ -1656,6 +1660,21 @@ extension SourceKitLSPServer {
1656
1660
return try await languageService. executeCommand ( executeCommand)
1657
1661
}
1658
1662
1663
+ func getReferenceDocument( _ req: GetReferenceDocumentRequest ) async throws -> GetReferenceDocumentResponse {
1664
+ let referenceDocumentURL = try ReferenceDocumentURL ( from: req. uri)
1665
+ let primaryFileURI = referenceDocumentURL. primaryFile
1666
+
1667
+ guard let workspace = await workspaceForDocument ( uri: primaryFileURI) else {
1668
+ throw ResponseError . workspaceNotOpen ( primaryFileURI)
1669
+ }
1670
+
1671
+ guard let languageService = workspace. documentService ( for: primaryFileURI) else {
1672
+ throw ResponseError . unknown ( " No Language Service for URI: \( primaryFileURI) " )
1673
+ }
1674
+
1675
+ return try await languageService. getReferenceDocument ( req)
1676
+ }
1677
+
1659
1678
func codeAction(
1660
1679
_ req: CodeActionRequest ,
1661
1680
workspace: Workspace ,
0 commit comments