Skip to content

Commit 4bdb2e3

Browse files
committed
part 1
1 parent 70d900e commit 4bdb2e3

File tree

2 files changed

+41
-60
lines changed

2 files changed

+41
-60
lines changed

Sources/BuildServerIntegration/BuildServerManager.swift

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

965+
/// Check if the URI has been copied during the preparation phase. If so, return the original source URI.
966+
private func uriAdjustedForCopiedFiles(_ uri: DocumentURI) -> DocumentURI {
967+
return cachedCopiedFileMap[uri] ?? uri
968+
}
969+
970+
// ahopen: I don’t think any of these functions need to be async.
965971
package func workspaceEditAdjustedForCopiedFiles(_ workspaceEdit: WorkspaceEdit?) async -> WorkspaceEdit? {
966972
guard var edit = workspaceEdit else {
967973
return nil
968974
}
969975
if let changes = edit.changes {
970976
var newChanges: [DocumentURI: [TextEdit]] = [:]
971977
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)
978+
let newUri = self.uriAdjustedForCopiedFiles(uri)
979+
newChanges[newUri, default: []] += edits
976980
}
977981
edit.changes = newChanges
978982
}
@@ -981,32 +985,17 @@ package actor BuildServerManager: QueueBasedMessageHandler {
981985
for change in documentChanges {
982986
switch change {
983987
case .textDocumentEdit(var textEdit):
984-
let newUri = self.locationAdjustedForCopiedFiles(
985-
Location(uri: textEdit.textDocument.uri, range: Position(line: 0, utf16index: 0)..<Position(line: 0, utf16index: 0))
986-
).uri
987-
textEdit.textDocument.uri = newUri
988+
textEdit.textDocument.uri = self.uriAdjustedForCopiedFiles(textEdit.textDocument.uri)
988989
newDocumentChanges.append(.textDocumentEdit(textEdit))
989990
case .createFile(var create):
990-
let newUri = self.locationAdjustedForCopiedFiles(
991-
Location(uri: create.uri, range: Position(line: 0, utf16index: 0)..<Position(line: 0, utf16index: 0))
992-
).uri
993-
create.uri = newUri
991+
create.uri = self.uriAdjustedForCopiedFiles(create.uri)
994992
newDocumentChanges.append(.createFile(create))
995993
case .renameFile(var rename):
996-
let newOldUri = self.locationAdjustedForCopiedFiles(
997-
Location(uri: rename.oldUri, range: Position(line: 0, utf16index: 0)..<Position(line: 0, utf16index: 0))
998-
).uri
999-
let newNewUri = self.locationAdjustedForCopiedFiles(
1000-
Location(uri: rename.newUri, range: Position(line: 0, utf16index: 0)..<Position(line: 0, utf16index: 0))
1001-
).uri
1002-
rename.oldUri = newOldUri
1003-
rename.newUri = newNewUri
994+
rename.oldUri = self.uriAdjustedForCopiedFiles(rename.oldUri)
995+
rename.newUri = self.uriAdjustedForCopiedFiles(rename.newUri)
1004996
newDocumentChanges.append(.renameFile(rename))
1005997
case .deleteFile(var delete):
1006-
let newUri = self.locationAdjustedForCopiedFiles(
1007-
Location(uri: delete.uri, range: Position(line: 0, utf16index: 0)..<Position(line: 0, utf16index: 0))
1008-
).uri
1009-
delete.uri = newUri
998+
delete.uri = self.uriAdjustedForCopiedFiles(delete.uri)
1010999
newDocumentChanges.append(.deleteFile(delete))
10111000
}
10121001
}
@@ -1021,33 +1010,35 @@ package actor BuildServerManager: QueueBasedMessageHandler {
10211010
}
10221011
switch response {
10231012
case .locations(let locations):
1024-
let remappedLocations = await self.locationsAdjustedForCopiedFiles(locations)
1013+
let remappedLocations = self.locationsAdjustedForCopiedFiles(locations)
10251014
return .locations(remappedLocations)
10261015
case .locationLinks(let locationLinks):
10271016
var remappedLinks: [LocationLink] = []
10281017
for link in locationLinks {
1029-
let newUri = self.locationAdjustedForCopiedFiles(Location(uri: link.targetUri, range: link.targetRange)).uri
1018+
let adjustedTargetLocation = self.locationAdjustedForCopiedFiles(Location(uri: link.targetUri, range: link.targetRange))
1019+
let adjustedTargetSelectionLocation = self.locationAdjustedForCopiedFiles(Location(uri: link.targetUri, range: link.targetSelectionRange))
10301020
remappedLinks.append(LocationLink(
10311021
originSelectionRange: link.originSelectionRange,
1032-
targetUri: newUri,
1033-
targetRange: link.targetRange,
1034-
targetSelectionRange: link.targetSelectionRange
1022+
targetUri: adjustedTargetLocation.uri,
1023+
targetRange: adjustedTargetLocation.range,
1024+
targetSelectionRange: adjustedTargetSelectionLocation.range
10351025
))
10361026
}
10371027
return .locationLinks(remappedLinks)
10381028
}
10391029
}
10401030

