rust/crates/ide-assists/src/handlers/flip_comma.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

93 lines
2.6 KiB
Rust
Raw Normal View History

use syntax::{algo::non_trivia_sibling, Direction, SyntaxKind, T};
2019-01-03 06:08:32 -06:00
2020-06-28 17:36:05 -05:00
use crate::{AssistContext, AssistId, AssistKind, Assists};
2019-01-03 06:08:32 -06:00
2019-10-26 11:08:13 -05:00
// Assist: flip_comma
//
// Flips two comma-separated items.
//
// ```
// fn main() {
2021-01-06 14:15:48 -06:00
// ((1, 2),$0 (3, 4));
2019-10-26 11:08:13 -05:00
// }
// ```
// ->
// ```
// fn main() {
// ((3, 4), (1, 2));
// }
// ```
pub(crate) fn flip_comma(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let comma = ctx.find_token_syntax_at_offset(T![,])?;
2019-07-19 03:24:41 -05:00
let prev = non_trivia_sibling(comma.clone().into(), Direction::Prev)?;
let next = non_trivia_sibling(comma.clone().into(), Direction::Next)?;
2019-07-30 08:33:58 -05:00
// Don't apply a "flip" in case of a last comma
// that typically comes before punctuation
if next.kind().is_punct() {
return None;
}
// Don't apply a "flip" inside the macro call
// since macro input are just mere tokens
if comma.ancestors().any(|it| it.kind() == SyntaxKind::MACRO_CALL) {
return None;
}
2020-06-28 17:36:05 -05:00
acc.add(
2020-07-02 16:48:35 -05:00
AssistId("flip_comma", AssistKind::RefactorRewrite),
2020-06-28 17:36:05 -05:00
"Flip comma",
comma.text_range(),
|edit| {
edit.replace(prev.text_range(), next.to_string());
edit.replace(next.text_range(), prev.to_string());
},
)
2019-01-03 06:08:32 -06:00
}
#[cfg(test)]
mod tests {
use super::*;
2019-02-03 12:26:35 -06:00
use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
2019-01-03 06:08:32 -06:00
#[test]
2019-01-03 09:59:17 -06:00
fn flip_comma_works_for_function_parameters() {
check_assist(
flip_comma,
2021-01-07 09:21:00 -06:00
r#"fn foo(x: i32,$0 y: Result<(), ()>) {}"#,
r#"fn foo(y: Result<(), ()>, x: i32) {}"#,
2019-01-03 06:08:32 -06:00
)
}
2019-02-08 17:34:05 -06:00
#[test]
fn flip_comma_target() {
2021-01-07 09:21:00 -06:00
check_assist_target(flip_comma, r#"fn foo(x: i32,$0 y: Result<(), ()>) {}"#, ",")
2019-02-08 17:34:05 -06:00
}
2019-07-30 08:33:58 -05:00
#[test]
fn flip_comma_before_punct() {
// See https://github.com/rust-analyzer/rust-analyzer/issues/1619
// "Flip comma" assist shouldn't be applicable to the last comma in enum or struct
// declaration body.
check_assist_not_applicable(flip_comma, "pub enum Test { A,$0 }");
check_assist_not_applicable(flip_comma, "pub struct Test { foo: usize,$0 }");
2019-07-30 08:33:58 -05:00
}
#[test]
fn flip_comma_works() {
check_assist(
flip_comma,
r#"fn main() {((1, 2),$0 (3, 4));}"#,
r#"fn main() {((3, 4), (1, 2));}"#,
)
}
#[test]
fn flip_comma_not_applicable_for_macro_input() {
// "Flip comma" assist shouldn't be applicable inside the macro call
// See https://github.com/rust-analyzer/rust-analyzer/issues/7693
check_assist_not_applicable(flip_comma, r#"bar!(a,$0 b)"#);
}
2019-01-03 06:08:32 -06:00
}