@@ -357,7 +357,7 @@ package actor SourceKitLSPServer {
357357
358358 // This should be created as soon as we receive an open call, even if the document
359359 // isn't yet ready.
360- guard let languageService = workspace. documentService. value [ doc] else {
360+ guard let languageService = workspace. documentService ( for : doc) else {
361361 return
362362 }
363363
@@ -377,7 +377,7 @@ package actor SourceKitLSPServer {
377377 guard let workspace = await self . workspaceForDocument ( uri: request. textDocument. uri) else {
378378 throw ResponseError . workspaceNotOpen ( request. textDocument. uri)
379379 }
380- guard let languageService = workspace. documentService. value [ doc] else {
380+ guard let languageService = workspace. documentService ( for : doc) else {
381381 throw ResponseError . unknown ( " No language service for ' \( request. textDocument. uri) ' found " )
382382 }
383383 return try await requestHandler ( request, workspace, languageService)
@@ -400,7 +400,7 @@ package actor SourceKitLSPServer {
400400 guard let workspace = await self . workspaceForDocument ( uri: documentUri) else {
401401 continue
402402 }
403- guard workspace. documentService. value [ documentUri] === languageService else {
403+ guard workspace. documentService ( for : documentUri) === languageService else {
404404 continue
405405 }
406406 guard let snapshot = try ? self . documentManager. latestSnapshot ( documentUri) else {
@@ -518,7 +518,7 @@ package actor SourceKitLSPServer {
518518 _ language: Language ,
519519 in workspace: Workspace
520520 ) async -> LanguageService ? {
521- if let service = workspace. documentService. value [ uri] {
521+ if let service = workspace. documentService ( for : uri) {
522522 return service
523523 }
524524
@@ -536,17 +536,7 @@ package actor SourceKitLSPServer {
536536 """
537537 )
538538
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)
550540 }
551541}
552542
@@ -733,6 +723,8 @@ extension SourceKitLSPServer: MessageHandler {
733723 await request. reply { try await executeCommand ( request. params) }
734724 case let request as RequestAndReply < FoldingRangeRequest > :
735725 await self . handleRequest ( for: request, requestHandler: self . foldingRange)
726+ case let request as RequestAndReply < GetReferenceDocumentRequest > :
727+ await request. reply { try await getReferenceDocument ( request. params) }
736728 case let request as RequestAndReply < HoverRequest > :
737729 await self . handleRequest ( for: request, requestHandler: self . hover)
738730 case let request as RequestAndReply < ImplementationRequest > :
@@ -804,7 +796,7 @@ extension SourceKitLSPServer: BuildSystemDelegate {
804796 continue
805797 }
806798
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 {
808800 continue
809801 }
810802
@@ -828,7 +820,7 @@ extension SourceKitLSPServer: BuildSystemDelegate {
828820 }
829821 for uri in self . affectedOpenDocumentsForChangeSet ( changedFilesForWorkspace, self . documentManager) {
830822 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) {
832824 await service. documentDependenciesUpdated ( uri)
833825 }
834826 }
@@ -964,6 +956,8 @@ extension SourceKitLSPServer {
964956 //
965957 // The below is a workaround for the vscode-swift extension since it cannot set client capabilities.
966958 // It passes "workspace/peekDocuments" through the `initializationOptions`.
959+ //
960+ // Similarly, for "workspace/getReferenceDocument".
967961 var clientCapabilities = req. capabilities
968962 if case . dictionary( let initializationOptions) = req. initializationOptions {
969963 if let peekDocuments = initializationOptions [ " workspace/peekDocuments " ] {
@@ -975,6 +969,15 @@ extension SourceKitLSPServer {
975969 }
976970 }
977971
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+
978981 // The client announces what CodeLenses it supports, and the LSP will only return
979982 // ones found in the supportedCommands dictionary.
980983 if let codeLens = initializationOptions [ " textDocument/codeLens " ] ,
@@ -1143,6 +1146,7 @@ extension SourceKitLSPServer {
11431146 " workspace/tests " : . dictionary( [ " version " : . int( 2 ) ] ) ,
11441147 " textDocument/tests " : . dictionary( [ " version " : . int( 2 ) ] ) ,
11451148 " workspace/triggerReindex " : . dictionary( [ " version " : . int( 1 ) ] ) ,
1149+ " workspace/getReferenceDocument " : . dictionary( [ " version " : . int( 1 ) ] ) ,
11461150 ] )
11471151 )
11481152 }
@@ -1342,7 +1346,7 @@ extension SourceKitLSPServer {
13421346 )
13431347 return
13441348 }
1345- await workspace. documentService. value [ uri] ? . reopenDocument ( notification)
1349+ await workspace. documentService ( for : uri) ? . reopenDocument ( notification)
13461350 }
13471351
13481352 func closeDocument( _ notification: DidCloseTextDocumentNotification , workspace: Workspace ) async {
@@ -1356,7 +1360,7 @@ extension SourceKitLSPServer {
13561360
13571361 await workspace. buildSystemManager. unregisterForChangeNotifications ( for: uri)
13581362
1359- await workspace. documentService. value [ uri] ? . closeDocument ( notification)
1363+ await workspace. documentService ( for : uri) ? . closeDocument ( notification)
13601364 }
13611365
13621366 func changeDocument( _ notification: DidChangeTextDocumentNotification ) async {
@@ -1382,7 +1386,7 @@ extension SourceKitLSPServer {
13821386 // Already logged failure
13831387 return
13841388 }
1385- await workspace. documentService. value [ uri] ? . changeDocument (
1389+ await workspace. documentService ( for : uri) ? . changeDocument (
13861390 notification,
13871391 preEditSnapshot: preEditSnapshot,
13881392 postEditSnapshot: postEditSnapshot,
@@ -1645,7 +1649,7 @@ extension SourceKitLSPServer {
16451649 guard let workspace = await workspaceForDocument ( uri: uri) else {
16461650 throw ResponseError . workspaceNotOpen ( uri)
16471651 }
1648- guard let languageService = workspace. documentService. value [ uri] else {
1652+ guard let languageService = workspace. documentService ( for : uri) else {
16491653 return nil
16501654 }
16511655
@@ -1656,6 +1660,21 @@ extension SourceKitLSPServer {
16561660 return try await languageService. executeCommand ( executeCommand)
16571661 }
16581662
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+
16591678 func codeAction(
16601679 _ req: CodeActionRequest ,
16611680 workspace: Workspace ,
0 commit comments