aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/utils/insert_use.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/utils/insert_use.rs')
-rw-r--r--crates/ra_assists/src/utils/insert_use.rs11
1 files changed, 7 insertions, 4 deletions
diff --git a/crates/ra_assists/src/utils/insert_use.rs b/crates/ra_assists/src/utils/insert_use.rs
index c507e71e0..0ee43482f 100644
--- a/crates/ra_assists/src/utils/insert_use.rs
+++ b/crates/ra_assists/src/utils/insert_use.rs
@@ -11,17 +11,20 @@ use ra_syntax::{
11}; 11};
12use ra_text_edit::TextEditBuilder; 12use ra_text_edit::TextEditBuilder;
13 13
14use crate::assist_context::AssistContext;
15
14/// Creates and inserts a use statement for the given path to import. 16/// Creates and inserts a use statement for the given path to import.
15/// The use statement is inserted in the scope most appropriate to the 17/// The use statement is inserted in the scope most appropriate to the
16/// the cursor position given, additionally merged with the existing use imports. 18/// the cursor position given, additionally merged with the existing use imports.
17pub fn insert_use_statement( 19pub(crate) fn insert_use_statement(
18 // Ideally the position of the cursor, used to 20 // Ideally the position of the cursor, used to
19 position: &SyntaxNode, 21 position: &SyntaxNode,
20 path_to_import: &ModPath, 22 path_to_import: &ModPath,
21 edit: &mut TextEditBuilder, 23 ctx: &AssistContext,
24 builder: &mut TextEditBuilder,
22) { 25) {
23 let target = path_to_import.to_string().split("::").map(SmolStr::new).collect::<Vec<_>>(); 26 let target = path_to_import.to_string().split("::").map(SmolStr::new).collect::<Vec<_>>();
24 let container = position.ancestors().find_map(|n| { 27 let container = ctx.sema.ancestors_with_macros(position.clone()).find_map(|n| {
25 if let Some(module) = ast::Module::cast(n.clone()) { 28 if let Some(module) = ast::Module::cast(n.clone()) {
26 return module.item_list().map(|it| it.syntax().clone()); 29 return module.item_list().map(|it| it.syntax().clone());
27 } 30 }
@@ -30,7 +33,7 @@ pub fn insert_use_statement(
30 33
31 if let Some(container) = container { 34 if let Some(container) = container {
32 let action = best_action_for_target(container, position.clone(), &target); 35 let action = best_action_for_target(container, position.clone(), &target);
33 make_assist(&action, &target, edit); 36 make_assist(&action, &target, builder);
34 } 37 }
35} 38}
36 39