diff options
Diffstat (limited to 'crates/ra_ide_api/src/completion/complete_scope.rs')
-rw-r--r-- | crates/ra_ide_api/src/completion/complete_scope.rs | 70 |
1 files changed, 36 insertions, 34 deletions
diff --git a/crates/ra_ide_api/src/completion/complete_scope.rs b/crates/ra_ide_api/src/completion/complete_scope.rs index a2523c5ef..2473e58b4 100644 --- a/crates/ra_ide_api/src/completion/complete_scope.rs +++ b/crates/ra_ide_api/src/completion/complete_scope.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use rustc_hash::FxHashMap; | 1 | use rustc_hash::FxHashMap; |
2 | use ra_text_edit::TextEditBuilder; | 2 | use ra_text_edit::TextEditBuilder; |
3 | use ra_syntax::SmolStr; | 3 | use ra_syntax::{SmolStr, ast, AstNode}; |
4 | use ra_assists::auto_import; | 4 | use ra_assists::auto_import; |
5 | 5 | ||
6 | use crate::completion::{CompletionItem, Completions, CompletionKind, CompletionContext}; | 6 | use crate::completion::{CompletionItem, Completions, CompletionKind, CompletionContext}; |
@@ -9,41 +9,43 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) { | |||
9 | if ctx.is_trivial_path { | 9 | if ctx.is_trivial_path { |
10 | let names = ctx.analyzer.all_names(ctx.db); | 10 | let names = ctx.analyzer.all_names(ctx.db); |
11 | names.into_iter().for_each(|(name, res)| acc.add_resolution(ctx, name.to_string(), &res)); | 11 | names.into_iter().for_each(|(name, res)| acc.add_resolution(ctx, name.to_string(), &res)); |
12 | } | ||
13 | 12 | ||
14 | if let Some(name) = ctx.path_ident.as_ref() { | 13 | // auto-import |
15 | let import_resolver = ImportResolver::new(); | 14 | // We fetch ident from the original file, because we need to pre-filter auto-imports |
16 | let import_names = import_resolver.all_names(&name.to_string()); | 15 | if ast::NameRef::cast(ctx.token.parent()).is_some() { |
17 | import_names.into_iter().for_each(|(name, path)| { | 16 | let import_resolver = ImportResolver::new(); |
18 | let edit = { | 17 | let import_names = import_resolver.all_names(ctx.token.text()); |
19 | let mut builder = TextEditBuilder::default(); | 18 | import_names.into_iter().for_each(|(name, path)| { |
20 | builder.replace(ctx.source_range(), name.to_string()); | 19 | let edit = { |
21 | auto_import::auto_import_text_edit( | 20 | let mut builder = TextEditBuilder::default(); |
22 | ctx.token.parent(), | 21 | builder.replace(ctx.source_range(), name.to_string()); |
23 | ctx.token.parent(), | 22 | auto_import::auto_import_text_edit( |
24 | &path, | 23 | ctx.token.parent(), |
25 | &mut builder, | 24 | ctx.token.parent(), |
26 | ); | 25 | &path, |
27 | builder.finish() | 26 | &mut builder, |
28 | }; | 27 | ); |
28 | builder.finish() | ||
29 | }; | ||
29 | 30 | ||
30 | // Hack: copied this check form conv.rs beacause auto import can produce edits | 31 | // Hack: copied this check form conv.rs beacause auto import can produce edits |
31 | // that invalidate assert in conv_with. | 32 | // that invalidate assert in conv_with. |
32 | if edit | 33 | if edit |
33 | .as_atoms() | 34 | .as_atoms() |
34 | .iter() | 35 | .iter() |
35 | .filter(|atom| !ctx.source_range().is_subrange(&atom.delete)) | 36 | .filter(|atom| !ctx.source_range().is_subrange(&atom.delete)) |
36 | .all(|atom| ctx.source_range().intersection(&atom.delete).is_none()) | 37 | .all(|atom| ctx.source_range().intersection(&atom.delete).is_none()) |
37 | { | 38 | { |
38 | CompletionItem::new( | 39 | CompletionItem::new( |
39 | CompletionKind::Reference, | 40 | CompletionKind::Reference, |
40 | ctx.source_range(), | 41 | ctx.source_range(), |
41 | build_import_label(&name, &path), | 42 | build_import_label(&name, &path), |
42 | ) | 43 | ) |
43 | .text_edit(edit) | 44 | .text_edit(edit) |
44 | .add_to(acc); | 45 | .add_to(acc); |
45 | } | 46 | } |
46 | }); | 47 | }); |
48 | } | ||
47 | } | 49 | } |
48 | } | 50 | } |
49 | 51 | ||