aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/completion/completion_context.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-10-17 11:31:37 +0100
committerGitHub <[email protected]>2020-10-17 11:31:37 +0100
commitb1315be010f0d9f67ee4794cb57f7d1baa88a30d (patch)
treeb4259ce1810c3cd6c2bea62bae499415b764728d /crates/ide/src/completion/completion_context.rs
parent59483c217662fc5d89ef9da1cb93760e14a48418 (diff)
parent99c435939c941fe5216c39bc136769098abbbfea (diff)
Merge #6262
6262: Do not spawn redundant hints r=SomeoneToIgnore a=popzxc Closes #5206 This is a second part of the fix (first was #5997). This PR adds a new method to the `CompletionContext`: `no_completion_required`. If this method returns `true`, it essentially means that user is unlikely to expect any hints from the IDE at this cursor position. Currently, checks for the following cases were added: - Previous item is `fn`: user creates a new function, names of existing functions won't be helpful. Exception for this case is `impl Foo for Bar` -- we must suggest trait function names. - User entered `for _ i<|>`: it's obviously going to be `in` keyword, any hints here will be confusing. More checks may be added there later, but currently I've only figured two cases. ![no_redundant_hints](https://user-images.githubusercontent.com/12111581/96332088-da4d2a00-106a-11eb-89a1-1159ece18f9d.png) Co-authored-by: Igor Aleksanov <[email protected]>
Diffstat (limited to 'crates/ide/src/completion/completion_context.rs')
-rw-r--r--crates/ide/src/completion/completion_context.rs27
1 files changed, 23 insertions, 4 deletions
diff --git a/crates/ide/src/completion/completion_context.rs b/crates/ide/src/completion/completion_context.rs
index 8dea8a4bf..d9f90477c 100644
--- a/crates/ide/src/completion/completion_context.rs
+++ b/crates/ide/src/completion/completion_context.rs
@@ -16,10 +16,11 @@ use crate::{
16 call_info::ActiveParameter, 16 call_info::ActiveParameter,
17 completion::{ 17 completion::{
18 patterns::{ 18 patterns::{
19 has_bind_pat_parent, has_block_expr_parent, has_field_list_parent, 19 fn_is_prev, for_is_prev2, has_bind_pat_parent, has_block_expr_parent,
20 has_impl_as_prev_sibling, has_impl_parent, has_item_list_or_source_file_parent, 20 has_field_list_parent, has_impl_as_prev_sibling, has_impl_parent,
21 has_ref_parent, has_trait_as_prev_sibling, has_trait_parent, if_is_prev, 21 has_item_list_or_source_file_parent, has_ref_parent, has_trait_as_prev_sibling,
22 is_in_loop_body, is_match_arm, unsafe_is_prev, 22 has_trait_parent, if_is_prev, inside_impl_trait_block, is_in_loop_body, is_match_arm,
23 unsafe_is_prev,
23 }, 24 },
24 CompletionConfig, 25 CompletionConfig,
25 }, 26 },
@@ -86,11 +87,14 @@ pub(crate) struct CompletionContext<'a> {
86 pub(super) in_loop_body: bool, 87 pub(super) in_loop_body: bool,
87 pub(super) has_trait_parent: bool, 88 pub(super) has_trait_parent: bool,
88 pub(super) has_impl_parent: bool, 89 pub(super) has_impl_parent: bool,
90 pub(super) inside_impl_trait_block: bool,
89 pub(super) has_field_list_parent: bool, 91 pub(super) has_field_list_parent: bool,
90 pub(super) trait_as_prev_sibling: bool, 92 pub(super) trait_as_prev_sibling: bool,
91 pub(super) impl_as_prev_sibling: bool, 93 pub(super) impl_as_prev_sibling: bool,
92 pub(super) is_match_arm: bool, 94 pub(super) is_match_arm: bool,
93 pub(super) has_item_list_or_source_file_parent: bool, 95 pub(super) has_item_list_or_source_file_parent: bool,
96 pub(super) for_is_prev2: bool,
97 pub(super) fn_is_prev: bool,
94 pub(super) locals: Vec<(String, Local)>, 98 pub(super) locals: Vec<(String, Local)>,
95} 99}
96 100
@@ -168,12 +172,15 @@ impl<'a> CompletionContext<'a> {
168 block_expr_parent: false, 172 block_expr_parent: false,
169 has_trait_parent: false, 173 has_trait_parent: false,
170 has_impl_parent: false, 174 has_impl_parent: false,
175 inside_impl_trait_block: false,
171 has_field_list_parent: false, 176 has_field_list_parent: false,
172 trait_as_prev_sibling: false, 177 trait_as_prev_sibling: false,
173 impl_as_prev_sibling: false, 178 impl_as_prev_sibling: false,
174 if_is_prev: false, 179 if_is_prev: false,
175 is_match_arm: false, 180 is_match_arm: false,
176 has_item_list_or_source_file_parent: false, 181 has_item_list_or_source_file_parent: false,
182 for_is_prev2: false,
183 fn_is_prev: false,
177 locals, 184 locals,
178 }; 185 };
179 186
@@ -221,6 +228,15 @@ impl<'a> CompletionContext<'a> {
221 Some(ctx) 228 Some(ctx)
222 } 229 }
223 230
231 /// Checks whether completions in that particular case don't make much sense.
232 /// Examples:
233 /// - `fn <|>` -- we expect function name, it's unlikely that "hint" will be helpful.
234 /// Exception for this case is `impl Trait for Foo`, where we would like to hint trait method names.
235 /// - `for _ i<|>` -- obviously, it'll be "in" keyword.
236 pub(crate) fn no_completion_required(&self) -> bool {
237 (self.fn_is_prev && !self.inside_impl_trait_block) || self.for_is_prev2
238 }
239
224 /// The range of the identifier that is being completed. 240 /// The range of the identifier that is being completed.
225 pub(crate) fn source_range(&self) -> TextRange { 241 pub(crate) fn source_range(&self) -> TextRange {
226 // check kind of macro-expanded token, but use range of original token 242 // check kind of macro-expanded token, but use range of original token
@@ -244,6 +260,7 @@ impl<'a> CompletionContext<'a> {
244 self.in_loop_body = is_in_loop_body(syntax_element.clone()); 260 self.in_loop_body = is_in_loop_body(syntax_element.clone());
245 self.has_trait_parent = has_trait_parent(syntax_element.clone()); 261 self.has_trait_parent = has_trait_parent(syntax_element.clone());
246 self.has_impl_parent = has_impl_parent(syntax_element.clone()); 262 self.has_impl_parent = has_impl_parent(syntax_element.clone());
263 self.inside_impl_trait_block = inside_impl_trait_block(syntax_element.clone());
247 self.has_field_list_parent = has_field_list_parent(syntax_element.clone()); 264 self.has_field_list_parent = has_field_list_parent(syntax_element.clone());
248 self.impl_as_prev_sibling = has_impl_as_prev_sibling(syntax_element.clone()); 265 self.impl_as_prev_sibling = has_impl_as_prev_sibling(syntax_element.clone());
249 self.trait_as_prev_sibling = has_trait_as_prev_sibling(syntax_element.clone()); 266 self.trait_as_prev_sibling = has_trait_as_prev_sibling(syntax_element.clone());
@@ -253,6 +270,8 @@ impl<'a> CompletionContext<'a> {
253 self.mod_declaration_under_caret = 270 self.mod_declaration_under_caret =
254 find_node_at_offset::<ast::Module>(&file_with_fake_ident, offset) 271 find_node_at_offset::<ast::Module>(&file_with_fake_ident, offset)
255 .filter(|module| module.item_list().is_none()); 272 .filter(|module| module.item_list().is_none());
273 self.for_is_prev2 = for_is_prev2(syntax_element.clone());
274 self.fn_is_prev = fn_is_prev(syntax_element.clone());
256 } 275 }
257 276
258 fn fill( 277 fn fill(