diff options
Diffstat (limited to 'crates/assists/src/handlers')
-rw-r--r-- | crates/assists/src/handlers/add_turbo_fish.rs | 4 | ||||
-rw-r--r-- | crates/assists/src/handlers/auto_import.rs | 324 | ||||
-rw-r--r-- | crates/assists/src/handlers/expand_glob_import.rs | 4 | ||||
-rw-r--r-- | crates/assists/src/handlers/merge_imports.rs | 14 | ||||
-rw-r--r-- | crates/assists/src/handlers/replace_qualified_name_with_use.rs | 17 |
5 files changed, 118 insertions, 245 deletions
diff --git a/crates/assists/src/handlers/add_turbo_fish.rs b/crates/assists/src/handlers/add_turbo_fish.rs index f4f997d8e..e3d84d698 100644 --- a/crates/assists/src/handlers/add_turbo_fish.rs +++ b/crates/assists/src/handlers/add_turbo_fish.rs | |||
@@ -1,4 +1,4 @@ | |||
1 | use ide_db::defs::{classify_name_ref, Definition, NameRefClass}; | 1 | use ide_db::defs::{Definition, NameRefClass}; |
2 | use syntax::{ast, AstNode, SyntaxKind, T}; | 2 | use syntax::{ast, AstNode, SyntaxKind, T}; |
3 | use test_utils::mark; | 3 | use test_utils::mark; |
4 | 4 | ||
@@ -39,7 +39,7 @@ pub(crate) fn add_turbo_fish(acc: &mut Assists, ctx: &AssistContext) -> Option<( | |||
39 | return None; | 39 | return None; |
40 | } | 40 | } |
41 | let name_ref = ast::NameRef::cast(ident.parent())?; | 41 | let name_ref = ast::NameRef::cast(ident.parent())?; |
42 | let def = match classify_name_ref(&ctx.sema, &name_ref)? { | 42 | let def = match NameRefClass::classify(&ctx.sema, &name_ref)? { |
43 | NameRefClass::Definition(def) => def, | 43 | NameRefClass::Definition(def) => def, |
44 | NameRefClass::ExternCrate(_) | NameRefClass::FieldShorthand { .. } => return None, | 44 | NameRefClass::ExternCrate(_) | NameRefClass::FieldShorthand { .. } => return None, |
45 | }; | 45 | }; |
diff --git a/crates/assists/src/handlers/auto_import.rs b/crates/assists/src/handlers/auto_import.rs index d3ee98e5f..4a7059c83 100644 --- a/crates/assists/src/handlers/auto_import.rs +++ b/crates/assists/src/handlers/auto_import.rs | |||
@@ -1,23 +1,66 @@ | |||
1 | use std::collections::BTreeSet; | 1 | use syntax::ast; |
2 | |||
3 | use either::Either; | ||
4 | use hir::{ | ||
5 | AsAssocItem, AssocItemContainer, ModPath, Module, ModuleDef, PathResolution, Semantics, Trait, | ||
6 | Type, | ||
7 | }; | ||
8 | use ide_db::{imports_locator, RootDatabase}; | ||
9 | use insert_use::ImportScope; | ||
10 | use rustc_hash::FxHashSet; | ||
11 | use syntax::{ | ||
12 | ast::{self, AstNode}, | ||
13 | SyntaxNode, | ||
14 | }; | ||
15 | 2 | ||
16 | use crate::{ | 3 | use crate::{ |
17 | utils::insert_use, utils::mod_path_to_ast, AssistContext, AssistId, AssistKind, Assists, | 4 | utils::import_assets::{ImportAssets, ImportCandidate}, |
18 | GroupLabel, | 5 | utils::{insert_use, mod_path_to_ast, ImportScope}, |
6 | AssistContext, AssistId, AssistKind, Assists, GroupLabel, | ||
19 | }; | 7 | }; |
20 | 8 | ||
9 | // Feature: Import Insertion | ||
10 | // | ||
11 | // Using the `auto-import` assist it is possible to insert missing imports for unresolved items. | ||
12 | // When inserting an import it will do so in a structured manner by keeping imports grouped, | ||
13 | // separated by a newline in the following order: | ||
14 | // | ||
15 | // - `std` and `core` | ||
16 | // - External Crates | ||
17 | // - Current Crate, paths prefixed by `crate` | ||
18 | // - Current Module, paths prefixed by `self` | ||
19 | // - Super Module, paths prefixed by `super` | ||
20 | // | ||
21 | // Example: | ||
22 | // ```rust | ||
23 | // use std::fs::File; | ||
24 | // | ||
25 | // use itertools::Itertools; | ||
26 | // use syntax::ast; | ||
27 | // | ||
28 | // use crate::utils::insert_use; | ||
29 | // | ||
30 | // use self::auto_import; | ||
31 | // | ||
32 | // use super::AssistContext; | ||
33 | // ``` | ||
34 | // | ||
35 | // .Merge Behaviour | ||
36 | // | ||
37 | // It is possible to configure how use-trees are merged with the `importMergeBehaviour` setting. | ||
38 | // It has the following configurations: | ||
39 | // | ||
40 | // - `full`: This setting will cause auto-import to always completely merge use-trees that share the | ||
41 | // same path prefix while also merging inner trees that share the same path-prefix. This kind of | ||
42 | // nesting is only supported in Rust versions later than 1.24. | ||
43 | // - `last`: This setting will cause auto-import to merge use-trees as long as the resulting tree | ||
44 | // will only contain a nesting of single segment paths at the very end. | ||
45 | // - `none`: This setting will cause auto-import to never merge use-trees keeping them as simple | ||
46 | // paths. | ||
47 | // | ||
48 | // In `VS Code` the configuration for this is `rust-analyzer.assist.importMergeBehaviour`. | ||
49 | // | ||
50 | // .Import Prefix | ||
51 | // | ||
52 | // The style of imports in the same crate is configurable through the `importPrefix` setting. | ||
53 | // It has the following configurations: | ||
54 | // | ||
55 | // - `by_crate`: This setting will force paths to be always absolute, starting with the `crate` | ||
56 | // prefix, unless the item is defined outside of the current crate. | ||
57 | // - `by_self`: This setting will force paths that are relative to the current module to always | ||
58 | // start with `self`. This will result in paths that always start with either `crate`, `self`, | ||
59 | // `super` or an extern crate identifier. | ||
60 | // - `plain`: This setting does not impose any restrictions in imports. | ||
61 | // | ||
62 | // In `VS Code` the configuration for this is `rust-analyzer.assist.importPrefix`. | ||
63 | |||
21 | // Assist: auto_import | 64 | // Assist: auto_import |
22 | // | 65 | // |
23 | // If the name is unresolved, provides all possible imports for it. | 66 | // If the name is unresolved, provides all possible imports for it. |
@@ -38,16 +81,24 @@ use crate::{ | |||
38 | // # pub mod std { pub mod collections { pub struct HashMap { } } } | 81 | // # pub mod std { pub mod collections { pub struct HashMap { } } } |
39 | // ``` | 82 | // ``` |
40 | pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | 83 | pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { |
41 | let auto_import_assets = AutoImportAssets::new(ctx)?; | 84 | let import_assets = |
42 | let proposed_imports = auto_import_assets.search_for_imports(ctx); | 85 | if let Some(path_under_caret) = ctx.find_node_at_offset_with_descend::<ast::Path>() { |
86 | ImportAssets::for_regular_path(path_under_caret, &ctx.sema) | ||
87 | } else if let Some(method_under_caret) = | ||
88 | ctx.find_node_at_offset_with_descend::<ast::MethodCallExpr>() | ||
89 | { | ||
90 | ImportAssets::for_method_call(method_under_caret, &ctx.sema) | ||
91 | } else { | ||
92 | None | ||
93 | }?; | ||
94 | let proposed_imports = import_assets.search_for_imports(&ctx.sema, &ctx.config.insert_use); | ||
43 | if proposed_imports.is_empty() { | 95 | if proposed_imports.is_empty() { |
44 | return None; | 96 | return None; |
45 | } | 97 | } |
46 | 98 | ||
47 | let range = ctx.sema.original_range(&auto_import_assets.syntax_under_caret).range; | 99 | let range = ctx.sema.original_range(import_assets.syntax_under_caret()).range; |
48 | let group = auto_import_assets.get_import_group_message(); | 100 | let group = import_group_message(import_assets.import_candidate()); |
49 | let scope = | 101 | let scope = ImportScope::find_insert_use_container(import_assets.syntax_under_caret(), ctx)?; |
50 | ImportScope::find_insert_use_container(&auto_import_assets.syntax_under_caret, ctx)?; | ||
51 | let syntax = scope.as_syntax_node(); | 102 | let syntax = scope.as_syntax_node(); |
52 | for import in proposed_imports { | 103 | for import in proposed_imports { |
53 | acc.add_group( | 104 | acc.add_group( |
@@ -65,227 +116,18 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> | |||
65 | Some(()) | 116 | Some(()) |
66 | } | 117 | } |
67 | 118 | ||
68 | #[derive(Debug)] | 119 | fn import_group_message(import_candidate: &ImportCandidate) -> GroupLabel { |
69 | struct AutoImportAssets { | 120 | let name = match import_candidate { |
70 | import_candidate: ImportCandidate, | 121 | ImportCandidate::UnqualifiedName(candidate) |
71 | module_with_name_to_import: Module, | 122 | | ImportCandidate::QualifierStart(candidate) => format!("Import {}", &candidate.name), |
72 | syntax_under_caret: SyntaxNode, | 123 | ImportCandidate::TraitAssocItem(candidate) => { |
73 | } | 124 | format!("Import a trait for item {}", &candidate.name) |
74 | |||
75 | impl AutoImportAssets { | ||
76 | fn new(ctx: &AssistContext) -> Option<Self> { | ||
77 | if let Some(path_under_caret) = ctx.find_node_at_offset_with_descend::<ast::Path>() { | ||
78 | Self::for_regular_path(path_under_caret, &ctx) | ||
79 | } else { | ||
80 | Self::for_method_call(ctx.find_node_at_offset_with_descend()?, &ctx) | ||
81 | } | 125 | } |
82 | } | 126 | ImportCandidate::TraitMethod(candidate) => { |
83 | 127 | format!("Import a trait for method {}", &candidate.name) | |
84 | fn for_method_call(method_call: ast::MethodCallExpr, ctx: &AssistContext) -> Option<Self> { | ||
85 | let syntax_under_caret = method_call.syntax().to_owned(); | ||
86 | let module_with_name_to_import = ctx.sema.scope(&syntax_under_caret).module()?; | ||
87 | Some(Self { | ||
88 | import_candidate: ImportCandidate::for_method_call(&ctx.sema, &method_call)?, | ||
89 | module_with_name_to_import, | ||
90 | syntax_under_caret, | ||
91 | }) | ||
92 | } | ||
93 | |||
94 | fn for_regular_path(path_under_caret: ast::Path, ctx: &AssistContext) -> Option<Self> { | ||
95 | let syntax_under_caret = path_under_caret.syntax().to_owned(); | ||
96 | if syntax_under_caret.ancestors().find_map(ast::Use::cast).is_some() { | ||
97 | return None; | ||
98 | } | 128 | } |
99 | 129 | }; | |
100 | let module_with_name_to_import = ctx.sema.scope(&syntax_under_caret).module()?; | 130 | GroupLabel(name) |
101 | Some(Self { | ||
102 | import_candidate: ImportCandidate::for_regular_path(&ctx.sema, &path_under_caret)?, | ||
103 | module_with_name_to_import, | ||
104 | syntax_under_caret, | ||
105 | }) | ||
106 | } | ||
107 | |||
108 | fn get_search_query(&self) -> &str { | ||
109 | match &self.import_candidate { | ||
110 | ImportCandidate::UnqualifiedName(name) => name, | ||
111 | ImportCandidate::QualifierStart(qualifier_start) => qualifier_start, | ||
112 | ImportCandidate::TraitAssocItem(_, trait_assoc_item_name) => trait_assoc_item_name, | ||
113 | ImportCandidate::TraitMethod(_, trait_method_name) => trait_method_name, | ||
114 | } | ||
115 | } | ||
116 | |||
117 | fn get_import_group_message(&self) -> GroupLabel { | ||
118 | let name = match &self.import_candidate { | ||
119 | ImportCandidate::UnqualifiedName(name) => format!("Import {}", name), | ||
120 | ImportCandidate::QualifierStart(qualifier_start) => { | ||
121 | format!("Import {}", qualifier_start) | ||
122 | } | ||
123 | ImportCandidate::TraitAssocItem(_, trait_assoc_item_name) => { | ||
124 | format!("Import a trait for item {}", trait_assoc_item_name) | ||
125 | } | ||
126 | ImportCandidate::TraitMethod(_, trait_method_name) => { | ||
127 | format!("Import a trait for method {}", trait_method_name) | ||
128 | } | ||
129 | }; | ||
130 | GroupLabel(name) | ||
131 | } | ||
132 | |||
133 | fn search_for_imports(&self, ctx: &AssistContext) -> BTreeSet<ModPath> { | ||
134 | let _p = profile::span("auto_import::search_for_imports"); | ||
135 | let db = ctx.db(); | ||
136 | let current_crate = self.module_with_name_to_import.krate(); | ||
137 | imports_locator::find_imports(&ctx.sema, current_crate, &self.get_search_query()) | ||
138 | .into_iter() | ||
139 | .filter_map(|candidate| match &self.import_candidate { | ||
140 | ImportCandidate::TraitAssocItem(assoc_item_type, _) => { | ||
141 | let located_assoc_item = match candidate { | ||
142 | Either::Left(ModuleDef::Function(located_function)) => located_function | ||
143 | .as_assoc_item(db) | ||
144 | .map(|assoc| assoc.container(db)) | ||
145 | .and_then(Self::assoc_to_trait), | ||
146 | Either::Left(ModuleDef::Const(located_const)) => located_const | ||
147 | .as_assoc_item(db) | ||
148 | .map(|assoc| assoc.container(db)) | ||
149 | .and_then(Self::assoc_to_trait), | ||
150 | _ => None, | ||
151 | }?; | ||
152 | |||
153 | let mut trait_candidates = FxHashSet::default(); | ||
154 | trait_candidates.insert(located_assoc_item.into()); | ||
155 | |||
156 | assoc_item_type | ||
157 | .iterate_path_candidates( | ||
158 | db, | ||
159 | current_crate, | ||
160 | &trait_candidates, | ||
161 | None, | ||
162 | |_, assoc| Self::assoc_to_trait(assoc.container(db)), | ||
163 | ) | ||
164 | .map(ModuleDef::from) | ||
165 | .map(Either::Left) | ||
166 | } | ||
167 | ImportCandidate::TraitMethod(function_callee, _) => { | ||
168 | let located_assoc_item = | ||
169 | if let Either::Left(ModuleDef::Function(located_function)) = candidate { | ||
170 | located_function | ||
171 | .as_assoc_item(db) | ||
172 | .map(|assoc| assoc.container(db)) | ||
173 | .and_then(Self::assoc_to_trait) | ||
174 | } else { | ||
175 | None | ||
176 | }?; | ||
177 | |||
178 | let mut trait_candidates = FxHashSet::default(); | ||
179 | trait_candidates.insert(located_assoc_item.into()); | ||
180 | |||
181 | function_callee | ||
182 | .iterate_method_candidates( | ||
183 | db, | ||
184 | current_crate, | ||
185 | &trait_candidates, | ||
186 | None, | ||
187 | |_, function| { | ||
188 | Self::assoc_to_trait(function.as_assoc_item(db)?.container(db)) | ||
189 | }, | ||
190 | ) | ||
191 | .map(ModuleDef::from) | ||
192 | .map(Either::Left) | ||
193 | } | ||
194 | _ => Some(candidate), | ||
195 | }) | ||
196 | .filter_map(|candidate| match candidate { | ||
197 | Either::Left(module_def) => self.module_with_name_to_import.find_use_path_prefixed( | ||
198 | db, | ||
199 | module_def, | ||
200 | ctx.config.insert_use.prefix_kind, | ||
201 | ), | ||
202 | Either::Right(macro_def) => self.module_with_name_to_import.find_use_path_prefixed( | ||
203 | db, | ||
204 | macro_def, | ||
205 | ctx.config.insert_use.prefix_kind, | ||
206 | ), | ||
207 | }) | ||
208 | .filter(|use_path| !use_path.segments.is_empty()) | ||
209 | .take(20) | ||
210 | .collect::<BTreeSet<_>>() | ||
211 | } | ||
212 | |||
213 | fn assoc_to_trait(assoc: AssocItemContainer) -> Option<Trait> { | ||
214 | if let AssocItemContainer::Trait(extracted_trait) = assoc { | ||
215 | Some(extracted_trait) | ||
216 | } else { | ||
217 | None | ||
218 | } | ||
219 | } | ||
220 | } | ||
221 | |||
222 | #[derive(Debug)] | ||
223 | enum ImportCandidate { | ||
224 | /// Simple name like 'HashMap' | ||
225 | UnqualifiedName(String), | ||
226 | /// First part of the qualified name. | ||
227 | /// For 'std::collections::HashMap', that will be 'std'. | ||
228 | QualifierStart(String), | ||
229 | /// A trait associated function (with no self parameter) or associated constant. | ||
230 | /// For 'test_mod::TestEnum::test_function', `Type` is the `test_mod::TestEnum` expression type | ||
231 | /// and `String` is the `test_function` | ||
232 | TraitAssocItem(Type, String), | ||
233 | /// A trait method with self parameter. | ||
234 | /// For 'test_enum.test_method()', `Type` is the `test_enum` expression type | ||
235 | /// and `String` is the `test_method` | ||
236 | TraitMethod(Type, String), | ||
237 | } | ||
238 | |||
239 | impl ImportCandidate { | ||
240 | fn for_method_call( | ||
241 | sema: &Semantics<RootDatabase>, | ||
242 | method_call: &ast::MethodCallExpr, | ||
243 | ) -> Option<Self> { | ||
244 | if sema.resolve_method_call(method_call).is_some() { | ||
245 | return None; | ||
246 | } | ||
247 | Some(Self::TraitMethod( | ||
248 | sema.type_of_expr(&method_call.receiver()?)?, | ||
249 | method_call.name_ref()?.syntax().to_string(), | ||
250 | )) | ||
251 | } | ||
252 | |||
253 | fn for_regular_path( | ||
254 | sema: &Semantics<RootDatabase>, | ||
255 | path_under_caret: &ast::Path, | ||
256 | ) -> Option<Self> { | ||
257 | if sema.resolve_path(path_under_caret).is_some() { | ||
258 | return None; | ||
259 | } | ||
260 | |||
261 | let segment = path_under_caret.segment()?; | ||
262 | if let Some(qualifier) = path_under_caret.qualifier() { | ||
263 | let qualifier_start = qualifier.syntax().descendants().find_map(ast::NameRef::cast)?; | ||
264 | let qualifier_start_path = | ||
265 | qualifier_start.syntax().ancestors().find_map(ast::Path::cast)?; | ||
266 | if let Some(qualifier_start_resolution) = sema.resolve_path(&qualifier_start_path) { | ||
267 | let qualifier_resolution = if qualifier_start_path == qualifier { | ||
268 | qualifier_start_resolution | ||
269 | } else { | ||
270 | sema.resolve_path(&qualifier)? | ||
271 | }; | ||
272 | if let PathResolution::Def(ModuleDef::Adt(assoc_item_path)) = qualifier_resolution { | ||
273 | Some(ImportCandidate::TraitAssocItem( | ||
274 | assoc_item_path.ty(sema.db), | ||
275 | segment.syntax().to_string(), | ||
276 | )) | ||
277 | } else { | ||
278 | None | ||
279 | } | ||
280 | } else { | ||
281 | Some(ImportCandidate::QualifierStart(qualifier_start.syntax().to_string())) | ||
282 | } | ||
283 | } else { | ||
284 | Some(ImportCandidate::UnqualifiedName( | ||
285 | segment.syntax().descendants().find_map(ast::NameRef::cast)?.syntax().to_string(), | ||
286 | )) | ||
287 | } | ||
288 | } | ||
289 | } | 131 | } |
290 | 132 | ||
291 | #[cfg(test)] | 133 | #[cfg(test)] |
diff --git a/crates/assists/src/handlers/expand_glob_import.rs b/crates/assists/src/handlers/expand_glob_import.rs index d1adff972..316a58d88 100644 --- a/crates/assists/src/handlers/expand_glob_import.rs +++ b/crates/assists/src/handlers/expand_glob_import.rs | |||
@@ -1,7 +1,7 @@ | |||
1 | use either::Either; | 1 | use either::Either; |
2 | use hir::{AssocItem, MacroDef, Module, ModuleDef, Name, PathResolution, ScopeDef}; | 2 | use hir::{AssocItem, MacroDef, Module, ModuleDef, Name, PathResolution, ScopeDef}; |
3 | use ide_db::{ | 3 | use ide_db::{ |
4 | defs::{classify_name_ref, Definition, NameRefClass}, | 4 | defs::{Definition, NameRefClass}, |
5 | search::SearchScope, | 5 | search::SearchScope, |
6 | }; | 6 | }; |
7 | use syntax::{ | 7 | use syntax::{ |
@@ -217,7 +217,7 @@ fn find_imported_defs(ctx: &AssistContext, star: SyntaxToken) -> Option<Vec<Def> | |||
217 | .flatten() | 217 | .flatten() |
218 | .filter_map(|n| Some(n.descendants().filter_map(ast::NameRef::cast))) | 218 | .filter_map(|n| Some(n.descendants().filter_map(ast::NameRef::cast))) |
219 | .flatten() | 219 | .flatten() |
220 | .filter_map(|r| match classify_name_ref(&ctx.sema, &r)? { | 220 | .filter_map(|r| match NameRefClass::classify(&ctx.sema, &r)? { |
221 | NameRefClass::Definition(Definition::ModuleDef(def)) => Some(Def::ModuleDef(def)), | 221 | NameRefClass::Definition(Definition::ModuleDef(def)) => Some(Def::ModuleDef(def)), |
222 | NameRefClass::Definition(Definition::Macro(def)) => Some(Def::MacroDef(def)), | 222 | NameRefClass::Definition(Definition::Macro(def)) => Some(Def::MacroDef(def)), |
223 | _ => None, | 223 | _ => None, |
diff --git a/crates/assists/src/handlers/merge_imports.rs b/crates/assists/src/handlers/merge_imports.rs index fe33cee53..fd9c9e03c 100644 --- a/crates/assists/src/handlers/merge_imports.rs +++ b/crates/assists/src/handlers/merge_imports.rs | |||
@@ -73,6 +73,20 @@ mod tests { | |||
73 | use super::*; | 73 | use super::*; |
74 | 74 | ||
75 | #[test] | 75 | #[test] |
76 | fn test_merge_equal() { | ||
77 | check_assist( | ||
78 | merge_imports, | ||
79 | r" | ||
80 | use std::fmt<|>::{Display, Debug}; | ||
81 | use std::fmt::{Display, Debug}; | ||
82 | ", | ||
83 | r" | ||
84 | use std::fmt::{Debug, Display}; | ||
85 | ", | ||
86 | ) | ||
87 | } | ||
88 | |||
89 | #[test] | ||
76 | fn test_merge_first() { | 90 | fn test_merge_first() { |
77 | check_assist( | 91 | check_assist( |
78 | merge_imports, | 92 | merge_imports, |
diff --git a/crates/assists/src/handlers/replace_qualified_name_with_use.rs b/crates/assists/src/handlers/replace_qualified_name_with_use.rs index e95b971a4..c50bc7604 100644 --- a/crates/assists/src/handlers/replace_qualified_name_with_use.rs +++ b/crates/assists/src/handlers/replace_qualified_name_with_use.rs | |||
@@ -124,6 +124,23 @@ mod tests { | |||
124 | use super::*; | 124 | use super::*; |
125 | 125 | ||
126 | #[test] | 126 | #[test] |
127 | fn test_replace_already_imported() { | ||
128 | check_assist( | ||
129 | replace_qualified_name_with_use, | ||
130 | r"use std::fs; | ||
131 | |||
132 | fn main() { | ||
133 | std::f<|>s::Path | ||
134 | }", | ||
135 | r"use std::fs; | ||
136 | |||
137 | fn main() { | ||
138 | fs::Path | ||
139 | }", | ||
140 | ) | ||
141 | } | ||
142 | |||
143 | #[test] | ||
127 | fn test_replace_add_use_no_anchor() { | 144 | fn test_replace_add_use_no_anchor() { |
128 | check_assist( | 145 | check_assist( |
129 | replace_qualified_name_with_use, | 146 | replace_qualified_name_with_use, |