Skip to content

Commit fd18714

Browse files
committed
Don’t report no-op rename edits
When renaming `func test(foo: Int) {}` to `test2(foo:)`, rename used to report an edit from `foo` to `foo`, which clutters the refactor preview view. We shouldn’t report edits if no text is actually changed. rdar://127291815
1 parent e71aa5d commit fd18714

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

Sources/SourceKitLSP/Rename.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,7 @@ extension SourceKitLSPServer {
817817
newName: newName
818818
)
819819
}
820+
edits = edits.filter { !$0.isNoOp(in: snapshot) }
820821
return (uri, edits)
821822
}.compactMap { $0 }
822823
for (uri, editsForUri) in urisAndEdits {
@@ -1089,7 +1090,11 @@ extension SwiftLanguageService {
10891090
)
10901091
}
10911092
}
1093+
edits = edits.filter { !$0.isNoOp(in: snapshot) }
10921094

1095+
if edits.isEmpty {
1096+
return (edits: WorkspaceEdit(changes: [:]), usr: usr)
1097+
}
10931098
return (edits: WorkspaceEdit(changes: [snapshot.uri: edits]), usr: usr)
10941099
}
10951100

@@ -1451,3 +1456,13 @@ fileprivate extension RelatedIdentifiersResponse {
14511456
}
14521457
}
14531458
}
1459+
1460+
fileprivate extension TextEdit {
1461+
/// Returns `true` the replaced text is the same as the new text
1462+
func isNoOp(in snapshot: DocumentSnapshot) -> Bool {
1463+
if snapshot.text[snapshot.indexRange(of: range)] == newText {
1464+
return true
1465+
}
1466+
return false
1467+
}
1468+
}

Tests/SourceKitLSPTests/RenameTests.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,4 +1181,25 @@ final class RenameTests: XCTestCase {
11811181
)
11821182
)
11831183
}
1184+
1185+
func testRenameDoesNotReportEditsIfNoActualChangeIsMade() async throws {
1186+
let project = try await SwiftPMTestProject(
1187+
files: [
1188+
"FileA.swift": """
1189+
func 1️⃣foo(x: Int) {}
1190+
""",
1191+
"FileB.swift": """
1192+
func test() {
1193+
foo(x: 1)
1194+
}
1195+
""",
1196+
],
1197+
build: true
1198+
)
1199+
let (uri, positions) = try project.openDocument("FileA.swift")
1200+
let result = try await project.testClient.send(
1201+
RenameRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"], newName: "foo(x:)")
1202+
)
1203+
XCTAssertEqual(result?.changes, [:])
1204+
}
11841205
}

0 commit comments

Comments
 (0)