Skip to content

Commit 9b3a9da

Browse files
authored
Merge pull request #1164 from ahoppen/rename-from-end-of-identifier
Support rename initiated from the end of an identifier
2 parents d1d312c + 5c549ce commit 9b3a9da

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

Sources/SourceKitLSP/Rename.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,12 +1037,13 @@ extension SwiftLanguageService {
10371037
at position: Position,
10381038
in snapshot: DocumentSnapshot
10391039
) async -> (position: Position?, usr: String?, functionLikeRange: Range<Position>?) {
1040+
let startOfIdentifierPosition = await adjustPositionToStartOfIdentifier(position, in: snapshot)
10401041
let symbolInfo = try? await self.symbolInfo(
1041-
SymbolInfoRequest(textDocument: TextDocumentIdentifier(snapshot.uri), position: position)
1042+
SymbolInfoRequest(textDocument: TextDocumentIdentifier(snapshot.uri), position: startOfIdentifierPosition)
10421043
)
10431044

1044-
guard let functionLikeRange = await findFunctionLikeRange(of: position, in: snapshot) else {
1045-
return (position, symbolInfo?.only?.usr, nil)
1045+
guard let functionLikeRange = await findFunctionLikeRange(of: startOfIdentifierPosition, in: snapshot) else {
1046+
return (startOfIdentifierPosition, symbolInfo?.only?.usr, nil)
10461047
}
10471048
if let onlySymbol = symbolInfo?.only, onlySymbol.kind == .constructor {
10481049
// We have a rename like `MyStruct(x: 1)`, invoked from `x`.

Tests/SourceKitLSPTests/RenameAssertions.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ func assertSingleFileRename(
5151
return
5252
}
5353
for marker in positions.allMarkers {
54+
let position = positions[marker]
5455
let prepareRenameResponse = try await testClient.send(
55-
PrepareRenameRequest(textDocument: TextDocumentIdentifier(uri), position: positions[marker])
56+
PrepareRenameRequest(textDocument: TextDocumentIdentifier(uri), position: position)
5657
)
5758
if let prepareRenameResponse {
5859
XCTAssertEqual(
@@ -62,9 +63,11 @@ func assertSingleFileRename(
6263
file: file,
6364
line: line
6465
)
66+
// VS Code considers the upper bound of a range as part of the identifier so both `contains` and equality in
67+
// `upperBound` are fine.
6568
XCTAssert(
66-
prepareRenameResponse.range.contains(positions[marker]),
67-
"Prepare rename range \(prepareRenameResponse.range) does not contain rename position \(positions[marker])",
69+
prepareRenameResponse.range.contains(position) || prepareRenameResponse.range.upperBound == position,
70+
"Prepare rename range \(prepareRenameResponse.range) does not contain rename position \(position)",
6871
file: file,
6972
line: line
7073
)

Tests/SourceKitLSPTests/RenameTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ final class RenameTests: XCTestCase {
1818
func testRenameVariableBaseName() async throws {
1919
try await assertSingleFileRename(
2020
"""
21-
let 1️⃣foo = 1
22-
print(2️⃣foo)
21+
let 1️⃣foo2️⃣ = 1
22+
print(3️⃣foo4️⃣)
2323
""",
2424
newName: "bar",
2525
expectedPrepareRenamePlaceholder: "foo",

0 commit comments

Comments
 (0)