aboutsummaryrefslogtreecommitdiff
path: root/crates/assists/src/handlers/replace_qualified_name_with_use.rs
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2020-09-03 15:23:33 +0100
committerLukas Wirth <[email protected]>2020-09-03 17:36:08 +0100
commit7de2a30f4080d0e6d8790882238bed1cb6ccbc21 (patch)
tree450b363b8f6646ca7cda39325e4524af7b9389e8 /crates/assists/src/handlers/replace_qualified_name_with_use.rs
parent98e2f674e9e736720d1cd0a5b8c24e1fb10f64a1 (diff)
Fix import insertion breaking nested modules
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.rs20
1 files changed, 11 insertions, 9 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 597bc268c..85c70d16b 100644
--- a/crates/assists/src/handlers/replace_qualified_name_with_use.rs
+++ b/crates/assists/src/handlers/replace_qualified_name_with_use.rs
@@ -2,7 +2,7 @@ use syntax::{algo::SyntaxRewriter, ast, match_ast, AstNode, SyntaxNode, TextRang
2use test_utils::mark; 2use test_utils::mark;
3 3
4use crate::{ 4use crate::{
5 utils::{find_insert_use_container, insert_use, MergeBehaviour}, 5 utils::{insert_use, ImportScope, MergeBehaviour},
6 AssistContext, AssistId, AssistKind, Assists, 6 AssistContext, AssistId, AssistKind, Assists,
7}; 7};
8use ast::make; 8use ast::make;
@@ -44,8 +44,8 @@ pub(crate) fn replace_qualified_name_with_use(
44 }; 44 };
45 45
46 let target = path.syntax().text_range(); 46 let target = path.syntax().text_range();
47 let container = find_insert_use_container(path.syntax(), ctx)?; 47 let scope = ImportScope::find_insert_use_container(path.syntax(), ctx)?;
48 let syntax = container.either(|l| l.syntax().clone(), |r| r.syntax().clone()); 48 let syntax = scope.as_syntax_node();
49 acc.add( 49 acc.add(
50 AssistId("replace_qualified_name_with_use", AssistKind::RefactorRewrite), 50 AssistId("replace_qualified_name_with_use", AssistKind::RefactorRewrite),
51 "Replace qualified path with use", 51 "Replace qualified path with use",
@@ -56,12 +56,14 @@ pub(crate) fn replace_qualified_name_with_use(
56 let mut rewriter = SyntaxRewriter::default(); 56 let mut rewriter = SyntaxRewriter::default();
57 shorten_paths(&mut rewriter, syntax.clone(), path); 57 shorten_paths(&mut rewriter, syntax.clone(), path);
58 let rewritten_syntax = rewriter.rewrite(&syntax); 58 let rewritten_syntax = rewriter.rewrite(&syntax);
59 let new_syntax = insert_use( 59 if let Some(ref import_scope) = ImportScope::from(rewritten_syntax) {
60 &rewritten_syntax, 60 let new_syntax = insert_use(
61 make::path_from_text(path_to_import), 61 import_scope,
62 Some(MergeBehaviour::Full), 62 make::path_from_text(path_to_import),
63 ); 63 Some(MergeBehaviour::Full),
64 builder.replace(syntax.text_range(), new_syntax.to_string()) 64 );
65 builder.replace(syntax.text_range(), new_syntax.to_string())
66 }
65 }, 67 },
66 ) 68 )
67} 69}