Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-assists/src/handlers/flip_binexpr.rs')
-rw-r--r--crates/ide-assists/src/handlers/flip_binexpr.rs54
1 files changed, 28 insertions, 26 deletions
diff --git a/crates/ide-assists/src/handlers/flip_binexpr.rs b/crates/ide-assists/src/handlers/flip_binexpr.rs
index 8b46a23f9a..818a868fe3 100644
--- a/crates/ide-assists/src/handlers/flip_binexpr.rs
+++ b/crates/ide-assists/src/handlers/flip_binexpr.rs
@@ -1,4 +1,7 @@
-use syntax::ast::{self, AstNode, BinExpr};
+use syntax::{
+ ast::{self, syntax_factory::SyntaxFactory, AstNode, BinExpr},
+ SyntaxKind, T,
+};
use crate::{AssistContext, AssistId, AssistKind, Assists};
@@ -19,22 +22,17 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
// ```
pub(crate) fn flip_binexpr(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
let expr = ctx.find_node_at_offset::<BinExpr>()?;
- let rhs = expr.rhs()?.syntax().clone();
- let lhs = expr.lhs()?.syntax().clone();
-
- let lhs = if let Some(bin_expr) = BinExpr::cast(lhs.clone()) {
- if bin_expr.op_kind() == expr.op_kind() {
- bin_expr.rhs()?.syntax().clone()
- } else {
- lhs
- }
- } else {
- lhs
+ let lhs = expr.lhs()?;
+ let rhs = expr.rhs()?;
+
+ let lhs = match &lhs {
+ ast::Expr::BinExpr(bin_expr) if bin_expr.op_kind() == expr.op_kind() => bin_expr.rhs()?,
+ _ => lhs,
};
- let op_range = expr.op_token()?.text_range();
+ let op_token = expr.op_token()?;
// The assist should be applied only if the cursor is on the operator
- let cursor_in_range = op_range.contains_range(ctx.selection_trimmed());
+ let cursor_in_range = op_token.text_range().contains_range(ctx.selection_trimmed());
if !cursor_in_range {
return None;
}
@@ -47,13 +45,17 @@ pub(crate) fn flip_binexpr(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
acc.add(
AssistId("flip_binexpr", AssistKind::RefactorRewrite),
"Flip binary expression",
- op_range,
- |edit| {
- if let FlipAction::FlipAndReplaceOp(new_op) = action {
- edit.replace(op_range, new_op);
- }
- edit.replace(lhs.text_range(), rhs.text());
- edit.replace(rhs.text_range(), lhs.text());
+ op_token.text_range(),
+ |builder| {
+ let mut editor = builder.make_editor(&expr.syntax().parent().unwrap());
+ let make = SyntaxFactory::new();
+ if let FlipAction::FlipAndReplaceOp(binary_op) = action {
+ editor.replace(op_token, make.token(binary_op))
+ };
+ editor.replace(lhs.syntax(), rhs.syntax());
+ editor.replace(rhs.syntax(), lhs.syntax());
+ editor.add_mappings(make.finish_with_mappings());
+ builder.add_file_edits(ctx.file_id(), editor);
},
)
}
@@ -62,7 +64,7 @@ enum FlipAction {
// Flip the expression
Flip,
// Flip the expression and replace the operator with this string
- FlipAndReplaceOp(&'static str),
+ FlipAndReplaceOp(SyntaxKind),
// Do not flip the expression
DontFlip,
}
@@ -73,10 +75,10 @@ impl From<ast::BinaryOp> for FlipAction {
ast::BinaryOp::Assignment { .. } => FlipAction::DontFlip,
ast::BinaryOp::CmpOp(ast::CmpOp::Ord { ordering, strict }) => {
let rev_op = match (ordering, strict) {
- (ast::Ordering::Less, true) => ">",
- (ast::Ordering::Less, false) => ">=",
- (ast::Ordering::Greater, true) => "<",
- (ast::Ordering::Greater, false) => "<=",
+ (ast::Ordering::Less, true) => T![>],
+ (ast::Ordering::Less, false) => T![>=],
+ (ast::Ordering::Greater, true) => T![<],
+ (ast::Ordering::Greater, false) => T![<=],
};
FlipAction::FlipAndReplaceOp(rev_op)
}