Skip to content

Don’t report no-op rename edits #1230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Sources/SourceKitLSP/Rename.swift
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,7 @@ extension SourceKitLSPServer {
newName: newName
)
}
edits = edits.filter { !$0.isNoOp(in: snapshot) }
return (uri, edits)
}.compactMap { $0 }
for (uri, editsForUri) in urisAndEdits {
Expand Down Expand Up @@ -1116,7 +1117,11 @@ extension SwiftLanguageService {
)
}
}
edits = edits.filter { !$0.isNoOp(in: snapshot) }

if edits.isEmpty {
return (edits: WorkspaceEdit(changes: [:]), usr: usr)
}
return (edits: WorkspaceEdit(changes: [snapshot.uri: edits]), usr: usr)
}

Expand Down Expand Up @@ -1485,3 +1490,13 @@ fileprivate extension RelatedIdentifiersResponse {
}
}
}

fileprivate extension TextEdit {
/// Returns `true` the replaced text is the same as the new text
func isNoOp(in snapshot: DocumentSnapshot) -> Bool {
if snapshot.text[snapshot.indexRange(of: range)] == newText {
return true
}
return false
}
}
22 changes: 22 additions & 0 deletions Tests/SourceKitLSPTests/RenameTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1236,4 +1236,26 @@ final class RenameTests: XCTestCase {
"""
)
}

func testRenameDoesNotReportEditsIfNoActualChangeIsMade() async throws {
try await SkipUnless.sourcekitdSupportsRename()
let project = try await SwiftPMTestProject(
files: [
"FileA.swift": """
func 1️⃣foo(x: Int) {}
""",
"FileB.swift": """
func test() {
foo(x: 1)
}
""",
],
build: true
)
let (uri, positions) = try project.openDocument("FileA.swift")
let result = try await project.testClient.send(
RenameRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"], newName: "foo(x:)")
)
XCTAssertEqual(result?.changes, [:])
}
}