aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/handlers/auto_import.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/handlers/auto_import.rs')
-rw-r--r--crates/ra_assists/src/handlers/auto_import.rs41
1 files changed, 22 insertions, 19 deletions
diff --git a/crates/ra_assists/src/handlers/auto_import.rs b/crates/ra_assists/src/handlers/auto_import.rs
index 2224b9714..78d23150d 100644
--- a/crates/ra_assists/src/handlers/auto_import.rs
+++ b/crates/ra_assists/src/handlers/auto_import.rs
@@ -1,5 +1,6 @@
1use std::collections::BTreeSet; 1use std::collections::BTreeSet;
2 2
3use either::Either;
3use hir::{ 4use hir::{
4 AsAssocItem, AssocItemContainer, ModPath, Module, ModuleDef, PathResolution, Semantics, Trait, 5 AsAssocItem, AssocItemContainer, ModPath, Module, ModuleDef, PathResolution, Semantics, Trait,
5 Type, 6 Type,
@@ -12,12 +13,7 @@ use ra_syntax::{
12}; 13};
13use rustc_hash::FxHashSet; 14use rustc_hash::FxHashSet;
14 15
15use crate::{ 16use crate::{utils::insert_use_statement, AssistContext, AssistId, Assists, GroupLabel};
16 assist_ctx::{Assist, AssistCtx},
17 utils::insert_use_statement,
18 AssistId,
19};
20use either::Either;
21 17
22// Assist: auto_import 18// Assist: auto_import
23// 19//
@@ -38,7 +34,7 @@ use either::Either;
38// } 34// }
39// # pub mod std { pub mod collections { pub struct HashMap { } } } 35// # pub mod std { pub mod collections { pub struct HashMap { } } }
40// ``` 36// ```
41pub(crate) fn auto_import(ctx: AssistCtx) -> Option<Assist> { 37pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
42 let auto_import_assets = AutoImportAssets::new(&ctx)?; 38 let auto_import_assets = AutoImportAssets::new(&ctx)?;
43 let proposed_imports = auto_import_assets.search_for_imports(ctx.db); 39 let proposed_imports = auto_import_assets.search_for_imports(ctx.db);
44 if proposed_imports.is_empty() { 40 if proposed_imports.is_empty() {
@@ -46,13 +42,19 @@ pub(crate) fn auto_import(ctx: AssistCtx) -> Option<Assist> {
46 } 42 }
47 43
48 let range = ctx.sema.original_range(&auto_import_assets.syntax_under_caret).range; 44 let range = ctx.sema.original_range(&auto_import_assets.syntax_under_caret).range;
49 let mut group = ctx.add_assist_group(auto_import_assets.get_import_group_message()); 45 let group = auto_import_assets.get_import_group_message();
50 for import in proposed_imports { 46 for import in proposed_imports {
51 group.add_assist(AssistId("auto_import"), format!("Import `{}`", &import), range, |edit| { 47 acc.add_group(
52 insert_use_statement(&auto_import_assets.syntax_under_caret, &import, edit); 48 &group,
53 }); 49 AssistId("auto_import"),
50 format!("Import `{}`", &import),
51 range,
52 |builder| {
53 insert_use_statement(&auto_import_assets.syntax_under_caret, &import, ctx, builder);
54 },
55 );
54 } 56 }
55 group.finish() 57 Some(())
56} 58}
57 59
58#[derive(Debug)] 60#[derive(Debug)]
@@ -63,7 +65,7 @@ struct AutoImportAssets {
63} 65}
64 66
65impl AutoImportAssets { 67impl AutoImportAssets {
66 fn new(ctx: &AssistCtx) -> Option<Self> { 68 fn new(ctx: &AssistContext) -> Option<Self> {
67 if let Some(path_under_caret) = ctx.find_node_at_offset_with_descend::<ast::Path>() { 69 if let Some(path_under_caret) = ctx.find_node_at_offset_with_descend::<ast::Path>() {
68 Self::for_regular_path(path_under_caret, &ctx) 70 Self::for_regular_path(path_under_caret, &ctx)
69 } else { 71 } else {
@@ -71,7 +73,7 @@ impl AutoImportAssets {
71 } 73 }
72 } 74 }
73 75
74 fn for_method_call(method_call: ast::MethodCallExpr, ctx: &AssistCtx) -> Option<Self> { 76 fn for_method_call(method_call: ast::MethodCallExpr, ctx: &AssistContext) -> Option<Self> {
75 let syntax_under_caret = method_call.syntax().to_owned(); 77 let syntax_under_caret = method_call.syntax().to_owned();
76 let module_with_name_to_import = ctx.sema.scope(&syntax_under_caret).module()?; 78 let module_with_name_to_import = ctx.sema.scope(&syntax_under_caret).module()?;
77 Some(Self { 79 Some(Self {
@@ -81,7 +83,7 @@ impl AutoImportAssets {
81 }) 83 })
82 } 84 }
83 85
84 fn for_regular_path(path_under_caret: ast::Path, ctx: &AssistCtx) -> Option<Self> { 86 fn for_regular_path(path_under_caret: ast::Path, ctx: &AssistContext) -> Option<Self> {
85 let syntax_under_caret = path_under_caret.syntax().to_owned(); 87 let syntax_under_caret = path_under_caret.syntax().to_owned();
86 if syntax_under_caret.ancestors().find_map(ast::UseItem::cast).is_some() { 88 if syntax_under_caret.ancestors().find_map(ast::UseItem::cast).is_some() {
87 return None; 89 return None;
@@ -104,8 +106,8 @@ impl AutoImportAssets {
104 } 106 }
105 } 107 }
106 108
107 fn get_import_group_message(&self) -> String { 109 fn get_import_group_message(&self) -> GroupLabel {
108 match &self.import_candidate { 110 let name = match &self.import_candidate {
109 ImportCandidate::UnqualifiedName(name) => format!("Import {}", name), 111 ImportCandidate::UnqualifiedName(name) => format!("Import {}", name),
110 ImportCandidate::QualifierStart(qualifier_start) => { 112 ImportCandidate::QualifierStart(qualifier_start) => {
111 format!("Import {}", qualifier_start) 113 format!("Import {}", qualifier_start)
@@ -116,7 +118,8 @@ impl AutoImportAssets {
116 ImportCandidate::TraitMethod(_, trait_method_name) => { 118 ImportCandidate::TraitMethod(_, trait_method_name) => {
117 format!("Import a trait for method {}", trait_method_name) 119 format!("Import a trait for method {}", trait_method_name)
118 } 120 }
119 } 121 };
122 GroupLabel(name)
120 } 123 }
121 124
122 fn search_for_imports(&self, db: &RootDatabase) -> BTreeSet<ModPath> { 125 fn search_for_imports(&self, db: &RootDatabase) -> BTreeSet<ModPath> {
@@ -383,7 +386,7 @@ mod tests {
383 } 386 }
384 ", 387 ",
385 r" 388 r"
386 use PubMod1::PubStruct; 389 use PubMod3::PubStruct;
387 390
388 PubSt<|>ruct 391 PubSt<|>ruct
389 392