aboutsummaryrefslogtreecommitdiff
path: root/crates/assists
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-01-16 17:52:29 +0000
committerGitHub <[email protected]>2021-01-16 17:52:29 +0000
commit9a349f280ff1c6d0b57df80aa3d6720474e4b00a (patch)
tree23bbb365f31438949358a576bc9467fa70fa874e /crates/assists
parent3782c78d7558633be5483a04aa1c098fe76100b9 (diff)
parent497fc232e7d90d8d39c7a13742dd85d758dc2f72 (diff)
Merge #7295
7295: Share import_assets and related entities r=matklad a=SomeoneToIgnore Part of https://github.com/rust-analyzer/rust-analyzer/pull/7293 Addresses https://github.com/rust-analyzer/rust-analyzer/pull/7293#issuecomment-761569558 Prepares `import_assets` and related to be used later for the trait fuzzy importing. Also moves fuzzy imports into a separate completion module and renames them, as suggested in https://github.com/rust-analyzer/rust-analyzer/pull/7293#discussion_r558896685 Co-authored-by: Kirill Bulatov <[email protected]>
Diffstat (limited to 'crates/assists')
-rw-r--r--crates/assists/src/assist_config.rs8
-rw-r--r--crates/assists/src/handlers/auto_import.rs9
-rw-r--r--crates/assists/src/handlers/qualify_path.rs40
-rw-r--r--crates/assists/src/lib.rs2
-rw-r--r--crates/assists/src/tests.rs9
-rw-r--r--crates/assists/src/utils.rs1
-rw-r--r--crates/assists/src/utils/import_assets.rs265
7 files changed, 34 insertions, 300 deletions
diff --git a/crates/assists/src/assist_config.rs b/crates/assists/src/assist_config.rs
index 4fe8ea761..9cabf037c 100644
--- a/crates/assists/src/assist_config.rs
+++ b/crates/assists/src/assist_config.rs
@@ -4,7 +4,7 @@
4//! module, and we use to statically check that we only produce snippet 4//! module, and we use to statically check that we only produce snippet
5//! assists if we are allowed to. 5//! assists if we are allowed to.
6 6
7use ide_db::helpers::{insert_use::MergeBehavior, SnippetCap}; 7use ide_db::helpers::{insert_use::InsertUseConfig, SnippetCap};
8 8
9use crate::AssistKind; 9use crate::AssistKind;
10 10
@@ -14,9 +14,3 @@ pub struct AssistConfig {
14 pub allowed: Option<Vec<AssistKind>>, 14 pub allowed: Option<Vec<AssistKind>>,
15 pub insert_use: InsertUseConfig, 15 pub insert_use: InsertUseConfig,
16} 16}
17
18#[derive(Clone, Copy, Debug, PartialEq, Eq)]
19pub struct InsertUseConfig {
20 pub merge: Option<MergeBehavior>,
21 pub prefix_kind: hir::PrefixKind,
22}
diff --git a/crates/assists/src/handlers/auto_import.rs b/crates/assists/src/handlers/auto_import.rs
index 55620f0f3..4e2a4fcd9 100644
--- a/crates/assists/src/handlers/auto_import.rs
+++ b/crates/assists/src/handlers/auto_import.rs
@@ -1,13 +1,11 @@
1use ide_db::helpers::{ 1use ide_db::helpers::{
2 import_assets::{ImportAssets, ImportCandidate},
2 insert_use::{insert_use, ImportScope}, 3 insert_use::{insert_use, ImportScope},
3 mod_path_to_ast, 4 mod_path_to_ast,
4}; 5};
5use syntax::ast; 6use syntax::ast;
6 7
7use crate::{ 8use crate::{AssistContext, AssistId, AssistKind, Assists, GroupLabel};
8 utils::import_assets::{ImportAssets, ImportCandidate},
9 AssistContext, AssistId, AssistKind, Assists, GroupLabel,
10};
11 9
12// Feature: Auto Import 10// Feature: Auto Import
13// 11//
@@ -121,8 +119,7 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
121 119
122fn import_group_message(import_candidate: &ImportCandidate) -> GroupLabel { 120fn import_group_message(import_candidate: &ImportCandidate) -> GroupLabel {
123 let name = match import_candidate { 121 let name = match import_candidate {
124 ImportCandidate::UnqualifiedName(candidate) 122 ImportCandidate::Path(candidate) => format!("Import {}", &candidate.name),
125 | ImportCandidate::QualifierStart(candidate) => format!("Import {}", &candidate.name),
126 ImportCandidate::TraitAssocItem(candidate) => { 123 ImportCandidate::TraitAssocItem(candidate) => {
127 format!("Import a trait for item {}", &candidate.name) 124 format!("Import a trait for item {}", &candidate.name)
128 } 125 }
diff --git a/crates/assists/src/handlers/qualify_path.rs b/crates/assists/src/handlers/qualify_path.rs
index f7fbf37f4..a7d9fd4dc 100644
--- a/crates/assists/src/handlers/qualify_path.rs
+++ b/crates/assists/src/handlers/qualify_path.rs
@@ -1,7 +1,10 @@
1use std::iter; 1use std::iter;
2 2
3use hir::AsName; 3use hir::AsName;
4use ide_db::helpers::mod_path_to_ast; 4use ide_db::helpers::{
5 import_assets::{ImportAssets, ImportCandidate},
6 mod_path_to_ast,
7};
5use ide_db::RootDatabase; 8use ide_db::RootDatabase;
6use syntax::{ 9use syntax::{
7 ast, 10 ast,
@@ -12,7 +15,6 @@ use test_utils::mark;
12 15
13use crate::{ 16use crate::{
14 assist_context::{AssistContext, Assists}, 17 assist_context::{AssistContext, Assists},
15 utils::import_assets::{ImportAssets, ImportCandidate},
16 AssistId, AssistKind, GroupLabel, 18 AssistId, AssistKind, GroupLabel,
17}; 19};
18 20
@@ -53,17 +55,18 @@ pub(crate) fn qualify_path(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
53 let range = ctx.sema.original_range(import_assets.syntax_under_caret()).range; 55 let range = ctx.sema.original_range(import_assets.syntax_under_caret()).range;
54 56
55 let qualify_candidate = match candidate { 57 let qualify_candidate = match candidate {
56 ImportCandidate::QualifierStart(_) => { 58 ImportCandidate::Path(candidate) => {
57 mark::hit!(qualify_path_qualifier_start); 59 if candidate.qualifier.is_some() {
58 let path = ast::Path::cast(import_assets.syntax_under_caret().clone())?; 60 mark::hit!(qualify_path_qualifier_start);
59 let (prev_segment, segment) = (path.qualifier()?.segment()?, path.segment()?); 61 let path = ast::Path::cast(import_assets.syntax_under_caret().clone())?;
60 QualifyCandidate::QualifierStart(segment, prev_segment.generic_arg_list()) 62 let (prev_segment, segment) = (path.qualifier()?.segment()?, path.segment()?);
61 } 63 QualifyCandidate::QualifierStart(segment, prev_segment.generic_arg_list())
62 ImportCandidate::UnqualifiedName(_) => { 64 } else {
63 mark::hit!(qualify_path_unqualified_name); 65 mark::hit!(qualify_path_unqualified_name);
64 let path = ast::Path::cast(import_assets.syntax_under_caret().clone())?; 66 let path = ast::Path::cast(import_assets.syntax_under_caret().clone())?;
65 let generics = path.segment()?.generic_arg_list(); 67 let generics = path.segment()?.generic_arg_list();
66 QualifyCandidate::UnqualifiedName(generics) 68 QualifyCandidate::UnqualifiedName(generics)
69 }
67 } 70 }
68 ImportCandidate::TraitAssocItem(_) => { 71 ImportCandidate::TraitAssocItem(_) => {
69 mark::hit!(qualify_path_trait_assoc_item); 72 mark::hit!(qualify_path_trait_assoc_item);
@@ -186,7 +189,7 @@ fn item_as_trait(item: hir::ItemInNs) -> Option<hir::Trait> {
186 189
187fn group_label(candidate: &ImportCandidate) -> GroupLabel { 190fn group_label(candidate: &ImportCandidate) -> GroupLabel {
188 let name = match candidate { 191 let name = match candidate {
189 ImportCandidate::UnqualifiedName(it) | ImportCandidate::QualifierStart(it) => &it.name, 192 ImportCandidate::Path(it) => &it.name,
190 ImportCandidate::TraitAssocItem(it) | ImportCandidate::TraitMethod(it) => &it.name, 193 ImportCandidate::TraitAssocItem(it) | ImportCandidate::TraitMethod(it) => &it.name,
191 }; 194 };
192 GroupLabel(format!("Qualify {}", name)) 195 GroupLabel(format!("Qualify {}", name))
@@ -194,8 +197,13 @@ fn group_label(candidate: &ImportCandidate) -> GroupLabel {
194 197
195fn label(candidate: &ImportCandidate, import: &hir::ModPath) -> String { 198fn label(candidate: &ImportCandidate, import: &hir::ModPath) -> String {
196 match candidate { 199 match candidate {
197 ImportCandidate::UnqualifiedName(_) => format!("Qualify as `{}`", &import), 200 ImportCandidate::Path(candidate) => {
198 ImportCandidate::QualifierStart(_) => format!("Qualify with `{}`", &import), 201 if candidate.qualifier.is_some() {
202 format!("Qualify with `{}`", &import)
203 } else {
204 format!("Qualify as `{}`", &import)
205 }
206 }
199 ImportCandidate::TraitAssocItem(_) => format!("Qualify `{}`", &import), 207 ImportCandidate::TraitAssocItem(_) => format!("Qualify `{}`", &import),
200 ImportCandidate::TraitMethod(_) => format!("Qualify with cast as `{}`", &import), 208 ImportCandidate::TraitMethod(_) => format!("Qualify with cast as `{}`", &import),
201 } 209 }
diff --git a/crates/assists/src/lib.rs b/crates/assists/src/lib.rs
index 3d7971806..14178a651 100644
--- a/crates/assists/src/lib.rs
+++ b/crates/assists/src/lib.rs
@@ -24,7 +24,7 @@ use syntax::TextRange;
24 24
25pub(crate) use crate::assist_context::{AssistContext, Assists}; 25pub(crate) use crate::assist_context::{AssistContext, Assists};
26 26
27pub use assist_config::{AssistConfig, InsertUseConfig}; 27pub use assist_config::AssistConfig;
28 28
29#[derive(Debug, Clone, Copy, PartialEq, Eq)] 29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30pub enum AssistKind { 30pub enum AssistKind {
diff --git a/crates/assists/src/tests.rs b/crates/assists/src/tests.rs
index 71431b406..32bd8698b 100644
--- a/crates/assists/src/tests.rs
+++ b/crates/assists/src/tests.rs
@@ -3,16 +3,17 @@ mod generated;
3use hir::Semantics; 3use hir::Semantics;
4use ide_db::{ 4use ide_db::{
5 base_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt}, 5 base_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt},
6 helpers::{insert_use::MergeBehavior, SnippetCap}, 6 helpers::{
7 insert_use::{InsertUseConfig, MergeBehavior},
8 SnippetCap,
9 },
7 source_change::FileSystemEdit, 10 source_change::FileSystemEdit,
8 RootDatabase, 11 RootDatabase,
9}; 12};
10use syntax::TextRange; 13use syntax::TextRange;
11use test_utils::{assert_eq_text, extract_offset, extract_range}; 14use test_utils::{assert_eq_text, extract_offset, extract_range};
12 15
13use crate::{ 16use crate::{handlers::Handler, Assist, AssistConfig, AssistContext, AssistKind, Assists};
14 handlers::Handler, Assist, AssistConfig, AssistContext, AssistKind, Assists, InsertUseConfig,
15};
16use stdx::{format_to, trim_indent}; 17use stdx::{format_to, trim_indent};
17 18
18pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig { 19pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig {
diff --git a/crates/assists/src/utils.rs b/crates/assists/src/utils.rs
index 9ea96eb73..fc9f83bab 100644
--- a/crates/assists/src/utils.rs
+++ b/crates/assists/src/utils.rs
@@ -1,5 +1,4 @@
1//! Assorted functions shared by several assists. 1//! Assorted functions shared by several assists.
2pub(crate) mod import_assets;
3 2
4use std::ops; 3use std::ops;
5 4
diff --git a/crates/assists/src/utils/import_assets.rs b/crates/assists/src/utils/import_assets.rs
deleted file mode 100644
index 4ce82c1ba..000000000
--- a/crates/assists/src/utils/import_assets.rs
+++ /dev/null
@@ -1,265 +0,0 @@
1//! Look up accessible paths for items.
2use either::Either;
3use hir::{AsAssocItem, AssocItemContainer, ModuleDef, Semantics};
4use ide_db::{imports_locator, RootDatabase};
5use rustc_hash::FxHashSet;
6use syntax::{ast, AstNode, SyntaxNode};
7
8use crate::assist_config::InsertUseConfig;
9
10#[derive(Debug)]
11pub(crate) enum ImportCandidate {
12 /// Simple name like 'HashMap'
13 UnqualifiedName(PathImportCandidate),
14 /// First part of the qualified name.
15 /// For 'std::collections::HashMap', that will be 'std'.
16 QualifierStart(PathImportCandidate),
17 /// A trait associated function (with no self parameter) or associated constant.
18 /// For 'test_mod::TestEnum::test_function', `ty` is the `test_mod::TestEnum` expression type
19 /// and `name` is the `test_function`
20 TraitAssocItem(TraitImportCandidate),
21 /// A trait method with self parameter.
22 /// For 'test_enum.test_method()', `ty` is the `test_enum` expression type
23 /// and `name` is the `test_method`
24 TraitMethod(TraitImportCandidate),
25}
26
27#[derive(Debug)]
28pub(crate) struct TraitImportCandidate {
29 pub(crate) ty: hir::Type,
30 pub(crate) name: ast::NameRef,
31}
32
33#[derive(Debug)]
34pub(crate) struct PathImportCandidate {
35 pub(crate) name: ast::NameRef,
36}
37
38#[derive(Debug)]
39pub(crate) struct ImportAssets {
40 import_candidate: ImportCandidate,
41 module_with_name_to_import: hir::Module,
42 syntax_under_caret: SyntaxNode,
43}
44
45impl ImportAssets {
46 pub(crate) fn for_method_call(
47 method_call: ast::MethodCallExpr,
48 sema: &Semantics<RootDatabase>,
49 ) -> Option<Self> {
50 let syntax_under_caret = method_call.syntax().to_owned();
51 let module_with_name_to_import = sema.scope(&syntax_under_caret).module()?;
52 Some(Self {
53 import_candidate: ImportCandidate::for_method_call(sema, &method_call)?,
54 module_with_name_to_import,
55 syntax_under_caret,
56 })
57 }
58
59 pub(crate) fn for_regular_path(
60 path_under_caret: ast::Path,
61 sema: &Semantics<RootDatabase>,
62 ) -> Option<Self> {
63 let syntax_under_caret = path_under_caret.syntax().to_owned();
64 if syntax_under_caret.ancestors().find_map(ast::Use::cast).is_some() {
65 return None;
66 }
67
68 let module_with_name_to_import = sema.scope(&syntax_under_caret).module()?;
69 Some(Self {
70 import_candidate: ImportCandidate::for_regular_path(sema, &path_under_caret)?,
71 module_with_name_to_import,
72 syntax_under_caret,
73 })
74 }
75
76 pub(crate) fn syntax_under_caret(&self) -> &SyntaxNode {
77 &self.syntax_under_caret
78 }
79
80 pub(crate) fn import_candidate(&self) -> &ImportCandidate {
81 &self.import_candidate
82 }
83
84 fn get_search_query(&self) -> &str {
85 match &self.import_candidate {
86 ImportCandidate::UnqualifiedName(candidate)
87 | ImportCandidate::QualifierStart(candidate) => candidate.name.text(),
88 ImportCandidate::TraitAssocItem(candidate)
89 | ImportCandidate::TraitMethod(candidate) => candidate.name.text(),
90 }
91 }
92
93 pub(crate) fn search_for_imports(
94 &self,
95 sema: &Semantics<RootDatabase>,
96 config: &InsertUseConfig,
97 ) -> Vec<(hir::ModPath, hir::ItemInNs)> {
98 let _p = profile::span("import_assists::search_for_imports");
99 self.search_for(sema, Some(config.prefix_kind))
100 }
101
102 /// This may return non-absolute paths if a part of the returned path is already imported into scope.
103 #[allow(dead_code)]
104 pub(crate) fn search_for_relative_paths(
105 &self,
106 sema: &Semantics<RootDatabase>,
107 ) -> Vec<(hir::ModPath, hir::ItemInNs)> {
108 let _p = profile::span("import_assists::search_for_relative_paths");
109 self.search_for(sema, None)
110 }
111
112 fn search_for(
113 &self,
114 sema: &Semantics<RootDatabase>,
115 prefixed: Option<hir::PrefixKind>,
116 ) -> Vec<(hir::ModPath, hir::ItemInNs)> {
117 let db = sema.db;
118 let mut trait_candidates = FxHashSet::default();
119 let current_crate = self.module_with_name_to_import.krate();
120
121 let filter = |candidate: Either<hir::ModuleDef, hir::MacroDef>| {
122 trait_candidates.clear();
123 match &self.import_candidate {
124 ImportCandidate::TraitAssocItem(trait_candidate) => {
125 let located_assoc_item = match candidate {
126 Either::Left(ModuleDef::Function(located_function)) => {
127 located_function.as_assoc_item(db)
128 }
129 Either::Left(ModuleDef::Const(located_const)) => {
130 located_const.as_assoc_item(db)
131 }
132 _ => None,
133 }
134 .map(|assoc| assoc.container(db))
135 .and_then(Self::assoc_to_trait)?;
136
137 trait_candidates.insert(located_assoc_item.into());
138
139 trait_candidate
140 .ty
141 .iterate_path_candidates(
142 db,
143 current_crate,
144 &trait_candidates,
145 None,
146 |_, assoc| Self::assoc_to_trait(assoc.container(db)),
147 )
148 .map(ModuleDef::from)
149 .map(Either::Left)
150 }
151 ImportCandidate::TraitMethod(trait_candidate) => {
152 let located_assoc_item =
153 if let Either::Left(ModuleDef::Function(located_function)) = candidate {
154 located_function
155 .as_assoc_item(db)
156 .map(|assoc| assoc.container(db))
157 .and_then(Self::assoc_to_trait)
158 } else {
159 None
160 }?;
161
162 trait_candidates.insert(located_assoc_item.into());
163
164 trait_candidate
165 .ty
166 .iterate_method_candidates(
167 db,
168 current_crate,
169 &trait_candidates,
170 None,
171 |_, function| {
172 Self::assoc_to_trait(function.as_assoc_item(db)?.container(db))
173 },
174 )
175 .map(ModuleDef::from)
176 .map(Either::Left)
177 }
178 _ => Some(candidate),
179 }
180 };
181
182 let mut res = imports_locator::find_exact_imports(
183 sema,
184 current_crate,
185 self.get_search_query().to_string(),
186 )
187 .filter_map(filter)
188 .filter_map(|candidate| {
189 let item: hir::ItemInNs = candidate.either(Into::into, Into::into);
190 if let Some(prefix_kind) = prefixed {
191 self.module_with_name_to_import.find_use_path_prefixed(db, item, prefix_kind)
192 } else {
193 self.module_with_name_to_import.find_use_path(db, item)
194 }
195 .map(|path| (path, item))
196 })
197 .filter(|(use_path, _)| use_path.len() > 1)
198 .take(20)
199 .collect::<Vec<_>>();
200 res.sort_by_key(|(path, _)| path.clone());
201 res
202 }
203
204 fn assoc_to_trait(assoc: AssocItemContainer) -> Option<hir::Trait> {
205 if let AssocItemContainer::Trait(extracted_trait) = assoc {
206 Some(extracted_trait)
207 } else {
208 None
209 }
210 }
211}
212
213impl ImportCandidate {
214 fn for_method_call(
215 sema: &Semantics<RootDatabase>,
216 method_call: &ast::MethodCallExpr,
217 ) -> Option<Self> {
218 match sema.resolve_method_call(method_call) {
219 Some(_) => None,
220 None => Some(Self::TraitMethod(TraitImportCandidate {
221 ty: sema.type_of_expr(&method_call.receiver()?)?,
222 name: method_call.name_ref()?,
223 })),
224 }
225 }
226
227 fn for_regular_path(
228 sema: &Semantics<RootDatabase>,
229 path_under_caret: &ast::Path,
230 ) -> Option<Self> {
231 if sema.resolve_path(path_under_caret).is_some() {
232 return None;
233 }
234
235 let segment = path_under_caret.segment()?;
236 let candidate = if let Some(qualifier) = path_under_caret.qualifier() {
237 let qualifier_start = qualifier.syntax().descendants().find_map(ast::NameRef::cast)?;
238 let qualifier_start_path =
239 qualifier_start.syntax().ancestors().find_map(ast::Path::cast)?;
240 if let Some(qualifier_start_resolution) = sema.resolve_path(&qualifier_start_path) {
241 let qualifier_resolution = if qualifier_start_path == qualifier {
242 qualifier_start_resolution
243 } else {
244 sema.resolve_path(&qualifier)?
245 };
246 match qualifier_resolution {
247 hir::PathResolution::Def(hir::ModuleDef::Adt(assoc_item_path)) => {
248 ImportCandidate::TraitAssocItem(TraitImportCandidate {
249 ty: assoc_item_path.ty(sema.db),
250 name: segment.name_ref()?,
251 })
252 }
253 _ => return None,
254 }
255 } else {
256 ImportCandidate::QualifierStart(PathImportCandidate { name: qualifier_start })
257 }
258 } else {
259 ImportCandidate::UnqualifiedName(PathImportCandidate {
260 name: segment.syntax().descendants().find_map(ast::NameRef::cast)?,
261 })
262 };
263 Some(candidate)
264 }
265}