aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvgenii P <[email protected]>2019-07-30 14:33:58 +0100
committerEvgenii P <[email protected]>2019-07-30 14:33:58 +0100
commite7cdbe795a0eb061ce5b0274d4b3ca3029cfbe18 (patch)
treeedfbc47d54e6642c68d168442fa260c5596c7463
parentc9aca0acef75b24199e70f82313732fa489c5e1e (diff)
Fix flip comma assist
-rw-r--r--crates/ra_assists/src/flip_comma.rs25
1 files changed, 25 insertions, 0 deletions
diff --git a/crates/ra_assists/src/flip_comma.rs b/crates/ra_assists/src/flip_comma.rs
index 34489329c..1b7521e8b 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,22 @@ 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(flip_comma,
53 "pub enum Test { \
54 A,<|> \
55 }", ",");
56
57
58 check_assist_target(flip_comma,
59 "pub struct Test { \
60 foo: usize,<|> \
61 }", ",");
62 }
38} 63}