-
Notifications
You must be signed in to change notification settings - Fork 332
Add if let to guard and Vice versa transforms #2420
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
base: main
Are you sure you want to change the base?
Conversation
ahoppen
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for implementing this refactoring, @PhantomInTheWire ❤️
The PR is already pretty large, so I’d suggest we focus on the if -> guard refactoring in this PR first and leave guard -> if for a follow-up PR, to simplify review.
I left some first initial comments in the PR. The overall direction seems great and you thought about many edge cases. I’ll do another thorough review once the first set of comments are addressed.
Sources/SwiftLanguageService/CodeActions/ConvertIfLetToGuard.swift
Outdated
Show resolved
Hide resolved
Sources/SwiftLanguageService/CodeActions/ConvertIfLetToGuard.swift
Outdated
Show resolved
Hide resolved
Sources/SwiftLanguageService/CodeActions/ConvertIfLetToGuard.swift
Outdated
Show resolved
Hide resolved
Sources/SwiftLanguageService/CodeActions/ConvertIfLetToGuard.swift
Outdated
Show resolved
Hide resolved
Sources/SwiftLanguageService/CodeActions/ConvertIfLetToGuard.swift
Outdated
Show resolved
Hide resolved
| let lineTable = LineTable(cleanInput) | ||
| let sortedEdits = changes.sorted { | ||
| (a: LanguageServerProtocol.TextEdit, b: LanguageServerProtocol.TextEdit) -> Bool in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| (a: LanguageServerProtocol.TextEdit, b: LanguageServerProtocol.TextEdit) -> Bool in | |
| (a: TextEdit, b: TextEdit) -> Bool in |
| if a.range.lowerBound.line != b.range.lowerBound.line { | ||
| return a.range.lowerBound.line > b.range.lowerBound.line | ||
| } | ||
| return a.range.lowerBound.utf16index > b.range.lowerBound.utf16index |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Position should conform to Comparable, so you should be able to compare that straight away.
| return a.range.lowerBound.utf16index > b.range.lowerBound.utf16index | ||
| } | ||
| for edit in sortedEdits { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should be able to use apply(edits:to:) from RenameAssertions, which already does this.
| """, | ||
| expectedOutput: """ | ||
| func test() -> Int? { | ||
| guard let value = optional else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like the double whitespace before else shouldn’t be there.
| } | ||
| /// Tests guard-to-if conversion eligibility directly without LSP overhead. | ||
| private func assertConvertibleToIfLet( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of having a separate test function to test whether something is convertible, should we just use validateCodeAction and check that we don’t get a specific code action?
Signed-off-by: Karan <[email protected]>
Signed-off-by: Karan <[email protected]>
601bb0d to
594ee8a
Compare
Signed-off-by: Karan <[email protected]>
…anteesExit Signed-off-by: Karan <[email protected]>
Signed-off-by: Karan <[email protected]>
… doesn't exist Signed-off-by: Karan <[email protected]>
Signed-off-by: Karan <[email protected]>
Signed-off-by: Karan <[email protected]>
5e90594 to
410ce27
Compare
ahoppen
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Started reviewing this again but then realized that you haven’t addressed all my previous comments yet (no problem with that, just didn’t realize before I started reviewing), so stopped half-way.
| if parent.is(ExpressionStmtSyntax.self) { | ||
| current = parent | ||
| continue | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should only check if the parent of ifExpr is an ExpressionStmtSyntax instead of having a loop. Otherwise we would pick the wrong if statement to convert in the following when invoking on b.
if let a {
let x = if let b { b } else { nil }
}| let guardStmt = buildGuardStatement(from: ifExpr, elseBody: Array(followingStatements)) | ||
| let newBodyStatements = ifExpr.body.statements | ||
|
|
||
| let lastStatement = followingStatements[followingStatements.index(before: followingStatements.endIndex)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn’t this the same as followingStatements.last? We could avoid the force unwrap of last if we captured the last statement instead of checking for followingStatements.isEmpty.
| let newTrivia: Trivia = .newline + baseIndentation | ||
| let unindentedStmt = stmt.with(\.leadingTrivia, newTrivia) | ||
| replacementText += unindentedStmt.description |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this has two issues:
- It removes non-whitespace trivia, eg.
if let a {
// a comment
return
}
print(a)- It doesn’t properly un-indent multi-line code items, eg.
if let a {
print(
"oops"
)
return
}
print(a)This is what I meant in #2420 (comment)
fixes: #1569
mostly works but not sure what to do with many edge cases and has a todo for switch statements, also this will probably have conflicts with #2406 marking as draft till that merges and i can resolve the conflicts.
Screen.Recording.2026-01-04.at.17.03.17.mov