Skip to content

Commit 4fba69c

Browse files
committed
Extend copied file mapping to all LSP requests returning locations
This addresses issue #2276 by ensuring that all LSP requests that return source file locations map copied header files back to their original locations, not just jump-to-definition. Previously, only the definition request applied this mapping. Now, the following requests also adjust locations for copied files: - textDocument/references - textDocument/implementation - workspace/symbol - callHierarchy/prepare - callHierarchy/incomingCalls - callHierarchy/outgoingCalls - typeHierarchy/prepare - typeHierarchy/supertypes - typeHierarchy/subtypes This provides consistent navigation behavior, ensuring users are always taken to the original source files instead of build artifacts when possible.
1 parent d449cb7 commit 4fba69c

File tree

3 files changed

+328
-55
lines changed

3 files changed

+328
-55
lines changed

Sources/BuildServerIntegration/BuildServerManager.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,47 @@ package actor BuildServerManager: QueueBasedMessageHandler {
962962
return locations.map { locationAdjustedForCopiedFiles($0) }
963963
}
964964

965+
package func workspaceEditAdjustedForCopiedFiles(_ workspaceEdit: WorkspaceEdit?) async -> WorkspaceEdit? {
966+
guard var edit = workspaceEdit else {
967+
return nil
968+
}
969+
if let changes = edit.changes {
970+
var newChanges: [DocumentURI: [TextEdit]] = [:]
971+
for (uri, edits) in changes {
972+
let newUri = self.locationAdjustedForCopiedFiles(
973+
Location(uri: uri, range: Position(line: 0, utf16index: 0)..<Position(line: 0, utf16index: 0))
974+
).uri
975+
newChanges[newUri, default: []].append(contentsOf: edits)
976+
}
977+
edit.changes = newChanges
978+
}
979+
// documentChanges are not handled yet.
980+
return edit
981+
}
982+
983+
package func locationsOrLocationLinksAdjustedForCopiedFiles(_ response: LocationsOrLocationLinksResponse?) async -> LocationsOrLocationLinksResponse? {
984+
guard let response = response else {
985+
return nil
986+
}
987+
switch response {
988+
case .locations(let locations):
989+
let remappedLocations = await self.locationsAdjustedForCopiedFiles(locations)
990+
return .locations(remappedLocations)
991+
case .locationLinks(let locationLinks):
992+
var remappedLinks: [LocationLink] = []
993+
for link in locationLinks {
994+
let newUri = self.locationAdjustedForCopiedFiles(Location(uri: link.targetUri, range: link.targetRange)).uri
995+
remappedLinks.append(LocationLink(
996+
originSelectionRange: link.originSelectionRange,
997+
targetUri: newUri,
998+
targetRange: link.targetRange,
999+
targetSelectionRange: link.targetSelectionRange
1000+
))
1001+
}
1002+
return .locationLinks(remappedLinks)
1003+
}
1004+
}
1005+
9651006
@discardableResult
9661007
package func scheduleRecomputeCopyFileMap() -> Task<Void, Never> {
9671008
let task = Task { [previousUpdateTask = copiedFileMapUpdateTask] in

Sources/ClangLanguageService/ClangLanguageService.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,11 @@ extension ClangLanguageService {
481481
}
482482

483483
package func declaration(_ req: DeclarationRequest) async throws -> LocationsOrLocationLinksResponse? {
484-
return try await forwardRequestToClangd(req)
484+
let result = try await forwardRequestToClangd(req)
485+
guard let workspace = self.workspace.value else {
486+
return result
487+
}
488+
return await workspace.buildServerManager.locationsOrLocationLinksAdjustedForCopiedFiles(result)
485489
}
486490

487491
package func completion(_ req: CompletionRequest) async throws -> CompletionList {
@@ -618,7 +622,11 @@ extension ClangLanguageService {
618622
}
619623

620624
package func indexedRename(_ request: IndexedRenameRequest) async throws -> WorkspaceEdit? {
621-
return try await forwardRequestToClangd(request)
625+
let workspaceEdit = try await forwardRequestToClangd(request)
626+
guard let workspace = self.workspace.value else {
627+
return workspaceEdit
628+
}
629+
return await workspace.buildServerManager.workspaceEditAdjustedForCopiedFiles(workspaceEdit)
622630
}
623631

624632
// MARK: - Other
@@ -634,7 +642,12 @@ extension ClangLanguageService {
634642
position: renameRequest.position
635643
)
636644
let symbolDetail = try await forwardRequestToClangd(symbolInfoRequest).only
637-
return (try await edits ?? WorkspaceEdit(), symbolDetail?.usr)
645+
let workspaceEdit = try await edits
646+
guard let workspace = self.workspace.value else {
647+
return (workspaceEdit ?? WorkspaceEdit(), symbolDetail?.usr)
648+
}
649+
let remappedEdit = await workspace.buildServerManager.workspaceEditAdjustedForCopiedFiles(workspaceEdit)
650+
return (remappedEdit ?? WorkspaceEdit(), symbolDetail?.usr)
638651
}
639652

640653
package func syntacticDocumentTests(

0 commit comments

Comments
 (0)