From c303014f3923e46ae63fbcdc6cf6f166bb040b1e Mon Sep 17 00:00:00 2001 From: unexge Date: Fri, 15 Jan 2021 22:35:42 +0300 Subject: Create new nodes only when applying changes --- crates/assists/src/handlers/unmerge_use.rs | 47 ++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/crates/assists/src/handlers/unmerge_use.rs b/crates/assists/src/handlers/unmerge_use.rs index d7dfe70d9..6da1795da 100644 --- a/crates/assists/src/handlers/unmerge_use.rs +++ b/crates/assists/src/handlers/unmerge_use.rs @@ -32,28 +32,33 @@ pub(crate) fn unmerge_use(acc: &mut Assists, ctx: &AssistContext) -> Option<()> let use_: ast::Use = tree_list.syntax().ancestors().find_map(ast::Use::cast)?; let path = resolve_full_path(&tree)?; - let new_use = ast::make::use_( - use_.visibility(), - ast::make::use_tree(path, None, tree.rename(), tree.star_token().is_some()), - ); - - let mut rewriter = SyntaxRewriter::default(); - rewriter += tree.remove(); - rewriter.insert_after(use_.syntax(), &ast::make::tokens::single_newline()); - if let ident_level @ 1..=usize::MAX = use_.indent_level().0 as usize { - rewriter.insert_after( - use_.syntax(), - &ast::make::tokens::whitespace(&" ".repeat(4 * ident_level)), - ); - } - rewriter.insert_after(use_.syntax(), new_use.syntax()); - let target = tree.syntax().text_range(); acc.add( AssistId("unmerge_use", AssistKind::RefactorRewrite), "Unmerge use", target, |builder| { + let new_use = ast::make::use_( + use_.visibility(), + ast::make::use_tree( + path, + tree.use_tree_list(), + tree.rename(), + tree.star_token().is_some(), + ), + ); + + let mut rewriter = SyntaxRewriter::default(); + rewriter += tree.remove(); + rewriter.insert_after(use_.syntax(), &ast::make::tokens::single_newline()); + if let ident_level @ 1..=usize::MAX = use_.indent_level().0 as usize { + rewriter.insert_after( + use_.syntax(), + &ast::make::tokens::whitespace(&" ".repeat(4 * ident_level)), + ); + } + rewriter.insert_after(use_.syntax(), new_use.syntax()); + builder.rewrite(rewriter); }, ) @@ -193,6 +198,16 @@ use foo::bar::{baz::{qux$0, foobar}, barbaz}; r" use foo::bar::{baz::{foobar}, barbaz}; use foo::bar::baz::qux; +", + ); + check_assist( + unmerge_use, + r" +use foo::bar::{baz$0::{qux, foobar}, barbaz}; +", + r" +use foo::bar::{barbaz}; +use foo::bar::baz::{qux, foobar}; ", ); } -- cgit v1.2.3