mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-11 20:58:54 +00:00
Merge #3961
3961: Fix double comma when merge imports on second line r=edwin0cheng a=IceSentry This fixes the bug when merging imports from the second line when it already has a comma it would previously insert a comma. There's probably a better way to check for a COMMA. This also ends up with a weird indentation, but rust-fmt can easily deal with it so I'm not sure how to resolve that. Closes #3832 Co-authored-by: IceSentry <c.giguere42@gmail.com>
This commit is contained in:
commit
c82e7696e6
1 changed files with 30 additions and 3 deletions
|
@ -1,7 +1,7 @@
|
||||||
use std::iter::successors;
|
use std::iter::successors;
|
||||||
|
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
algo::{neighbor, SyntaxRewriter},
|
algo::{neighbor, skip_trivia_token, SyntaxRewriter},
|
||||||
ast::{self, edit::AstNodeEdit, make},
|
ast::{self, edit::AstNodeEdit, make},
|
||||||
AstNode, Direction, InsertPosition, SyntaxElement, T,
|
AstNode, Direction, InsertPosition, SyntaxElement, T,
|
||||||
};
|
};
|
||||||
|
@ -72,9 +72,18 @@ fn try_merge_trees(old: &ast::UseTree, new: &ast::UseTree) -> Option<ast::UseTre
|
||||||
let lhs = old.split_prefix(&lhs_prefix);
|
let lhs = old.split_prefix(&lhs_prefix);
|
||||||
let rhs = new.split_prefix(&rhs_prefix);
|
let rhs = new.split_prefix(&rhs_prefix);
|
||||||
|
|
||||||
|
let should_insert_comma = lhs
|
||||||
|
.use_tree_list()?
|
||||||
|
.r_curly_token()
|
||||||
|
.and_then(|it| skip_trivia_token(it.prev_token()?, Direction::Prev))
|
||||||
|
.map(|it| it.kind() != T![,])
|
||||||
|
.unwrap_or(true);
|
||||||
|
|
||||||
let mut to_insert: Vec<SyntaxElement> = Vec::new();
|
let mut to_insert: Vec<SyntaxElement> = Vec::new();
|
||||||
to_insert.push(make::token(T![,]).into());
|
if should_insert_comma {
|
||||||
to_insert.push(make::tokens::single_space().into());
|
to_insert.push(make::token(T![,]).into());
|
||||||
|
to_insert.push(make::tokens::single_space().into());
|
||||||
|
}
|
||||||
to_insert.extend(
|
to_insert.extend(
|
||||||
rhs.use_tree_list()?
|
rhs.use_tree_list()?
|
||||||
.syntax()
|
.syntax()
|
||||||
|
@ -247,4 +256,22 @@ use {
|
||||||
",
|
",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_double_comma() {
|
||||||
|
check_assist(
|
||||||
|
merge_imports,
|
||||||
|
r"
|
||||||
|
use foo::bar::baz;
|
||||||
|
use foo::<|>{
|
||||||
|
FooBar,
|
||||||
|
};
|
||||||
|
",
|
||||||
|
r"
|
||||||
|
use foo::{<|>
|
||||||
|
FooBar,
|
||||||
|
bar::baz};
|
||||||
|
",
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue