ide_assists/handlers/
flip_or_pattern.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use syntax::{
    Direction, T,
    algo::non_trivia_sibling,
    ast::{self, AstNode},
};

use crate::{AssistContext, AssistId, Assists};

// Assist: flip_or_pattern
//
// Flips two patterns in an or-pattern.
//
// ```
// fn foo() {
//     let (a |$0 b) = 1;
// }
// ```
// ->
// ```
// fn foo() {
//     let (b | a) = 1;
// }
// ```
pub(crate) fn flip_or_pattern(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
    // Only flip on the `|` token
    let pipe = ctx.find_token_syntax_at_offset(T![|])?;

    let parent = ast::OrPat::cast(pipe.parent()?)?;

    let before = non_trivia_sibling(pipe.clone().into(), Direction::Prev)?.into_node()?;
    let after = non_trivia_sibling(pipe.clone().into(), Direction::Next)?.into_node()?;

    let target = pipe.text_range();
    acc.add(AssistId::refactor_rewrite("flip_or_pattern"), "Flip patterns", target, |builder| {
        let mut editor = builder.make_editor(parent.syntax());
        editor.replace(before.clone(), after.clone());
        editor.replace(after, before);
        builder.add_file_edits(ctx.file_id(), editor);
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};

    #[test]
    fn flip_or_pattern_assist_available() {
        check_assist_target(flip_or_pattern, "fn main(a |$0 b: ()) {}", "|")
    }

    #[test]
    fn flip_or_pattern_not_applicable_for_leading_pipe() {
        check_assist_not_applicable(flip_or_pattern, "fn main(|$0 b: ()) {}")
    }

    #[test]
    fn flip_or_pattern_works() {
        check_assist(
            flip_or_pattern,
            "fn foo() { let (a | b |$0 c | d) = 1; }",
            "fn foo() { let (a | c | b | d) = 1; }",
        )
    }

    #[test]
    fn flip_or_pattern_works_match_guard() {
        check_assist(
            flip_or_pattern,
            "fn foo() { match() { a |$0 b if true => () }}",
            "fn foo() { match() { b | a if true => () }}",
        )
    }
}