aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/assists/flip_binexpr.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/assists/flip_binexpr.rs')
-rw-r--r--crates/ra_assists/src/assists/flip_binexpr.rs28
1 files changed, 19 insertions, 9 deletions
diff --git a/crates/ra_assists/src/assists/flip_binexpr.rs b/crates/ra_assists/src/assists/flip_binexpr.rs
index c51035282..386045eb0 100644
--- a/crates/ra_assists/src/assists/flip_binexpr.rs
+++ b/crates/ra_assists/src/assists/flip_binexpr.rs
@@ -1,13 +1,25 @@
1//! FIXME: write short doc here
2
3use hir::db::HirDatabase; 1use hir::db::HirDatabase;
4use ra_syntax::ast::{AstNode, BinExpr, BinOp}; 2use ra_syntax::ast::{AstNode, BinExpr, BinOp};
5 3
6use crate::{Assist, AssistCtx, AssistId}; 4use crate::{Assist, AssistCtx, AssistId};
7 5
8/// Flip binary expression assist. 6// Assist: flip_binexpr
9pub(crate) fn flip_binexpr(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 7//
10 let expr = ctx.node_at_offset::<BinExpr>()?; 8// Flips operands of a binary expression.
9//
10// ```
11// fn main() {
12// let _ = 90 +<|> 2;
13// }
14// ```
15// ->
16// ```
17// fn main() {
18// let _ = 2 + 90;
19// }
20// ```
21pub(crate) fn flip_binexpr(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
22 let expr = ctx.find_node_at_offset::<BinExpr>()?;
11 let lhs = expr.lhs()?.syntax().clone(); 23 let lhs = expr.lhs()?.syntax().clone();
12 let rhs = expr.rhs()?.syntax().clone(); 24 let rhs = expr.rhs()?.syntax().clone();
13 let op_range = expr.op_token()?.text_range(); 25 let op_range = expr.op_token()?.text_range();
@@ -22,16 +34,14 @@ pub(crate) fn flip_binexpr(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assis
22 return None; 34 return None;
23 } 35 }
24 36
25 ctx.add_action(AssistId("flip_binexpr"), "flip binary expression", |edit| { 37 ctx.add_assist(AssistId("flip_binexpr"), "flip binary expression", |edit| {
26 edit.target(op_range); 38 edit.target(op_range);
27 if let FlipAction::FlipAndReplaceOp(new_op) = action { 39 if let FlipAction::FlipAndReplaceOp(new_op) = action {
28 edit.replace(op_range, new_op); 40 edit.replace(op_range, new_op);
29 } 41 }
30 edit.replace(lhs.text_range(), rhs.text()); 42 edit.replace(lhs.text_range(), rhs.text());
31 edit.replace(rhs.text_range(), lhs.text()); 43 edit.replace(rhs.text_range(), lhs.text());
32 }); 44 })
33
34 ctx.build()
35} 45}
36 46
37enum FlipAction { 47enum FlipAction {