diff options
author | Andrea Pretto <[email protected]> | 2019-04-15 15:11:32 +0100 |
---|---|---|
committer | Andrea Pretto <[email protected]> | 2019-04-21 23:14:58 +0100 |
commit | 914421495835380b96e0016763fda6eff31a8179 (patch) | |
tree | 86b348e13e9272f1320aad0416313c0f6f93a19c /crates/ra_ide_api/src/completion/completion_context.rs | |
parent | cf0eff2e332f46eda4fcecb043854c9c0d710e4e (diff) |
complete_import: add new import resolver infrastructure with some hardcoded importable name.
Changes complete_scope to support that.
Diffstat (limited to 'crates/ra_ide_api/src/completion/completion_context.rs')
-rw-r--r-- | crates/ra_ide_api/src/completion/completion_context.rs | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/crates/ra_ide_api/src/completion/completion_context.rs b/crates/ra_ide_api/src/completion/completion_context.rs index 359f2cffa..ca8f7900d 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 | use hir::source_binder; | ||
9 | 8 | ||
10 | use crate::{db, FilePosition}; | 9 | use hir::{ source_binder, Name }; |
11 | 10 | ||
11 | use crate::{db, FilePosition}; | ||
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)] |
@@ -27,8 +27,10 @@ pub(crate) struct CompletionContext<'a> { | |||
27 | pub(super) is_pat_binding: bool, | 27 | pub(super) is_pat_binding: bool, |
28 | /// A single-indent path, like `foo`. `::foo` should not be considered a trivial path. | 28 | /// A single-indent path, like `foo`. `::foo` should not be considered a trivial path. |
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>, | ||
32 | pub(super) after_if: bool, | 34 | pub(super) after_if: bool, |
33 | /// `true` if we are a statement or a last expr in the block. | 35 | /// `true` if we are a statement or a last expr in the block. |
34 | pub(super) can_be_stmt: bool, | 36 | pub(super) can_be_stmt: bool, |
@@ -63,6 +65,7 @@ impl<'a> CompletionContext<'a> { | |||
63 | is_pat_binding: false, | 65 | is_pat_binding: false, |
64 | is_trivial_path: false, | 66 | is_trivial_path: false, |
65 | path_prefix: None, | 67 | path_prefix: None, |
68 | path_ident: None, | ||
66 | after_if: false, | 69 | after_if: false, |
67 | can_be_stmt: false, | 70 | can_be_stmt: false, |
68 | is_new_item: false, | 71 | is_new_item: false, |
@@ -83,6 +86,18 @@ impl<'a> CompletionContext<'a> { | |||
83 | } | 86 | } |
84 | 87 | ||
85 | fn fill(&mut self, original_file: &'a SourceFile, offset: TextUnit) { | 88 | fn fill(&mut self, original_file: &'a SourceFile, offset: TextUnit) { |
89 | // We heed the original NameRef before the "intellijRulezz" hack | ||
90 | if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(original_file.syntax(), offset) | ||
91 | { | ||
92 | if let Some(path) = name_ref.syntax().ancestors().find_map(ast::Path::cast) { | ||
93 | if let Some(path) = hir::Path::from_ast(path) { | ||
94 | if let Some(ident) = path.as_ident() { | ||
95 | self.path_ident = Some(ident.clone()); | ||
96 | } | ||
97 | } | ||
98 | } | ||
99 | } | ||
100 | |||
86 | // Insert a fake ident to get a valid parse tree. We will use this file | 101 | // Insert a fake ident to get a valid parse tree. We will use this file |
87 | // to determine context, though the original_file will be used for | 102 | // to determine context, though the original_file will be used for |
88 | // actual completion. | 103 | // actual completion. |
@@ -151,6 +166,7 @@ impl<'a> CompletionContext<'a> { | |||
151 | Some(it) => it, | 166 | Some(it) => it, |
152 | None => return, | 167 | None => return, |
153 | }; | 168 | }; |
169 | |||
154 | if let Some(segment) = ast::PathSegment::cast(parent) { | 170 | if let Some(segment) = ast::PathSegment::cast(parent) { |
155 | let path = segment.parent_path(); | 171 | let path = segment.parent_path(); |
156 | self.is_call = path | 172 | self.is_call = path |
@@ -167,6 +183,7 @@ impl<'a> CompletionContext<'a> { | |||
167 | return; | 183 | return; |
168 | } | 184 | } |
169 | } | 185 | } |
186 | |||
170 | if path.qualifier().is_none() { | 187 | if path.qualifier().is_none() { |
171 | self.is_trivial_path = true; | 188 | self.is_trivial_path = true; |
172 | 189 | ||