diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-07-30 17:48:35 +0100 |
---|---|---|
committer | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-07-30 17:48:35 +0100 |
commit | 457fdf90c8110f74fb9e917fac825a60915c22a8 (patch) | |
tree | 4ba087762ce53633d1d59ae6384109d94ed408ea /crates/ra_assists | |
parent | c9aca0acef75b24199e70f82313732fa489c5e1e (diff) | |
parent | 06c3de310e4a9e82c64eb3e537a2b358ea2a6aed (diff) |
Merge #1625
1625: Fix flip comma assist r=matklad a=eupn
Should fix and close #1619 🤔
r? @matklad
Co-authored-by: Evgenii P <[email protected]>
Diffstat (limited to 'crates/ra_assists')
-rw-r--r-- | crates/ra_assists/src/flip_comma.rs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/crates/ra_assists/src/flip_comma.rs b/crates/ra_assists/src/flip_comma.rs index 34489329c..5ee7561bc 100644 --- a/crates/ra_assists/src/flip_comma.rs +++ b/crates/ra_assists/src/flip_comma.rs | |||
@@ -7,6 +7,13 @@ pub(crate) fn flip_comma(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> | |||
7 | let comma = ctx.token_at_offset().find(|leaf| leaf.kind() == T![,])?; | 7 | let comma = ctx.token_at_offset().find(|leaf| leaf.kind() == T![,])?; |
8 | let prev = non_trivia_sibling(comma.clone().into(), Direction::Prev)?; | 8 | let prev = non_trivia_sibling(comma.clone().into(), Direction::Prev)?; |
9 | let next = non_trivia_sibling(comma.clone().into(), Direction::Next)?; | 9 | let next = non_trivia_sibling(comma.clone().into(), Direction::Next)?; |
10 | |||
11 | // Don't apply a "flip" in case of a last comma | ||
12 | // that typically comes before punctuation | ||
13 | if next.kind().is_punct() { | ||
14 | return None; | ||
15 | } | ||
16 | |||
10 | ctx.add_action(AssistId("flip_comma"), "flip comma", |edit| { | 17 | ctx.add_action(AssistId("flip_comma"), "flip comma", |edit| { |
11 | edit.target(comma.text_range()); | 18 | edit.target(comma.text_range()); |
12 | edit.replace(prev.text_range(), next.to_string()); | 19 | edit.replace(prev.text_range(), next.to_string()); |
@@ -35,4 +42,27 @@ mod tests { | |||
35 | fn flip_comma_target() { | 42 | fn flip_comma_target() { |
36 | check_assist_target(flip_comma, "fn foo(x: i32,<|> y: Result<(), ()>) {}", ",") | 43 | check_assist_target(flip_comma, "fn foo(x: i32,<|> y: Result<(), ()>) {}", ",") |
37 | } | 44 | } |
45 | |||
46 | #[test] | ||
47 | #[should_panic] | ||
48 | fn flip_comma_before_punct() { | ||
49 | // See https://github.com/rust-analyzer/rust-analyzer/issues/1619 | ||
50 | // "Flip comma" assist shouldn't be applicable to the last comma in enum or struct | ||
51 | // declaration body. | ||
52 | check_assist_target( | ||
53 | flip_comma, | ||
54 | "pub enum Test { \ | ||
55 | A,<|> \ | ||
56 | }", | ||
57 | ",", | ||
58 | ); | ||
59 | |||
60 | check_assist_target( | ||
61 | flip_comma, | ||
62 | "pub struct Test { \ | ||
63 | foo: usize,<|> \ | ||
64 | }", | ||
65 | ",", | ||
66 | ); | ||
67 | } | ||
38 | } | 68 | } |