aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api_light/src/assists/flip_comma.rs
blob: a343413cc5789e7e6a89e3e32513a214f3b0e2d0 (plain)
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
use ra_syntax::{
    Direction,
    SyntaxKind::COMMA,
};

use crate::assists::{non_trivia_sibling, AssistCtx, Assist};

pub fn flip_comma(ctx: AssistCtx) -> Option<Assist> {
    let comma = ctx.leaf_at_offset().find(|leaf| leaf.kind() == COMMA)?;
    let prev = non_trivia_sibling(comma, Direction::Prev)?;
    let next = non_trivia_sibling(comma, Direction::Next)?;
    ctx.build("flip comma", |edit| {
        edit.replace(prev.range(), next.text());
        edit.replace(next.range(), prev.text());
    })
}

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

    #[test]
    fn flip_comma_works_for_function_parameters() {
        check_assist(
            flip_comma,
            "fn foo(x: i32,<|> y: Result<(), ()>) {}",
            "fn foo(y: Result<(), ()>,<|> x: i32) {}",
        )
    }
}