mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-08 21:13:55 +00:00
Fix flip comma assist
This commit is contained in:
parent
c9aca0acef
commit
e7cdbe795a
@ -7,6 +7,13 @@ pub(crate) fn flip_comma(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist>
|
||||
let comma = ctx.token_at_offset().find(|leaf| leaf.kind() == T![,])?;
|
||||
let prev = non_trivia_sibling(comma.clone().into(), Direction::Prev)?;
|
||||
let next = non_trivia_sibling(comma.clone().into(), Direction::Next)?;
|
||||
|
||||
// Don't apply a "flip" in case of a last comma
|
||||
// that typically comes before punctuation
|
||||
if next.kind().is_punct() {
|
||||
return None;
|
||||
}
|
||||
|
||||
ctx.add_action(AssistId("flip_comma"), "flip comma", |edit| {
|
||||
edit.target(comma.text_range());
|
||||
edit.replace(prev.text_range(), next.to_string());
|
||||
@ -35,4 +42,22 @@ mod tests {
|
||||
fn flip_comma_target() {
|
||||
check_assist_target(flip_comma, "fn foo(x: i32,<|> y: Result<(), ()>) {}", ",")
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
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_target(flip_comma,
|
||||
"pub enum Test { \
|
||||
A,<|> \
|
||||
}", ",");
|
||||
|
||||
|
||||
check_assist_target(flip_comma,
|
||||
"pub struct Test { \
|
||||
foo: usize,<|> \
|
||||
}", ",");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user