aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src')
-rw-r--r--crates/ra_assists/src/flip_eq_operands.rs23
1 files changed, 11 insertions, 12 deletions
diff --git a/crates/ra_assists/src/flip_eq_operands.rs b/crates/ra_assists/src/flip_eq_operands.rs
index f9d1aad62..df0bb689d 100644
--- a/crates/ra_assists/src/flip_eq_operands.rs
+++ b/crates/ra_assists/src/flip_eq_operands.rs
@@ -1,24 +1,23 @@
1use hir::db::HirDatabase; 1use hir::db::HirDatabase;
2use ra_syntax::{ 2use ra_syntax::ast::{AstNode, BinExpr, BinOp};
3 ast::{AstNode, BinExpr, BinOp}
4};
5 3
6use crate::{AssistCtx, Assist, AssistId}; 4use crate::{AssistCtx, Assist, AssistId};
7 5
8pub(crate) fn flip_eq_operands(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 6pub(crate) fn flip_eq_operands(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
9 let expr = ctx.node_at_offset::<BinExpr>()?; 7 let expr = ctx.node_at_offset::<BinExpr>()?;
8 let lhs = expr.lhs()?.syntax();
9 let rhs = expr.rhs()?.syntax();
10 let op_range = expr.op()?.range();
11 let cursor_in_range = ctx.frange.range.is_subrange(&op_range);
10 let allowed_ops = [BinOp::EqualityTest, BinOp::NegatedEqualityTest]; 12 let allowed_ops = [BinOp::EqualityTest, BinOp::NegatedEqualityTest];
11 let expr_op = expr.op()?; 13 let expr_op = expr.op_kind()?;
12 if !allowed_ops.iter().any(|o| *o == expr_op) { 14 if !cursor_in_range || !allowed_ops.iter().any(|o| *o == expr_op) {
13 return None; 15 return None;
14 } 16 }
15 let node = expr.syntax();
16 let prev = node.first_child()?;
17 let next = node.last_child()?;
18 ctx.add_action(AssistId("flip_eq_operands"), "flip equality operands", |edit| { 17 ctx.add_action(AssistId("flip_eq_operands"), "flip equality operands", |edit| {
19 edit.target(node.range()); 18 edit.target(op_range);
20 edit.replace(prev.range(), next.text()); 19 edit.replace(lhs.range(), rhs.text());
21 edit.replace(next.range(), prev.text()); 20 edit.replace(rhs.range(), lhs.text());
22 }); 21 });
23 22
24 ctx.build() 23 ctx.build()
@@ -82,6 +81,6 @@ mod tests {
82 81
83 #[test] 82 #[test]
84 fn flip_eq_operands_target() { 83 fn flip_eq_operands_target() {
85 check_assist_target(flip_eq_operands, "fn f() { let res = 1 ==<|> 2; }", "1 == 2") 84 check_assist_target(flip_eq_operands, "fn f() { let res = 1 ==<|> 2; }", "==")
86 } 85 }
87} 86}