From 70157f07d9a33e4b8ed71e162218a793f44a7691 Mon Sep 17 00:00:00 2001 From: Igor Aleksanov Date: Mon, 12 Oct 2020 10:59:15 +0300 Subject: Remove redundant completions --- crates/ide/src/completion.rs | 33 +++++++++++++++++++++++++ crates/ide/src/completion/completion_context.rs | 22 ++++++++++++++--- crates/ide/src/completion/patterns.rs | 19 ++++++++++++++ 3 files changed, 70 insertions(+), 4 deletions(-) (limited to 'crates/ide') diff --git a/crates/ide/src/completion.rs b/crates/ide/src/completion.rs index b0e35b2bd..570091ba3 100644 --- a/crates/ide/src/completion.rs +++ b/crates/ide/src/completion.rs @@ -112,6 +112,11 @@ pub(crate) fn completions( ) -> Option { let ctx = CompletionContext::new(db, position, config)?; + if ctx.no_completion_required() { + // No work required here. + return None; + } + let mut acc = Completions::default(); complete_attribute::complete_attribute(&mut acc, &ctx); complete_fn_param::complete_fn_param(&mut acc, &ctx); @@ -157,6 +162,23 @@ mod tests { panic!("completion detail not found: {}", expected.detail) } + fn check_no_completion(ra_fixture: &str) { + let (analysis, position) = fixture::position(ra_fixture); + let config = CompletionConfig::default(); + analysis.completions(&config, position).unwrap(); + + let completions: Option> = analysis + .completions(&config, position) + .unwrap() + .and_then(|completions| if completions.is_empty() { None } else { Some(completions) }) + .map(|completions| { + completions.into_iter().map(|completion| format!("{:?}", completion)).collect() + }); + + // `assert_eq` instead of `assert!(completions.is_none())` to get the list of completions if test will panic. + assert_eq!(completions, None, "Completions were generated, but weren't expected"); + } + #[test] fn test_completion_detail_from_macro_generated_struct_fn_doc_attr() { check_detail_and_documentation( @@ -208,4 +230,15 @@ mod tests { DetailAndDocumentation { detail: "fn foo(&self)", documentation: " Do the foo" }, ); } + + #[test] + fn test_no_completions_required() { + check_no_completion( + r#" + fn foo() { + for i i<|> + } + "#, + ) + } } diff --git a/crates/ide/src/completion/completion_context.rs b/crates/ide/src/completion/completion_context.rs index 8dea8a4bf..c9473cca6 100644 --- a/crates/ide/src/completion/completion_context.rs +++ b/crates/ide/src/completion/completion_context.rs @@ -16,10 +16,10 @@ use crate::{ call_info::ActiveParameter, completion::{ patterns::{ - has_bind_pat_parent, has_block_expr_parent, has_field_list_parent, - has_impl_as_prev_sibling, has_impl_parent, has_item_list_or_source_file_parent, - has_ref_parent, has_trait_as_prev_sibling, has_trait_parent, if_is_prev, - is_in_loop_body, is_match_arm, unsafe_is_prev, + fn_is_prev, for_is_prev2, has_bind_pat_parent, has_block_expr_parent, + has_field_list_parent, has_impl_as_prev_sibling, has_impl_parent, + has_item_list_or_source_file_parent, has_ref_parent, has_trait_as_prev_sibling, + has_trait_parent, if_is_prev, is_in_loop_body, is_match_arm, unsafe_is_prev, }, CompletionConfig, }, @@ -91,6 +91,8 @@ pub(crate) struct CompletionContext<'a> { pub(super) impl_as_prev_sibling: bool, pub(super) is_match_arm: bool, pub(super) has_item_list_or_source_file_parent: bool, + pub(super) for_is_prev2: bool, + pub(super) fn_is_prev: bool, pub(super) locals: Vec<(String, Local)>, } @@ -174,6 +176,8 @@ impl<'a> CompletionContext<'a> { if_is_prev: false, is_match_arm: false, has_item_list_or_source_file_parent: false, + for_is_prev2: false, + fn_is_prev: false, locals, }; @@ -221,6 +225,14 @@ impl<'a> CompletionContext<'a> { Some(ctx) } + /// Checks whether completions in that particular case don't make much sense. + /// Examples: + /// - `fn <|>` -- we expect function name, it's unlikely that "hint" will be helpful. + /// - `for _ i<|>` -- obviously, it'll be "in" keyword. + pub(crate) fn no_completion_required(&self) -> bool { + self.fn_is_prev || self.for_is_prev2 + } + /// The range of the identifier that is being completed. pub(crate) fn source_range(&self) -> TextRange { // check kind of macro-expanded token, but use range of original token @@ -253,6 +265,8 @@ impl<'a> CompletionContext<'a> { self.mod_declaration_under_caret = find_node_at_offset::(&file_with_fake_ident, offset) .filter(|module| module.item_list().is_none()); + self.for_is_prev2 = for_is_prev2(syntax_element.clone()); + self.fn_is_prev = fn_is_prev(syntax_element.clone()); } fn fill( diff --git a/crates/ide/src/completion/patterns.rs b/crates/ide/src/completion/patterns.rs index b17ddf133..76fcad631 100644 --- a/crates/ide/src/completion/patterns.rs +++ b/crates/ide/src/completion/patterns.rs @@ -116,6 +116,25 @@ pub(crate) fn if_is_prev(element: SyntaxElement) -> bool { .is_some() } +pub(crate) fn fn_is_prev(element: SyntaxElement) -> bool { + element + .into_token() + .and_then(|it| previous_non_trivia_token(it)) + .filter(|it| it.kind() == FN_KW) + .is_some() +} + +/// Check if the token previous to the previous one is `for`. +/// For example, `for _ i<|>` => true. +pub(crate) fn for_is_prev2(element: SyntaxElement) -> bool { + element + .into_token() + .and_then(|it| previous_non_trivia_token(it)) + .and_then(|it| previous_non_trivia_token(it)) + .filter(|it| it.kind() == FOR_KW) + .is_some() +} + #[test] fn test_if_is_prev() { check_pattern_is_applicable(r"if l<|>", if_is_prev); -- cgit v1.2.3