Skip to content

Commit 624d4b6

Browse files
authored
Merge pull request #1230 from ahoppen/no-noop-rename-edits
Don’t report no-op rename edits
2 parents 22e3eea + 30459c7 commit 624d4b6

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

Sources/SourceKitLSP/Rename.swift

+15
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 {
@@ -1116,7 +1117,11 @@ extension SwiftLanguageService {
11161117
)
11171118
}
11181119
}
1120+
edits = edits.filter { !$0.isNoOp(in: snapshot) }
11191121

1122+
if edits.isEmpty {
1123+
return (edits: WorkspaceEdit(changes: [:]), usr: usr)
1124+
}
11201125
return (edits: WorkspaceEdit(changes: [snapshot.uri: edits]), usr: usr)
11211126
}
11221127

@@ -1485,3 +1490,13 @@ fileprivate extension RelatedIdentifiersResponse {
14851490
}
14861491
}
14871492
}
1493+
1494+
fileprivate extension TextEdit {
1495+
/// Returns `true` the replaced text is the same as the new text
1496+
func isNoOp(in snapshot: DocumentSnapshot) -> Bool {
1497+
if snapshot.text[snapshot.indexRange(of: range)] == newText {
1498+
return true
1499+
}
1500+
return false
1501+
}
1502+
}

Tests/SourceKitLSPTests/RenameTests.swift

+22
Original file line numberDiff line numberDiff line change
@@ -1236,4 +1236,26 @@ final class RenameTests: XCTestCase {
12361236
"""
12371237
)
12381238
}
1239+
1240+
func testRenameDoesNotReportEditsIfNoActualChangeIsMade() async throws {
1241+
try await SkipUnless.sourcekitdSupportsRename()
1242+
let project = try await SwiftPMTestProject(
1243+
files: [
1244+
"FileA.swift": """
1245+
func 1️⃣foo(x: Int) {}
1246+
""",
1247+
"FileB.swift": """
1248+
func test() {
1249+
foo(x: 1)
1250+
}
1251+
""",
1252+
],
1253+
build: true
1254+
)
1255+
let (uri, positions) = try project.openDocument("FileA.swift")
1256+
let result = try await project.testClient.send(
1257+
RenameRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"], newName: "foo(x:)")
1258+
)
1259+
XCTAssertEqual(result?.changes, [:])
1260+
}
12391261
}

0 commit comments

Comments
 (0)