2019-02-03 12:26:35 -06:00
|
|
|
use hir::db::HirDatabase;
|
2019-01-03 06:08:32 -06:00
|
|
|
use ra_syntax::{
|
2019-01-03 09:59:17 -06:00
|
|
|
Direction,
|
2019-01-03 06:08:32 -06:00
|
|
|
SyntaxKind::COMMA,
|
2019-02-21 10:49:03 -06:00
|
|
|
algo::non_trivia_sibling,
|
2019-01-03 06:08:32 -06:00
|
|
|
};
|
|
|
|
|
2019-02-24 04:53:35 -06:00
|
|
|
use crate::{AssistCtx, Assist, AssistId};
|
2019-01-03 06:08:32 -06:00
|
|
|
|
2019-02-11 11:07:21 -06:00
|
|
|
pub(crate) fn flip_comma(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
2019-01-03 09:59:17 -06:00
|
|
|
let comma = ctx.leaf_at_offset().find(|leaf| leaf.kind() == COMMA)?;
|
2019-01-03 06:08:32 -06:00
|
|
|
let prev = non_trivia_sibling(comma, Direction::Prev)?;
|
|
|
|
let next = non_trivia_sibling(comma, Direction::Next)?;
|
2019-02-24 04:53:35 -06:00
|
|
|
ctx.add_action(AssistId("flip_comma"), "flip comma", |edit| {
|
2019-02-08 17:34:05 -06:00
|
|
|
edit.target(comma.range());
|
2019-01-03 09:59:17 -06:00
|
|
|
edit.replace(prev.range(), next.text());
|
|
|
|
edit.replace(next.range(), prev.text());
|
2019-02-11 11:07:21 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
ctx.build()
|
2019-01-03 06:08:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-02-03 12:26:35 -06:00
|
|
|
|
2019-02-08 17:34:05 -06:00
|
|
|
use crate::helpers::{check_assist, 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,
|
2019-01-03 06:08:32 -06:00
|
|
|
"fn foo(x: i32,<|> y: Result<(), ()>) {}",
|
|
|
|
"fn foo(y: Result<(), ()>,<|> x: i32) {}",
|
|
|
|
)
|
|
|
|
}
|
2019-02-08 17:34:05 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flip_comma_target() {
|
|
|
|
check_assist_target(flip_comma, "fn foo(x: i32,<|> y: Result<(), ()>) {}", ",")
|
|
|
|
}
|
2019-01-03 06:08:32 -06:00
|
|
|
}
|