Skip to content

Commit b8379d9

Browse files
committed
Allow rename when the new name is missing the ) or if it has text after )
1 parent df79c19 commit b8379d9

File tree

2 files changed

+18
-43
lines changed

2 files changed

+18
-43
lines changed

Sources/SourceKitLSP/Rename.swift

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,6 @@ import SourceKitD
2525
/// - `foo(_:b:)`
2626
/// - `foo` if no argument labels are specified, eg. for a variable.
2727
fileprivate struct CompoundDeclName {
28-
enum CompoundDeclNameParsingError: Error, CustomStringConvertible {
29-
case missingClosingParenthesis
30-
case closingParenthesisNotAtEnd
31-
32-
var description: String {
33-
switch self {
34-
case .missingClosingParenthesis: "Name contains '(' but no matching ')'"
35-
case .closingParenthesisNotAtEnd: "Additional text after ')'"
36-
}
37-
}
38-
}
39-
4028
/// The parameter of a compound decl name, which can either be the parameter's name or `_` to indicate that the
4129
/// parameter is unnamed.
4230
enum Parameter: Equatable {
@@ -62,20 +50,15 @@ fileprivate struct CompoundDeclName {
6250
let parameters: [Parameter]
6351

6452
/// Parse a compound decl name into its base names and parameters.
65-
init(_ compoundDeclName: String) throws {
53+
init(_ compoundDeclName: String) {
6654
guard let openParen = compoundDeclName.firstIndex(of: "(") else {
6755
// We don't have a compound name. Everything is the base name
6856
self.baseName = compoundDeclName
6957
self.parameters = []
7058
return
7159
}
7260
self.baseName = String(compoundDeclName[..<openParen])
73-
guard let closeParen = compoundDeclName.firstIndex(of: ")") else {
74-
throw CompoundDeclNameParsingError.missingClosingParenthesis
75-
}
76-
guard compoundDeclName.index(after: closeParen) == compoundDeclName.endIndex else {
77-
throw CompoundDeclNameParsingError.closingParenthesisNotAtEnd
78-
}
61+
let closeParen = compoundDeclName.firstIndex(of: ")") ?? compoundDeclName.endIndex
7962
let parametersText = compoundDeclName[compoundDeclName.index(after: openParen)..<closeParen]
8063
// Split by `:` to get the parameter names. Drop the last element so that we don't have a trailing empty element
8164
// after the last `:`.
@@ -549,8 +532,8 @@ extension SwiftLanguageServer {
549532
oldName: oldNameString,
550533
in: snapshot
551534
)
552-
let oldName = try CompoundDeclName(oldNameString)
553-
let newName = try CompoundDeclName(newNameString)
535+
let oldName = CompoundDeclName(oldNameString)
536+
let newName = CompoundDeclName(newNameString)
554537

555538
try Task.checkCancellation()
556539

Tests/SourceKitLSPTests/RenameTests.swift

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -366,40 +366,32 @@ final class RenameTests: XCTestCase {
366366
)
367367
}
368368

369-
func testErrorIfNewNameDoesntContainClosingParenthesis() async throws {
370-
let testClient = try await TestSourceKitLSPClient()
371-
let uri = DocumentURI.for(.swift)
372-
let positions = testClient.openDocument(
369+
func testNewNameDoesntContainClosingParenthesis() async throws {
370+
try await assertSingleFileRename(
373371
"""
374372
func 1️⃣foo(a: Int) {}
375373
2️⃣foo(a: 1)
376374
""",
377-
uri: uri
378-
)
379-
let request = RenameRequest(
380-
textDocument: TextDocumentIdentifier(uri),
381-
position: positions["1️⃣"],
382-
newName: "bar(x:"
375+
newName: "bar(x:",
376+
expected: """
377+
func bar(x: Int) {}
378+
bar(x: 1)
379+
"""
383380
)
384-
await assertThrowsError(try await testClient.send(request))
385381
}
386382

387-
func testErrorIfNewNameContainsTextAfterParenthesis() async throws {
388-
let testClient = try await TestSourceKitLSPClient()
389-
let uri = DocumentURI.for(.swift)
390-
let positions = testClient.openDocument(
383+
func testNewNameContainsTextAfterParenthesis() async throws {
384+
try await assertSingleFileRename(
391385
"""
392386
func 1️⃣foo(a: Int) {}
393387
2️⃣foo(a: 1)
394388
""",
395-
uri: uri
396-
)
397-
let request = RenameRequest(
398-
textDocument: TextDocumentIdentifier(uri),
399-
position: positions["1️⃣"],
400-
newName: "bar(x:)other:"
389+
newName: "bar(x:)other:",
390+
expected: """
391+
func bar(x: Int) {}
392+
bar(x: 1)
393+
"""
401394
)
402-
await assertThrowsError(try await testClient.send(request))
403395
}
404396

405397
func testSpacesInNewParameterNames() async throws {

0 commit comments

Comments
 (0)