diff options
Diffstat (limited to 'crates/ra_assists/src/assists/flip_comma.rs')
-rw-r--r-- | crates/ra_assists/src/assists/flip_comma.rs | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/crates/ra_assists/src/assists/flip_comma.rs b/crates/ra_assists/src/assists/flip_comma.rs index e31cc5e7d..9be1c1dc6 100644 --- a/crates/ra_assists/src/assists/flip_comma.rs +++ b/crates/ra_assists/src/assists/flip_comma.rs | |||
@@ -1,12 +1,25 @@ | |||
1 | //! FIXME: write short doc here | ||
2 | |||
3 | use hir::db::HirDatabase; | 1 | use hir::db::HirDatabase; |
4 | use ra_syntax::{algo::non_trivia_sibling, Direction, T}; | 2 | use ra_syntax::{algo::non_trivia_sibling, Direction, T}; |
5 | 3 | ||
6 | use crate::{Assist, AssistCtx, AssistId}; | 4 | use crate::{Assist, AssistCtx, AssistId}; |
7 | 5 | ||
8 | pub(crate) fn flip_comma(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 6 | // Assist: flip_comma |
9 | let comma = ctx.token_at_offset().find(|leaf| leaf.kind() == T![,])?; | 7 | // |
8 | // Flips two comma-separated items. | ||
9 | // | ||
10 | // ``` | ||
11 | // fn main() { | ||
12 | // ((1, 2),<|> (3, 4)); | ||
13 | // } | ||
14 | // ``` | ||
15 | // -> | ||
16 | // ``` | ||
17 | // fn main() { | ||
18 | // ((3, 4), (1, 2)); | ||
19 | // } | ||
20 | // ``` | ||
21 | pub(crate) fn flip_comma(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | ||
22 | let comma = ctx.find_token_at_offset(T![,])?; | ||
10 | let prev = non_trivia_sibling(comma.clone().into(), Direction::Prev)?; | 23 | let prev = non_trivia_sibling(comma.clone().into(), Direction::Prev)?; |
11 | let next = non_trivia_sibling(comma.clone().into(), Direction::Next)?; | 24 | let next = non_trivia_sibling(comma.clone().into(), Direction::Next)?; |
12 | 25 | ||
@@ -16,13 +29,11 @@ pub(crate) fn flip_comma(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> | |||
16 | return None; | 29 | return None; |
17 | } | 30 | } |
18 | 31 | ||
19 | ctx.add_action(AssistId("flip_comma"), "flip comma", |edit| { | 32 | ctx.add_assist(AssistId("flip_comma"), "flip comma", |edit| { |
20 | edit.target(comma.text_range()); | 33 | edit.target(comma.text_range()); |
21 | edit.replace(prev.text_range(), next.to_string()); | 34 | edit.replace(prev.text_range(), next.to_string()); |
22 | edit.replace(next.text_range(), prev.to_string()); | 35 | edit.replace(next.text_range(), prev.to_string()); |
23 | }); | 36 | }) |
24 | |||
25 | ctx.build() | ||
26 | } | 37 | } |
27 | 38 | ||
28 | #[cfg(test)] | 39 | #[cfg(test)] |