10411031
package func typeHierarchyItemAdjustedForCopiedFiles(_ item: TypeHierarchyItem) async -> TypeHierarchyItem {
1042-
let adjustedLocation = await self.locationAdjustedForCopiedFiles(Location(uri: item.uri, range: item.range))
1032+
let adjustedLocation = self.locationAdjustedForCopiedFiles(Location(uri: item.uri, range: item.range))
1033+
let adjustedSelectionLocation = self.locationAdjustedForCopiedFiles(Location(uri: item.uri, range: item.selectionRange))
10431034
return TypeHierarchyItem(
10441035
name: item.name,
10451036
kind: item.kind,
10461037
tags: item.tags,
10471038
detail: item.detail,
10481039
uri: adjustedLocation.uri,
10491040
range: adjustedLocation.range,
1050-
selectionRange: item.selectionRange,
1041+
selectionRange: adjustedSelectionLocation.range,
10511042
data: item.data
10521043
)
10531044
}

Sources/SourceKitLSP/SourceKitLSPServer.swift

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2512,30 +2512,20 @@ extension SourceKitLSPServer {
25122512
let conformances = index.occurrences(relatedToUSR: symbol.usr, roles: .baseOf)
25132513
if conformances.isEmpty {
25142514
name = symbol.name
2515-
} else {
2516-
name = "\(symbol.name): \(conformances.map(\.symbol.name).sorted().joined(separator: ", "))"
2517-
}
2518-
// Add the file name and line to the detail string
2519-
if let url = remappedLocation.uri.fileURL,
2520-
let basename = (try? AbsolutePath(validating: url.filePath))?.basename
2521-
{
2522-
detail = "Extension at \(basename):\(remappedLocation.range.lowerBound.line + 1)"
2523-
} else if !definition.location.moduleName.isEmpty {
2524-
detail = "Extension in \(definition.location.moduleName)"
2525-
} else {
2526-
detail = "Extension"
2527-
}
2528-
// Add the file name and line to the detail string
2529-
if let url = location.uri.fileURL,
2530-
let basename = (try? AbsolutePath(validating: url.filePath))?.basename
2531-
{
2532-
detail = "Extension at \(basename):\(location.range.lowerBound.line + 1)"
2533-
} else if let moduleName = moduleName {
2534-
detail = "Extension in \(moduleName)"
2535-
} else {
2536-
detail = "Extension"
2537-
}
2538-
default:
2515+
} else {
2516+
name = "\(symbol.name): \(conformances.map(\.symbol.name).sorted().joined(separator: ", "))"
2517+
}
2518+
// Add the file name and line to the detail string
2519+
if let url = location.uri.fileURL,
2520+
let basename = (try? AbsolutePath(validating: url.filePath))?.basename
2521+
{
2522+
detail = "Extension at \(basename):\(location.range.lowerBound.line + 1)"
2523+
} else if !definition.location.moduleName.isEmpty {
2524+
detail = "Extension in \(definition.location.moduleName)"
2525+
} else {
2526+
detail = "Extension"
2527+
}
2528+
default:
25392529
name = index.fullyQualifiedName(of: definition)
25402530
detail = moduleName
25412531
}
@@ -2754,10 +2744,10 @@ extension SourceKitLSPServer {
27542744
name = "\(symbol.name): \(conformances.map(\.symbol.name).sorted().joined(separator: ", "))"
27552745
}
27562746
// Add the file name and line to the detail string
2757-
if let url = adjustedLocation.uri.fileURL,
2747+
if let url = remappedLocation.uri.fileURL,
27582748
let basename = (try? AbsolutePath(validating: url.filePath))?.basename
27592749
{
2760-
detail = "Extension at \(basename):\(adjustedLocation.range.lowerBound.line + 1)"
2750+
detail = "Extension at \(basename):\(remappedLocation.range.lowerBound.line + 1)"
27612751
} else if !definition.location.moduleName.isEmpty {
27622752
detail = "Extension in \(definition.location.moduleName)"
27632753
} else {
@@ -2839,7 +2829,7 @@ extension SourceKitLSPServer {
28392829
guard let originalLocation = location else {
28402830
continue
28412831
}
2842-
let adjustedLocation = await workspace.buildServerManager.locationAdjustedForCopiedFiles(originalLocation)
2832+
let remappedLocation = await workspace.buildServerManager.locationAdjustedForCopiedFiles(originalLocation)
28432833

28442834
// Create a new TypeHierarchyItem with the original location, then adjust for copied files
28452835
let name: String
@@ -2855,10 +2845,10 @@ extension SourceKitLSPServer {
28552845
name = "\(symbol.name): \(conformances.map(\.symbol.name).sorted().joined(separator: ", "))"
28562846
}
28572847
// Add the file name and line to the detail string
2858-
if let url = adjustedLocation.uri.fileURL,
2848+
if let url = remappedLocation.uri.fileURL,
28592849
let basename = (try? AbsolutePath(validating: url.filePath))?.basename
28602850
{
2861-
detail = "Extension at \(basename):\(adjustedLocation.range.lowerBound.line + 1)"
2851+
detail = "Extension at \(basename):\(remappedLocation.range.lowerBound.line + 1)"
28622852
} else if !definition.location.moduleName.isEmpty {
28632853
detail = "Extension in \(definition.location.moduleName)"
28642854
} else {

0 commit comments

Comments
 (0)