Skip to content

Ensure rename tests don’t fail if running with a sourcekitd that doesn’t support rename yet #1015

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
Jan 9, 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
6 changes: 4 additions & 2 deletions Sources/SourceKitLSP/Rename.swift
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,10 @@ extension SwiftLanguageServer {
in: snapshot,
includeNonEditableBaseNames: true
)
guard let name = response.name,
let range = response.relatedIdentifiers.first(where: { $0.range.contains(request.position) })?.range
guard let name = response.name else {
throw ResponseError.unknown("Running sourcekit-lsp with a version of sourcekitd that does not support rename")
}
guard let range = response.relatedIdentifiers.first(where: { $0.range.contains(request.position) })?.range
else {
return nil
}
Expand Down
66 changes: 51 additions & 15 deletions Tests/SourceKitLSPTests/RenameTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,22 @@ private func assertSingleFileRename(
let uri = DocumentURI.for(.swift, testName: testName)
let positions = testClient.openDocument(markedSource, uri: uri)
for marker in positions.allMarkers {
let response = try await testClient.send(
RenameRequest(
textDocument: TextDocumentIdentifier(uri),
position: positions[marker],
newName: newName
let response: WorkspaceEdit?
do {
response = try await testClient.send(
RenameRequest(
textDocument: TextDocumentIdentifier(uri),
position: positions[marker],
newName: newName
)
)
)
} catch let error as ResponseError {
if error.message == "Running sourcekit-lsp with a version of sourcekitd that does not support rename" {
throw XCTSkip(error.message)
} else {
throw error
}
}
let edits = try XCTUnwrap(response?.changes?[uri], "while performing rename at \(marker)", file: file, line: line)
let source = extractMarkers(markedSource).textWithoutMarkers
let renamed = apply(edits: edits, to: source)
Expand Down Expand Up @@ -127,9 +136,18 @@ private func assertMultiFileRename(
ws.testClient.send(DidCloseTextDocumentNotification(textDocument: TextDocumentIdentifier(uri)))
}
for marker in markers {
let response = try await ws.testClient.send(
RenameRequest(textDocument: TextDocumentIdentifier(uri), position: positions[marker], newName: newName)
)
let response: WorkspaceEdit?
do {
response = try await ws.testClient.send(
RenameRequest(textDocument: TextDocumentIdentifier(uri), position: positions[marker], newName: newName)
)
} catch let error as ResponseError {
if error.message == "Running sourcekit-lsp with a version of sourcekitd that does not support rename" {
throw XCTSkip(error.message)
} else {
throw error
}
}
let changes = try XCTUnwrap(response?.changes)
try assertRenamedSourceMatches(
originalFiles: files,
Expand Down Expand Up @@ -696,9 +714,18 @@ final class RenameTests: XCTestCase {
""",
uri: uri
)
let response = try await testClient.send(
PrepareRenameRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"])
)
let response: PrepareRenameResponse?
do {
response = try await testClient.send(
PrepareRenameRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"])
)
} catch let error as ResponseError {
if error.message == "Running sourcekit-lsp with a version of sourcekitd that does not support rename" {
throw XCTSkip(error.message)
} else {
throw error
}
}
let range = try XCTUnwrap(response?.range)
let placeholder = try XCTUnwrap(response?.placeholder)
XCTAssertEqual(range, positions["1️⃣"]..<positions["2️⃣"])
Expand All @@ -715,9 +742,18 @@ final class RenameTests: XCTestCase {
""",
uri: uri
)
let response = try await testClient.send(
PrepareRenameRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"])
)
let response: PrepareRenameResponse?
do {
response = try await testClient.send(
PrepareRenameRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"])
)
} catch let error as ResponseError {
if error.message == "Running sourcekit-lsp with a version of sourcekitd that does not support rename" {
throw XCTSkip(error.message)
} else {
throw error
}
}
let range = try XCTUnwrap(response?.range)
let placeholder = try XCTUnwrap(response?.placeholder)
XCTAssertEqual(range, positions["1️⃣"]..<positions["2️⃣"])
Expand Down