Skip to content

Add swap binary operands code action#2694

Open
somiljain2006 wants to merge 4 commits into
swiftlang:mainfrom
somiljain2006:code-action
Open

Add swap binary operands code action#2694
somiljain2006 wants to merge 4 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

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