diff options
author | Aleksey Kladov <[email protected]> | 2019-04-22 14:04:56 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-04-22 14:04:56 +0100 |
commit | c27a3da5e86b1fcbfb562e0723fb97d72268fa72 (patch) | |
tree | 1a78d5e88244a4689cfd71cfadf76b0830a1280d /crates/ra_ide_api | |
parent | e01052d1f0b8bf70a418a10538528923c5f400d5 (diff) |
remove path_ident from CompletionContext
We really shouldn't be looking at the identifier at point. Instead,
all filtering and sorting should be implemented at the layer above.
This layer should probably be home for auto-import completions as
well, but, since that is not yet implemented, let's just stick this
into complete_scope.
Diffstat (limited to 'crates/ra_ide_api')
-rw-r--r-- | crates/ra_ide_api/src/completion/complete_scope.rs | 70 | ||||
-rw-r--r-- | crates/ra_ide_api/src/completion/completion_context.rs | 7 |
2 files changed, 38 insertions, 39 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 | ||
diff --git a/crates/ra_ide_api/src/completion/completion_context.rs b/crates/ra_ide_api/src/completion/completion_context.rs index 0d630fdf6..a8c8cc7b0 100644 --- a/crates/ra_ide_api/src/completion/completion_context.rs +++ b/crates/ra_ide_api/src/completion/completion_context.rs | |||
@@ -5,10 +5,10 @@ use ra_syntax::{ | |||
5 | algo::{find_token_at_offset, find_covering_element, find_node_at_offset}, | 5 | algo::{find_token_at_offset, find_covering_element, find_node_at_offset}, |
6 | SyntaxKind::*, | 6 | SyntaxKind::*, |
7 | }; | 7 | }; |
8 | 8 | use hir::source_binder; | |
9 | use hir::{ source_binder, Name }; | ||
10 | 9 | ||
11 | use crate::{db, FilePosition}; | 10 | use crate::{db, FilePosition}; |
11 | |||
12 | /// `CompletionContext` is created early during completion to figure out, where | 12 | /// `CompletionContext` is created early during completion to figure out, where |
13 | /// exactly is the cursor, syntax-wise. | 13 | /// exactly is the cursor, syntax-wise. |
14 | #[derive(Debug)] | 14 | #[derive(Debug)] |
@@ -29,8 +29,6 @@ pub(crate) struct CompletionContext<'a> { | |||
29 | pub(super) is_trivial_path: bool, | 29 | pub(super) is_trivial_path: bool, |
30 | /// If not a trivial path, the prefix (qualifier). | 30 | /// If not a trivial path, the prefix (qualifier). |
31 | pub(super) path_prefix: Option<hir::Path>, | 31 | pub(super) path_prefix: Option<hir::Path>, |
32 | /// If a trivial path, the ident. | ||
33 | pub(super) path_ident: Option<Name>, | ||
34 | pub(super) after_if: bool, | 32 | pub(super) after_if: bool, |
35 | /// `true` if we are a statement or a last expr in the block. | 33 | /// `true` if we are a statement or a last expr in the block. |
36 | pub(super) can_be_stmt: bool, | 34 | pub(super) can_be_stmt: bool, |
@@ -65,7 +63,6 @@ impl<'a> CompletionContext<'a> { | |||
65 | is_pat_binding: false, | 63 | is_pat_binding: false, |
66 | is_trivial_path: false, | 64 | is_trivial_path: false, |
67 | path_prefix: None, | 65 | path_prefix: None, |
68 | path_ident: None, | ||
69 | after_if: false, | 66 | after_if: false, |
70 | can_be_stmt: false, | 67 | can_be_stmt: false, |
71 | is_new_item: false, | 68 | is_new_item: false, |