From 6f573bd84f4564f11b08db720401ae16a0f42f2f Mon Sep 17 00:00:00 2001 From: Igor Aleksanov Date: Sat, 17 Oct 2020 11:03:07 +0300 Subject: Allow hints after 'fn' keyword if it's an impl trait block --- crates/ide/src/completion/completion_context.rs | 11 ++++++++--- crates/ide/src/completion/patterns.rs | 18 +++++++++++++++++- crates/ide/src/completion/test_utils.rs | 12 ++++++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) (limited to 'crates') diff --git a/crates/ide/src/completion/completion_context.rs b/crates/ide/src/completion/completion_context.rs index c9473cca6..cff0afae9 100644 --- a/crates/ide/src/completion/completion_context.rs +++ b/crates/ide/src/completion/completion_context.rs @@ -18,8 +18,9 @@ use crate::{ patterns::{ 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, + has_impl_trait_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, }, @@ -86,6 +87,7 @@ pub(crate) struct CompletionContext<'a> { pub(super) in_loop_body: bool, pub(super) has_trait_parent: bool, pub(super) has_impl_parent: bool, + pub(super) has_impl_trait_parent: bool, pub(super) has_field_list_parent: bool, pub(super) trait_as_prev_sibling: bool, pub(super) impl_as_prev_sibling: bool, @@ -170,6 +172,7 @@ impl<'a> CompletionContext<'a> { block_expr_parent: false, has_trait_parent: false, has_impl_parent: false, + has_impl_trait_parent: false, has_field_list_parent: false, trait_as_prev_sibling: false, impl_as_prev_sibling: false, @@ -228,9 +231,10 @@ impl<'a> CompletionContext<'a> { /// 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. + /// Exception for this case is `impl Trait for Foo`, where we would like to hint trait method names. /// - `for _ i<|>` -- obviously, it'll be "in" keyword. pub(crate) fn no_completion_required(&self) -> bool { - self.fn_is_prev || self.for_is_prev2 + (self.fn_is_prev && !self.has_impl_trait_parent) || self.for_is_prev2 } /// The range of the identifier that is being completed. @@ -256,6 +260,7 @@ impl<'a> CompletionContext<'a> { self.in_loop_body = is_in_loop_body(syntax_element.clone()); self.has_trait_parent = has_trait_parent(syntax_element.clone()); self.has_impl_parent = has_impl_parent(syntax_element.clone()); + self.has_impl_trait_parent = has_impl_trait_parent(syntax_element.clone()); self.has_field_list_parent = has_field_list_parent(syntax_element.clone()); self.impl_as_prev_sibling = has_impl_as_prev_sibling(syntax_element.clone()); self.trait_as_prev_sibling = has_trait_as_prev_sibling(syntax_element.clone()); diff --git a/crates/ide/src/completion/patterns.rs b/crates/ide/src/completion/patterns.rs index f00ddeed7..bdce7a6e7 100644 --- a/crates/ide/src/completion/patterns.rs +++ b/crates/ide/src/completion/patterns.rs @@ -9,7 +9,7 @@ use syntax::{ }; #[cfg(test)] -use crate::completion::test_utils::check_pattern_is_applicable; +use crate::completion::test_utils::{check_pattern_is_applicable, check_pattern_is_not_applicable}; pub(crate) fn has_trait_parent(element: SyntaxElement) -> bool { not_same_range_ancestor(element) @@ -34,6 +34,22 @@ pub(crate) fn has_impl_parent(element: SyntaxElement) -> bool { fn test_has_impl_parent() { check_pattern_is_applicable(r"impl A { f<|> }", has_impl_parent); } + +pub(crate) fn has_impl_trait_parent(element: SyntaxElement) -> bool { + not_same_range_ancestor(element) + .filter(|it| it.kind() == ASSOC_ITEM_LIST) + .and_then(|it| it.parent()) + .filter(|it| it.kind() == IMPL) + .map(|it| ast::Impl::cast(it).unwrap()) + .map(|it| it.trait_().is_some()) + .unwrap_or(false) +} +#[test] +fn test_has_impl_trait_parent() { + check_pattern_is_applicable(r"impl Foo for Bar { f<|> }", has_impl_trait_parent); + check_pattern_is_not_applicable(r"impl A { f<|> }", has_impl_trait_parent); +} + pub(crate) fn has_field_list_parent(element: SyntaxElement) -> bool { not_same_range_ancestor(element).filter(|it| it.kind() == RECORD_FIELD_LIST).is_some() } diff --git a/crates/ide/src/completion/test_utils.rs b/crates/ide/src/completion/test_utils.rs index feb8cd2a6..dabbef888 100644 --- a/crates/ide/src/completion/test_utils.rs +++ b/crates/ide/src/completion/test_utils.rs @@ -104,6 +104,18 @@ pub(crate) fn check_pattern_is_applicable(code: &str, check: fn(SyntaxElement) - .unwrap(); } +pub(crate) fn check_pattern_is_not_applicable(code: &str, check: fn(SyntaxElement) -> bool) { + let (analysis, pos) = fixture::position(code); + analysis + .with_db(|db| { + let sema = Semantics::new(db); + let original_file = sema.parse(pos.file_id); + let token = original_file.syntax().token_at_offset(pos.offset).left_biased().unwrap(); + assert!(!check(NodeOrToken::Token(token))); + }) + .unwrap(); +} + pub(crate) fn get_all_completion_items( config: CompletionConfig, code: &str, -- cgit v1.2.3