diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_assists/src/assists/flip_trait_bound.rs | 32 | ||||
-rw-r--r-- | crates/ra_assists/src/lib.rs | 2 |
2 files changed, 34 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 | |||
6 | use hir::db::HirDatabase; | ||
7 | use ra_syntax::{algo::non_trivia_sibling, ast::TypeBoundList, Direction, T}; | ||
8 | |||
9 | use crate::{Assist, AssistCtx, AssistId}; | ||
10 | |||
11 | /// Flip trait bound assist. | ||
12 | pub(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 | } | ||
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index de576324f..871d49960 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs | |||
@@ -97,6 +97,7 @@ mod assists { | |||
97 | mod apply_demorgan; | 97 | mod apply_demorgan; |
98 | mod flip_comma; | 98 | mod flip_comma; |
99 | mod flip_binexpr; | 99 | mod flip_binexpr; |
100 | mod flip_trait_bound; | ||
100 | mod change_visibility; | 101 | mod change_visibility; |
101 | mod fill_match_arms; | 102 | mod fill_match_arms; |
102 | mod merge_match_arms; | 103 | mod merge_match_arms; |
@@ -123,6 +124,7 @@ mod assists { | |||
123 | merge_match_arms::merge_match_arms, | 124 | merge_match_arms::merge_match_arms, |
124 | flip_comma::flip_comma, | 125 | flip_comma::flip_comma, |
125 | flip_binexpr::flip_binexpr, | 126 | flip_binexpr::flip_binexpr, |
127 | flip_trait_bound::flip_trait_bound, | ||
126 | introduce_variable::introduce_variable, | 128 | introduce_variable::introduce_variable, |
127 | replace_if_let_with_match::replace_if_let_with_match, | 129 | replace_if_let_with_match::replace_if_let_with_match, |
128 | split_import::split_import, | 130 | split_import::split_import, |