diff options
author | vsrs <[email protected]> | 2020-05-08 17:34:34 +0100 |
---|---|---|
committer | vsrs <[email protected]> | 2020-05-08 17:34:34 +0100 |
commit | 0ef17ef1ee9fb0ce7149176d12f4d225f6d01401 (patch) | |
tree | fa2f168120f36f8dbef5dc1e85fea0c0071639c2 /crates/ra_assists/src/handlers/auto_import.rs | |
parent | 1be6320ea6cf7830195f80681fa0f43cc340da7e (diff) | |
parent | d3eb9d8eafbebca7da95fa8a4813b92eb5080500 (diff) |
Merge remote-tracking branch 'upstream/master' into uniformed_debug_lens
# Conflicts:
# editors/code/src/commands/runnables.ts
Diffstat (limited to 'crates/ra_assists/src/handlers/auto_import.rs')
-rw-r--r-- | crates/ra_assists/src/handlers/auto_import.rs | 41 |
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 @@ | |||
1 | use std::collections::BTreeSet; | 1 | use std::collections::BTreeSet; |
2 | 2 | ||
3 | use either::Either; | ||
3 | use hir::{ | 4 | use 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 | }; |
13 | use rustc_hash::FxHashSet; | 14 | use rustc_hash::FxHashSet; |
14 | 15 | ||
15 | use crate::{ | 16 | use crate::{utils::insert_use_statement, AssistContext, AssistId, Assists, GroupLabel}; |
16 | assist_ctx::{Assist, AssistCtx}, | ||
17 | utils::insert_use_statement, | ||
18 | AssistId, | ||
19 | }; | ||
20 | use 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 | // ``` |
41 | pub(crate) fn auto_import(ctx: AssistCtx) -> Option<Assist> { | 37 | pub(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 | ||
65 | impl AutoImportAssets { | 67 | impl 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 | ||