diff options
Diffstat (limited to 'crates/ra_assists')
-rw-r--r-- | crates/ra_assists/src/assists/flip_trait_bound.rs | 33 | ||||
-rw-r--r-- | crates/ra_assists/src/doc_tests.rs | 12 | ||||
-rw-r--r-- | crates/ra_assists/src/doc_tests/generated.rs | 13 |
3 files changed, 46 insertions, 12 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)?, |
diff --git a/crates/ra_assists/src/doc_tests.rs b/crates/ra_assists/src/doc_tests.rs index 0ccf9d730..6e1e3de84 100644 --- a/crates/ra_assists/src/doc_tests.rs +++ b/crates/ra_assists/src/doc_tests.rs | |||
@@ -17,7 +17,17 @@ fn check(assist_id: &str, before: &str, after: &str) { | |||
17 | let (_assist_id, action) = crate::assists(&db, frange) | 17 | let (_assist_id, action) = crate::assists(&db, frange) |
18 | .into_iter() | 18 | .into_iter() |
19 | .find(|(id, _)| id.id.0 == assist_id) | 19 | .find(|(id, _)| id.id.0 == assist_id) |
20 | .unwrap_or_else(|| panic!("Assist {:?} is not applicable", assist_id)); | 20 | .unwrap_or_else(|| { |
21 | panic!( | ||
22 | "\n\nAssist is not applicable: {}\nAvailable assists: {}", | ||
23 | assist_id, | ||
24 | crate::assists(&db, frange) | ||
25 | .into_iter() | ||
26 | .map(|(id, _)| id.id.0) | ||
27 | .collect::<Vec<_>>() | ||
28 | .join(", ") | ||
29 | ) | ||
30 | }); | ||
21 | 31 | ||
22 | let actual = action.edit.apply(&before); | 32 | let actual = action.edit.apply(&before); |
23 | assert_eq_text!(after, &actual); | 33 | assert_eq_text!(after, &actual); |
diff --git a/crates/ra_assists/src/doc_tests/generated.rs b/crates/ra_assists/src/doc_tests/generated.rs index b8d335911..ebe49aecf 100644 --- a/crates/ra_assists/src/doc_tests/generated.rs +++ b/crates/ra_assists/src/doc_tests/generated.rs | |||
@@ -256,6 +256,19 @@ fn main() { | |||
256 | } | 256 | } |
257 | 257 | ||
258 | #[test] | 258 | #[test] |
259 | fn doctest_flip_trait_bound() { | ||
260 | check( | ||
261 | "flip_trait_bound", | ||
262 | r#####" | ||
263 | fn foo<T: Clone +<|> Copy>() { } | ||
264 | "#####, | ||
265 | r#####" | ||
266 | fn foo<T: Copy + Clone>() { } | ||
267 | "#####, | ||
268 | ) | ||
269 | } | ||
270 | |||
271 | #[test] | ||
259 | fn doctest_inline_local_variable() { | 272 | fn doctest_inline_local_variable() { |
260 | check( | 273 | check( |
261 | "inline_local_variable", | 274 | "inline_local_variable", |