Skip to content

Add swap binary operands code action#2694

Merged
ahoppen merged 5 commits into
swiftlang:mainfrom
somiljain2006:code-action
Jul 3, 2026
Merged

Add swap binary operands code action#2694
ahoppen merged 5 commits into
swiftlang:mainfrom
somiljain2006:code-action

Conversation

@somiljain2006

Copy link
Copy Markdown
Contributor

Fixes #2520

Implement swap binary operands refactoring. Add a syntax-based code action that swaps the left and right operands of a binary expression when the cursor is positioned on the operator. Comparison operators are inverted as needed (for example, < becomes >), while commutative operators are preserved. Add tests covering nested expressions, operator selection, unsupported operators, and trivia preservation.

@somiljain2006

Copy link
Copy Markdown
Contributor Author

@ahoppen @rintaro Can you review this pr?

Comment thread Sources/SwiftSyntaxCodeActions/SwapBinaryOperands.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/SwapBinaryOperands.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/SwapBinaryOperands.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/SwapBinaryOperands.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/SwapBinaryOperands.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/SwapBinaryOperands.swift Outdated
Comment thread Tests/SourceKitLSPTests/CodeActionTests.swift
@somiljain2006
somiljain2006 requested a review from ahoppen June 19, 2026 20:24
@somiljain2006

Copy link
Copy Markdown
Contributor Author

@ahoppen, can you review the updated changes?

@ahoppen ahoppen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. The results look good but I think the implementation can be simplified a bit. Left a few comments below.

Comment thread Sources/SwiftSyntaxCodeActions/SwapBinaryOperands.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/SwapBinaryOperands.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/SwapBinaryOperands.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/SwapBinaryOperands.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/SwapBinaryOperands.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/SwapBinaryOperands.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/SwapBinaryOperands.swift
Comment thread Tests/SourceKitLSPTests/CodeActionTests.swift Outdated
Comment thread Tests/SourceKitLSPTests/CodeActionTests.swift Outdated
Comment thread Tests/SourceKitLSPTests/CodeActionTests.swift Outdated
@somiljain2006
somiljain2006 requested a review from ahoppen July 2, 2026 13:14

@ahoppen ahoppen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple small comments, otherwise looks good to me.

Comment thread Sources/SwiftSyntaxCodeActions/SyntaxCodeActionProvider.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/SwapBinaryOperands.swift Outdated
Comment thread Sources/SwiftSyntaxCodeActions/SwapBinaryOperands.swift Outdated

@ahoppen ahoppen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you @somiljain2006!

@ahoppen
ahoppen enabled auto-merge (squash) July 2, 2026 14:20
@ahoppen

ahoppen commented Jul 2, 2026

Copy link
Copy Markdown
Member

@swift-ci Please test

@ahoppen

ahoppen commented Jul 3, 2026

Copy link
Copy Markdown
Member

@somiljain2006 Would you be able to resolve the merge conflict introduced by #2671?

auto-merge was automatically disabled July 3, 2026 08:01

Head branch was pushed to by a user without write access

@somiljain2006

Copy link
Copy Markdown
Contributor Author

@ahoppen Done

@somiljain2006
somiljain2006 requested a review from ahoppen July 3, 2026 08:09
@ahoppen

ahoppen commented Jul 3, 2026

Copy link
Copy Markdown
Member

@swift-ci Please test

@ahoppen
ahoppen enabled auto-merge July 3, 2026 08:11
@ahoppen

ahoppen commented Jul 3, 2026

Copy link
Copy Markdown
Member

@swift-ci Please test Windows

@ahoppen
ahoppen merged commit c747f6e into swiftlang:main Jul 3, 2026
3 checks passed
@somiljain2006
somiljain2006 deleted the code-action branch July 3, 2026 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Flip operands of binary expression code action

2 participants