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:
bors[bot] 2020-04-13 18:03:12 +00:00 committed by GitHub
commit c82e7696e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,7 +1,7 @@
use std::iter::successors;
use ra_syntax::{
algo::{neighbor, SyntaxRewriter},
algo::{neighbor, skip_trivia_token, SyntaxRewriter},
ast::{self, edit::AstNodeEdit, make},
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 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();
to_insert.push(make::token(T![,]).into());
to_insert.push(make::tokens::single_space().into());
if should_insert_comma {
to_insert.push(make::token(T![,]).into());
to_insert.push(make::tokens::single_space().into());
}
to_insert.extend(
rhs.use_tree_list()?
.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};
",
)
}
}