diff options
Diffstat (limited to 'crates/assists/src/handlers/replace_qualified_name_with_use.rs')
-rw-r--r-- | crates/assists/src/handlers/replace_qualified_name_with_use.rs | 26 |
1 files changed, 6 insertions, 20 deletions
diff --git a/crates/assists/src/handlers/replace_qualified_name_with_use.rs b/crates/assists/src/handlers/replace_qualified_name_with_use.rs index 8ac907707..74afc123b 100644 --- a/crates/assists/src/handlers/replace_qualified_name_with_use.rs +++ b/crates/assists/src/handlers/replace_qualified_name_with_use.rs | |||
@@ -1,11 +1,10 @@ | |||
1 | use syntax::{algo::SyntaxRewriter, ast, match_ast, AstNode, SyntaxNode, TextRange}; | 1 | use syntax::{algo::SyntaxRewriter, ast, match_ast, AstNode, SyntaxNode}; |
2 | use test_utils::mark; | 2 | use test_utils::mark; |
3 | 3 | ||
4 | use crate::{ | 4 | use crate::{ |
5 | utils::{insert_use, ImportScope}, | 5 | utils::{insert_use, ImportScope}, |
6 | AssistContext, AssistId, AssistKind, Assists, | 6 | AssistContext, AssistId, AssistKind, Assists, |
7 | }; | 7 | }; |
8 | use ast::make; | ||
9 | 8 | ||
10 | // Assist: replace_qualified_name_with_use | 9 | // Assist: replace_qualified_name_with_use |
11 | // | 10 | // |
@@ -33,15 +32,6 @@ pub(crate) fn replace_qualified_name_with_use( | |||
33 | mark::hit!(dont_import_trivial_paths); | 32 | mark::hit!(dont_import_trivial_paths); |
34 | return None; | 33 | return None; |
35 | } | 34 | } |
36 | let path_to_import = path.to_string(); | ||
37 | let path_to_import = match path.segment()?.generic_arg_list() { | ||
38 | Some(generic_args) => { | ||
39 | let generic_args_start = | ||
40 | generic_args.syntax().text_range().start() - path.syntax().text_range().start(); | ||
41 | &path_to_import[TextRange::up_to(generic_args_start)] | ||
42 | } | ||
43 | None => path_to_import.as_str(), | ||
44 | }; | ||
45 | 35 | ||
46 | let target = path.syntax().text_range(); | 36 | let target = path.syntax().text_range(); |
47 | let scope = ImportScope::find_insert_use_container(path.syntax(), ctx)?; | 37 | let scope = ImportScope::find_insert_use_container(path.syntax(), ctx)?; |
@@ -54,14 +44,10 @@ pub(crate) fn replace_qualified_name_with_use( | |||
54 | // Now that we've brought the name into scope, re-qualify all paths that could be | 44 | // Now that we've brought the name into scope, re-qualify all paths that could be |
55 | // affected (that is, all paths inside the node we added the `use` to). | 45 | // affected (that is, all paths inside the node we added the `use` to). |
56 | let mut rewriter = SyntaxRewriter::default(); | 46 | let mut rewriter = SyntaxRewriter::default(); |
57 | shorten_paths(&mut rewriter, syntax.clone(), path); | 47 | shorten_paths(&mut rewriter, syntax.clone(), &path); |
58 | let rewritten_syntax = rewriter.rewrite(&syntax); | 48 | let rewritten_syntax = rewriter.rewrite(&syntax); |
59 | if let Some(ref import_scope) = ImportScope::from(rewritten_syntax) { | 49 | if let Some(ref import_scope) = ImportScope::from(rewritten_syntax) { |
60 | let new_syntax = insert_use( | 50 | let new_syntax = insert_use(import_scope, path, ctx.config.insert_use.merge); |
61 | import_scope, | ||
62 | make::path_from_text(path_to_import), | ||
63 | ctx.config.insert_use.merge, | ||
64 | ); | ||
65 | builder.replace(syntax.text_range(), new_syntax.to_string()) | 51 | builder.replace(syntax.text_range(), new_syntax.to_string()) |
66 | } | 52 | } |
67 | }, | 53 | }, |
@@ -69,7 +55,7 @@ pub(crate) fn replace_qualified_name_with_use( | |||
69 | } | 55 | } |
70 | 56 | ||
71 | /// Adds replacements to `re` that shorten `path` in all descendants of `node`. | 57 | /// Adds replacements to `re` that shorten `path` in all descendants of `node`. |
72 | fn shorten_paths(rewriter: &mut SyntaxRewriter<'static>, node: SyntaxNode, path: ast::Path) { | 58 | fn shorten_paths(rewriter: &mut SyntaxRewriter<'static>, node: SyntaxNode, path: &ast::Path) { |
73 | for child in node.children() { | 59 | for child in node.children() { |
74 | match_ast! { | 60 | match_ast! { |
75 | match child { | 61 | match child { |
@@ -82,10 +68,10 @@ fn shorten_paths(rewriter: &mut SyntaxRewriter<'static>, node: SyntaxNode, path: | |||
82 | ast::Path(p) => { | 68 | ast::Path(p) => { |
83 | match maybe_replace_path(rewriter, p.clone(), path.clone()) { | 69 | match maybe_replace_path(rewriter, p.clone(), path.clone()) { |
84 | Some(()) => {}, | 70 | Some(()) => {}, |
85 | None => shorten_paths(rewriter, p.syntax().clone(), path.clone()), | 71 | None => shorten_paths(rewriter, p.syntax().clone(), path), |
86 | } | 72 | } |
87 | }, | 73 | }, |
88 | _ => shorten_paths(rewriter, child, path.clone()), | 74 | _ => shorten_paths(rewriter, child, path), |
89 | } | 75 | } |
90 | } | 76 | } |
91 | } | 77 | } |