aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_assists/src/flip_comma.rs30
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}