From abdf725c558f860b47883a6843bfd2a5a7c633bd Mon Sep 17 00:00:00 2001 From: IceSentry Date: Sun, 12 Apr 2020 21:29:14 -0400 Subject: Fix double comma when merge imports on second line This fixes the a 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 --- crates/ra_assists/src/handlers/merge_imports.rs | 40 +++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) (limited to 'crates/ra_assists/src') diff --git a/crates/ra_assists/src/handlers/merge_imports.rs b/crates/ra_assists/src/handlers/merge_imports.rs index 0958f52f1..ab1fb2a0b 100644 --- a/crates/ra_assists/src/handlers/merge_imports.rs +++ b/crates/ra_assists/src/handlers/merge_imports.rs @@ -3,7 +3,8 @@ use std::iter::successors; use ra_syntax::{ algo::{neighbor, SyntaxRewriter}, ast::{self, edit::AstNodeEdit, make}, - AstNode, Direction, InsertPosition, SyntaxElement, T, + AstNode, Direction, InsertPosition, NodeOrToken, SyntaxElement, SyntaxKind, SyntaxToken, Token, + T, }; use crate::{Assist, AssistCtx, AssistId}; @@ -72,9 +73,24 @@ fn try_merge_trees(old: &ast::UseTree, new: &ast::UseTree) -> Option = 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 +263,22 @@ use { ", ); } + + #[test] + fn test_double_comma() { + check_assist( + merge_imports, + r" +use hyper::service::make_service_fn; +use hyper::<|>{ + StatusCode, +}; +", + r" +use hyper::{<|> + StatusCode, +service::make_service_fn}; +", + ) + } } -- cgit v1.2.3