diff options
author | unexge <[email protected]> | 2021-01-15 19:35:42 +0000 |
---|---|---|
committer | unexge <[email protected]> | 2021-01-15 19:35:42 +0000 |
commit | c303014f3923e46ae63fbcdc6cf6f166bb040b1e (patch) | |
tree | 5bd9f3c17f87851325565c3fb0aa698d5583f62c | |
parent | a3a722de9ffaefdd0a46194e7ea2edac9754fd38 (diff) |
Create new nodes only when applying changes
-rw-r--r-- | crates/assists/src/handlers/unmerge_use.rs | 47 |
1 files 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<()> | |||
32 | let use_: ast::Use = tree_list.syntax().ancestors().find_map(ast::Use::cast)?; | 32 | let use_: ast::Use = tree_list.syntax().ancestors().find_map(ast::Use::cast)?; |
33 | let path = resolve_full_path(&tree)?; | 33 | let path = resolve_full_path(&tree)?; |
34 | 34 | ||
35 | let new_use = ast::make::use_( | ||
36 | use_.visibility(), | ||
37 | ast::make::use_tree(path, None, tree.rename(), tree.star_token().is_some()), | ||
38 | ); | ||
39 | |||
40 | let mut rewriter = SyntaxRewriter::default(); | ||
41 | rewriter += tree.remove(); | ||
42 | rewriter.insert_after(use_.syntax(), &ast::make::tokens::single_newline()); | ||
43 | if let ident_level @ 1..=usize::MAX = use_.indent_level().0 as usize { | ||
44 | rewriter.insert_after( | ||
45 | use_.syntax(), | ||
46 | &ast::make::tokens::whitespace(&" ".repeat(4 * ident_level)), | ||
47 | ); | ||
48 | } | ||
49 | rewriter.insert_after(use_.syntax(), new_use.syntax()); | ||
50 | |||
51 | let target = tree.syntax().text_range(); | 35 | let target = tree.syntax().text_range(); |
52 | acc.add( | 36 | acc.add( |
53 | AssistId("unmerge_use", AssistKind::RefactorRewrite), | 37 | AssistId("unmerge_use", AssistKind::RefactorRewrite), |
54 | "Unmerge use", | 38 | "Unmerge use", |
55 | target, | 39 | target, |
56 | |builder| { | 40 | |builder| { |
41 | let new_use = ast::make::use_( | ||
42 | use_.visibility(), | ||
43 | ast::make::use_tree( | ||
44 | path, | ||
45 | tree.use_tree_list(), | ||
46 | tree.rename(), | ||
47 | tree.star_token().is_some(), | ||
48 | ), | ||
49 | ); | ||
50 | |||
51 | let mut rewriter = SyntaxRewriter::default(); | ||
52 | rewriter += tree.remove(); | ||
53 | rewriter.insert_after(use_.syntax(), &ast::make::tokens::single_newline()); | ||
54 | if let ident_level @ 1..=usize::MAX = use_.indent_level().0 as usize { | ||
55 | rewriter.insert_after( | ||
56 | use_.syntax(), | ||
57 | &ast::make::tokens::whitespace(&" ".repeat(4 * ident_level)), | ||
58 | ); | ||
59 | } | ||
60 | rewriter.insert_after(use_.syntax(), new_use.syntax()); | ||
61 | |||
57 | builder.rewrite(rewriter); | 62 | builder.rewrite(rewriter); |
58 | }, | 63 | }, |
59 | ) | 64 | ) |
@@ -195,6 +200,16 @@ use foo::bar::{baz::{foobar}, barbaz}; | |||
195 | use foo::bar::baz::qux; | 200 | use foo::bar::baz::qux; |
196 | ", | 201 | ", |
197 | ); | 202 | ); |
203 | check_assist( | ||
204 | unmerge_use, | ||
205 | r" | ||
206 | use foo::bar::{baz$0::{qux, foobar}, barbaz}; | ||
207 | ", | ||
208 | r" | ||
209 | use foo::bar::{barbaz}; | ||
210 | use foo::bar::baz::{qux, foobar}; | ||
211 | ", | ||
212 | ); | ||
198 | } | 213 | } |
199 | 214 | ||
200 | #[test] | 215 | #[test] |