]> git.lizzy.rs Git - rust.git/blob - src/tools/rust-analyzer/crates/ide-assists/src/handlers/flip_comma.rs
Rollup merge of #99460 - JanBeh:PR_asref_asmut_docs, r=joshtriplett
[rust.git] / src / tools / rust-analyzer / crates / ide-assists / src / handlers / flip_comma.rs
1 use syntax::{algo::non_trivia_sibling, Direction, SyntaxKind, T};
2
3 use crate::{AssistContext, AssistId, AssistKind, Assists};
4
5 // Assist: flip_comma
6 //
7 // Flips two comma-separated items.
8 //
9 // ```
10 // fn main() {
11 //     ((1, 2),$0 (3, 4));
12 // }
13 // ```
14 // ->
15 // ```
16 // fn main() {
17 //     ((3, 4), (1, 2));
18 // }
19 // ```
20 pub(crate) fn flip_comma(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
21     let comma = ctx.find_token_syntax_at_offset(T![,])?;
22     let prev = non_trivia_sibling(comma.clone().into(), Direction::Prev)?;
23     let next = non_trivia_sibling(comma.clone().into(), Direction::Next)?;
24
25     // Don't apply a "flip" in case of a last comma
26     // that typically comes before punctuation
27     if next.kind().is_punct() {
28         return None;
29     }
30
31     // Don't apply a "flip" inside the macro call
32     // since macro input are just mere tokens
33     if comma.parent_ancestors().any(|it| it.kind() == SyntaxKind::MACRO_CALL) {
34         return None;
35     }
36
37     acc.add(
38         AssistId("flip_comma", AssistKind::RefactorRewrite),
39         "Flip comma",
40         comma.text_range(),
41         |edit| {
42             edit.replace(prev.text_range(), next.to_string());
43             edit.replace(next.text_range(), prev.to_string());
44         },
45     )
46 }
47
48 #[cfg(test)]
49 mod tests {
50     use super::*;
51
52     use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
53
54     #[test]
55     fn flip_comma_works_for_function_parameters() {
56         check_assist(
57             flip_comma,
58             r#"fn foo(x: i32,$0 y: Result<(), ()>) {}"#,
59             r#"fn foo(y: Result<(), ()>, x: i32) {}"#,
60         )
61     }
62
63     #[test]
64     fn flip_comma_target() {
65         check_assist_target(flip_comma, r#"fn foo(x: i32,$0 y: Result<(), ()>) {}"#, ",")
66     }
67
68     #[test]
69     fn flip_comma_before_punct() {
70         // See https://github.com/rust-lang/rust-analyzer/issues/1619
71         // "Flip comma" assist shouldn't be applicable to the last comma in enum or struct
72         // declaration body.
73         check_assist_not_applicable(flip_comma, "pub enum Test { A,$0 }");
74         check_assist_not_applicable(flip_comma, "pub struct Test { foo: usize,$0 }");
75     }
76
77     #[test]
78     fn flip_comma_works() {
79         check_assist(
80             flip_comma,
81             r#"fn main() {((1, 2),$0 (3, 4));}"#,
82             r#"fn main() {((3, 4), (1, 2));}"#,
83         )
84     }
85
86     #[test]
87     fn flip_comma_not_applicable_for_macro_input() {
88         // "Flip comma" assist shouldn't be applicable inside the macro call
89         // See https://github.com/rust-lang/rust-analyzer/issues/7693
90         check_assist_not_applicable(flip_comma, r#"bar!(a,$0 b)"#);
91     }
92 }