diff options
Diffstat (limited to 'crates/ra_assists/src/assists/flip_trait_bound.rs')
-rw-r--r-- | crates/ra_assists/src/assists/flip_trait_bound.rs | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/crates/ra_assists/src/assists/flip_trait_bound.rs b/crates/ra_assists/src/assists/flip_trait_bound.rs index 203092d03..1625b241f 100644 --- a/crates/ra_assists/src/assists/flip_trait_bound.rs +++ b/crates/ra_assists/src/assists/flip_trait_bound.rs | |||
@@ -1,21 +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; | 1 | use hir::db::HirDatabase; |
7 | use ra_syntax::{algo::non_trivia_sibling, ast::TypeBoundList, Direction, T}; | 2 | use ra_syntax::{ |
3 | algo::non_trivia_sibling, | ||
4 | ast::{self, AstNode}, | ||
5 | Direction, T, | ||
6 | }; | ||
8 | 7 | ||
9 | use crate::{Assist, AssistCtx, AssistId}; | 8 | use crate::{Assist, AssistCtx, AssistId}; |
10 | 9 | ||
11 | /// Flip trait bound assist. | 10 | // Assist: flip_trait_bound |
11 | // | ||
12 | // Flips two trait bounds. | ||
13 | // | ||
14 | // ``` | ||
15 | // fn foo<T: Clone +<|> Copy>() { } | ||
16 | // ``` | ||
17 | // -> | ||
18 | // ``` | ||
19 | // fn foo<T: Copy + Clone>() { } | ||
20 | // ``` | ||
12 | pub(crate) fn flip_trait_bound(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 21 | 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 | 22 | // We want to replicate the behavior of `flip_binexpr` by only suggesting |
17 | // the assist when the cursor is on a `+` | 23 | // the assist when the cursor is on a `+` |
18 | let plus = ctx.token_at_offset().find(|tkn| tkn.kind() == T![+])?; | 24 | let plus = ctx.find_token_at_offset(T![+])?; |
25 | |||
26 | // Make sure we're in a `TypeBoundList` | ||
27 | if ast::TypeBoundList::cast(plus.parent()).is_none() { | ||
28 | return None; | ||
29 | } | ||
19 | 30 | ||
20 | let (before, after) = ( | 31 | let (before, after) = ( |
21 | non_trivia_sibling(plus.clone().into(), Direction::Prev)?, | 32 | non_trivia_sibling(plus.clone().into(), Direction::Prev)?, |