1 use ra_syntax::{algo::non_trivia_sibling, Direction, T};
3 use crate::{Assist, AssistCtx, AssistId};
7 // Flips two comma-separated items.
11 // ((1, 2),<|> (3, 4));
20 pub(crate) fn flip_comma(ctx: AssistCtx) -> Option<Assist> {
21 let comma = ctx.find_token_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)?;
25 // Don't apply a "flip" in case of a last comma
26 // that typically comes before punctuation
27 if next.kind().is_punct() {
31 ctx.add_assist(AssistId("flip_comma"), "Flip comma", |edit| {
32 edit.target(comma.text_range());
33 edit.replace(prev.text_range(), next.to_string());
34 edit.replace(next.text_range(), prev.to_string());
42 use crate::tests::{check_assist, check_assist_target};
45 fn flip_comma_works_for_function_parameters() {
48 "fn foo(x: i32,<|> y: Result<(), ()>) {}",
49 "fn foo(y: Result<(), ()>,<|> x: i32) {}",
54 fn flip_comma_target() {
55 check_assist_target(flip_comma, "fn foo(x: i32,<|> y: Result<(), ()>) {}", ",")
60 fn flip_comma_before_punct() {
61 // See https://github.com/rust-analyzer/rust-analyzer/issues/1619
62 // "Flip comma" assist shouldn't be applicable to the last comma in enum or struct