aboutsummaryrefslogtreecommitdiff
path: root/crates/assists/src/handlers/auto_import.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/assists/src/handlers/auto_import.rs')
-rw-r--r--crates/assists/src/handlers/auto_import.rs22
1 files changed, 16 insertions, 6 deletions
diff --git a/crates/assists/src/handlers/auto_import.rs b/crates/assists/src/handlers/auto_import.rs
index 0fd2f94fa..7182a2a5d 100644
--- a/crates/assists/src/handlers/auto_import.rs
+++ b/crates/assists/src/handlers/auto_import.rs
@@ -1,3 +1,5 @@
1use syntax::ast;
2
1use crate::{ 3use crate::{
2 utils::import_assets::{ImportAssets, ImportCandidate}, 4 utils::import_assets::{ImportAssets, ImportCandidate},
3 utils::{insert_use, mod_path_to_ast, ImportScope}, 5 utils::{insert_use, mod_path_to_ast, ImportScope},
@@ -24,16 +26,24 @@ use crate::{
24// # pub mod std { pub mod collections { pub struct HashMap { } } } 26// # pub mod std { pub mod collections { pub struct HashMap { } } }
25// ``` 27// ```
26pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { 28pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
27 let auto_import_assets = ImportAssets::new(&ctx)?; 29 let import_assets =
28 let proposed_imports = auto_import_assets.search_for_imports(&ctx.sema, &ctx.config.insert_use); 30 if let Some(path_under_caret) = ctx.find_node_at_offset_with_descend::<ast::Path>() {
31 ImportAssets::for_regular_path(path_under_caret, &ctx.sema)
32 } else if let Some(method_under_caret) =
33 ctx.find_node_at_offset_with_descend::<ast::MethodCallExpr>()
34 {
35 ImportAssets::for_method_call(method_under_caret, &ctx.sema)
36 } else {
37 None
38 }?;
39 let proposed_imports = import_assets.search_for_imports(&ctx.sema, &ctx.config.insert_use);
29 if proposed_imports.is_empty() { 40 if proposed_imports.is_empty() {
30 return None; 41 return None;
31 } 42 }
32 43
33 let range = ctx.sema.original_range(auto_import_assets.syntax_under_caret()).range; 44 let range = ctx.sema.original_range(import_assets.syntax_under_caret()).range;
34 let group = import_group_message(auto_import_assets.import_candidate()); 45 let group = import_group_message(import_assets.import_candidate());
35 let scope = 46 let scope = ImportScope::find_insert_use_container(import_assets.syntax_under_caret(), ctx)?;
36 ImportScope::find_insert_use_container(auto_import_assets.syntax_under_caret(), ctx)?;
37 let syntax = scope.as_syntax_node(); 47 let syntax = scope.as_syntax_node();
38 for import in proposed_imports { 48 for import in proposed_imports {
39 acc.add_group( 49 acc.add_group(