aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/assists/flip_trait_bound.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/assists/flip_trait_bound.rs')
-rw-r--r--crates/ra_assists/src/assists/flip_trait_bound.rs32
1 files changed, 32 insertions, 0 deletions
diff --git a/crates/ra_assists/src/assists/flip_trait_bound.rs b/crates/ra_assists/src/assists/flip_trait_bound.rs
new file mode 100644
index 000000000..a2c954ec5
--- /dev/null
+++ b/crates/ra_assists/src/assists/flip_trait_bound.rs
@@ -0,0 +1,32 @@
1//! Assist for swapping traits inside of a trait bound list
2//!
3//! E.g. `A + B` => `B + A` when the cursor is placed by the `+` inside of a
4//! trait bound list
5
6use hir::db::HirDatabase;
7use ra_syntax::{algo::non_trivia_sibling, ast::TypeBoundList, Direction, T};
8
9use crate::{Assist, AssistCtx, AssistId};
10
11/// Flip trait bound assist.
12pub(crate) fn flip_trait_bound(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
13 // Make sure we're in a `TypeBoundList`
14 ctx.node_at_offset::<TypeBoundList>()?;
15
16 // We want to replicate the behavior of `flip_binexpr` by only suggesting
17 // the assist when the cursor is on a `+`
18 let plus = ctx.token_at_offset().find(|tkn| tkn.kind() == T![+])?;
19
20 let (before, after) = (
21 non_trivia_sibling(plus.clone().into(), Direction::Prev)?,
22 non_trivia_sibling(plus.clone().into(), Direction::Next)?,
23 );
24
25 ctx.add_action(AssistId("flip_trait_bound"), "flip trait bound", |edit| {
26 edit.target(plus.text_range());
27 edit.replace(before.text_range(), after.to_string());
28 edit.replace(after.text_range(), before.to_string());
29 });
30
31 ctx.build()
32}