diff options
Diffstat (limited to 'crates/ide_assists/src/handlers/replace_qualified_name_with_use.rs')
-rw-r--r-- | crates/ide_assists/src/handlers/replace_qualified_name_with_use.rs | 28 |
1 files changed, 12 insertions, 16 deletions
diff --git a/crates/ide_assists/src/handlers/replace_qualified_name_with_use.rs b/crates/ide_assists/src/handlers/replace_qualified_name_with_use.rs index 36d2e0331..2f2306fcc 100644 --- a/crates/ide_assists/src/handlers/replace_qualified_name_with_use.rs +++ b/crates/ide_assists/src/handlers/replace_qualified_name_with_use.rs | |||
@@ -1,5 +1,5 @@ | |||
1 | use ide_db::helpers::insert_use::{insert_use, ImportScope}; | 1 | use ide_db::helpers::insert_use::{insert_use, ImportScope}; |
2 | use syntax::{algo::SyntaxRewriter, ast, match_ast, AstNode, SyntaxNode}; | 2 | use syntax::{ast, match_ast, ted, AstNode, SyntaxNode}; |
3 | 3 | ||
4 | use crate::{AssistContext, AssistId, AssistKind, Assists}; | 4 | use crate::{AssistContext, AssistId, AssistKind, Assists}; |
5 | 5 | ||
@@ -40,18 +40,17 @@ pub(crate) fn replace_qualified_name_with_use( | |||
40 | |builder| { | 40 | |builder| { |
41 | // Now that we've brought the name into scope, re-qualify all paths that could be | 41 | // Now that we've brought the name into scope, re-qualify all paths that could be |
42 | // affected (that is, all paths inside the node we added the `use` to). | 42 | // affected (that is, all paths inside the node we added the `use` to). |
43 | let mut rewriter = SyntaxRewriter::default(); | 43 | let syntax = builder.make_mut(syntax.clone()); |
44 | shorten_paths(&mut rewriter, syntax.clone(), &path); | ||
45 | if let Some(ref import_scope) = ImportScope::from(syntax.clone()) { | 44 | if let Some(ref import_scope) = ImportScope::from(syntax.clone()) { |
46 | rewriter += insert_use(import_scope, path, ctx.config.insert_use); | 45 | insert_use(import_scope, path.clone(), ctx.config.insert_use); |
47 | builder.rewrite(rewriter); | ||
48 | } | 46 | } |
47 | shorten_paths(syntax.clone(), &path.clone_for_update()); | ||
49 | }, | 48 | }, |
50 | ) | 49 | ) |
51 | } | 50 | } |
52 | 51 | ||
53 | /// Adds replacements to `re` that shorten `path` in all descendants of `node`. | 52 | /// Adds replacements to `re` that shorten `path` in all descendants of `node`. |
54 | fn shorten_paths(rewriter: &mut SyntaxRewriter<'static>, node: SyntaxNode, path: &ast::Path) { | 53 | fn shorten_paths(node: SyntaxNode, path: &ast::Path) { |
55 | for child in node.children() { | 54 | for child in node.children() { |
56 | match_ast! { | 55 | match_ast! { |
57 | match child { | 56 | match child { |
@@ -62,32 +61,28 @@ fn shorten_paths(rewriter: &mut SyntaxRewriter<'static>, node: SyntaxNode, path: | |||
62 | ast::Module(_it) => continue, | 61 | ast::Module(_it) => continue, |
63 | 62 | ||
64 | ast::Path(p) => { | 63 | ast::Path(p) => { |
65 | match maybe_replace_path(rewriter, p.clone(), path.clone()) { | 64 | match maybe_replace_path(p.clone(), path.clone()) { |
66 | Some(()) => {}, | 65 | Some(()) => {}, |
67 | None => shorten_paths(rewriter, p.syntax().clone(), path), | 66 | None => shorten_paths(p.syntax().clone(), path), |
68 | } | 67 | } |
69 | }, | 68 | }, |
70 | _ => shorten_paths(rewriter, child, path), | 69 | _ => shorten_paths(child, path), |
71 | } | 70 | } |
72 | } | 71 | } |
73 | } | 72 | } |
74 | } | 73 | } |
75 | 74 | ||
76 | fn maybe_replace_path( | 75 | fn maybe_replace_path(path: ast::Path, target: ast::Path) -> Option<()> { |
77 | rewriter: &mut SyntaxRewriter<'static>, | ||
78 | path: ast::Path, | ||
79 | target: ast::Path, | ||
80 | ) -> Option<()> { | ||
81 | if !path_eq(path.clone(), target) { | 76 | if !path_eq(path.clone(), target) { |
82 | return None; | 77 | return None; |
83 | } | 78 | } |
84 | 79 | ||
85 | // Shorten `path`, leaving only its last segment. | 80 | // Shorten `path`, leaving only its last segment. |
86 | if let Some(parent) = path.qualifier() { | 81 | if let Some(parent) = path.qualifier() { |
87 | rewriter.delete(parent.syntax()); | 82 | ted::remove(parent.syntax()); |
88 | } | 83 | } |
89 | if let Some(double_colon) = path.coloncolon_token() { | 84 | if let Some(double_colon) = path.coloncolon_token() { |
90 | rewriter.delete(&double_colon); | 85 | ted::remove(&double_colon); |
91 | } | 86 | } |
92 | 87 | ||
93 | Some(()) | 88 | Some(()) |
@@ -150,6 +145,7 @@ Debug | |||
150 | ", | 145 | ", |
151 | ); | 146 | ); |
152 | } | 147 | } |
148 | |||
153 | #[test] | 149 | #[test] |
154 | fn test_replace_add_use_no_anchor_with_item_below() { | 150 | fn test_replace_add_use_no_anchor_with_item_below() { |
155 | check_assist( | 151 | check_assist( |