Skip to content

Fix renaming enum case parameters with unnamed associated arguments #2678

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
Jun 12, 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
8 changes: 8 additions & 0 deletions Sources/SwiftIDEUtils/DeclNameLocation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ public struct DeclNameLocation: Equatable {
/// The parameter of a function declaration, like `func foo(a b: Int)`
case parameters([Argument])

/// An enum case declaration like `case myCase(label: String)`.
///
/// For enum case parameters the argument label is removed when set to empty instead of being changed to eg.
/// `myCase(_ label: String)`
case enumCaseParameters([Argument])

/// Same as `param` but the parameters can't be collapsed if they are the same. This is the case for subscript
/// declarations.
///
Expand All @@ -123,6 +129,8 @@ public struct DeclNameLocation: Equatable {
)
case .parameters(let parameters):
return .parameters(parameters.map { $0.advanced(by: utf8Offset) })
case .enumCaseParameters(let parameters):
return .enumCaseParameters(parameters.map { $0.advanced(by: utf8Offset) })
case .noncollapsibleParameters(let parameters):
return .noncollapsibleParameters(parameters.map { $0.advanced(by: utf8Offset) })
case .selector(let arguments):
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftIDEUtils/NameMatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public class NameMatcher: SyntaxAnyVisitor {
return .unlabeled(argument: Syntax(argument.secondName) ?? Syntax(argument.colon) ?? Syntax(argument.type))
}
}
addResolvedLocIfRequested(baseName: node.name, argumentLabels: .parameters(argumentLabels))
addResolvedLocIfRequested(baseName: node.name, argumentLabels: .enumCaseParameters(argumentLabels))
}
return .visitChildren
}
Expand Down
55 changes: 55 additions & 0 deletions Tests/SwiftIDEUtilsTest/NameMatcherTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private func assertNameMatcherResult(
case .noArguments: argumentLabels = []
case .call(let labels, _): argumentLabels = labels
case .parameters(let labels): argumentLabels = labels
case .enumCaseParameters(let labels): argumentLabels = labels
case .noncollapsibleParameters(let labels): argumentLabels = labels
case .selector(let labels): argumentLabels = labels
}
Expand All @@ -75,6 +76,7 @@ private struct DeclNameLocationSpec {
case noArguments
case call
case parameters
case enumCaseParameters
case noncollapsibleParameters
case selector

Expand All @@ -83,6 +85,7 @@ private struct DeclNameLocationSpec {
case .noArguments: self = .noArguments
case .call: self = .call
case .parameters: self = .parameters
case .enumCaseParameters: self = .enumCaseParameters
case .noncollapsibleParameters: self = .noncollapsibleParameters
case .selector: self = .selector
}
Expand Down Expand Up @@ -326,4 +329,56 @@ class NameMatcherTests: XCTestCase {
]
)
}

func testEnumCaseParameterWithLabels() {
assertNameMatcherResult(
"""
enum MyEnum {
case 1️⃣myCase(label: String)
}
""",
expected: [
DeclNameLocationSpec(baseName: "myCase", argumentLabels: ["label"], type: .enumCaseParameters)
]
)
}

func testEnumCaseParameterWithoutLabels() {
assertNameMatcherResult(
"""
enum MyEnum {
case 1️⃣myCase(String)
}
""",
expected: [
DeclNameLocationSpec(baseName: "myCase", argumentLabels: [""], type: .enumCaseParameters)
]
)
}

func testEnumCaseParameterWithoutAssociatedValues() {
assertNameMatcherResult(
"""
enum MyEnum {
case 1️⃣myCase
}
""",
expected: [
DeclNameLocationSpec(baseName: "myCase", argumentLabels: [], type: .noArguments)
]
)
}

func testEnumCaseParameterWithoutWildcardAsExternalLabel() {
assertNameMatcherResult(
"""
enum MyEnum {
case 1️⃣myCase(_ label: String)
}
""",
expected: [
DeclNameLocationSpec(baseName: "myCase", argumentLabels: ["_ label"], type: .enumCaseParameters)
]
)
}
}