From f41c98342476087d0a4387e7d337ce2d897e0346 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 27 May 2021 04:34:21 +0200 Subject: Don't complete non-macro item paths in impls and modules --- crates/ide_completion/src/context.rs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'crates/ide_completion/src/context.rs') diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs index 66577df94..b7e116dba 100644 --- a/crates/ide_completion/src/context.rs +++ b/crates/ide_completion/src/context.rs @@ -127,6 +127,7 @@ pub(crate) struct CompletionContext<'a> { no_completion_required: bool, } + impl<'a> CompletionContext<'a> { pub(super) fn new( db: &'a RootDatabase, @@ -281,21 +282,25 @@ impl<'a> CompletionContext<'a> { self.previous_token.as_ref().map_or(false, |tok| tok.kind() == kind) } - pub(crate) fn has_impl_or_trait_parent(&self) -> bool { + pub(crate) fn expects_assoc_item(&self) -> bool { matches!( self.completion_location, Some(ImmediateLocation::Trait) | Some(ImmediateLocation::Impl) ) } - pub(crate) fn has_block_expr_parent(&self) -> bool { - matches!(self.completion_location, Some(ImmediateLocation::BlockExpr)) + pub(crate) fn expects_non_trait_assoc_item(&self) -> bool { + matches!(self.completion_location, Some(ImmediateLocation::Impl)) } - pub(crate) fn has_item_list_parent(&self) -> bool { + pub(crate) fn expects_item(&self) -> bool { matches!(self.completion_location, Some(ImmediateLocation::ItemList)) } + pub(crate) fn has_block_expr_parent(&self) -> bool { + matches!(self.completion_location, Some(ImmediateLocation::BlockExpr)) + } + pub(crate) fn has_ident_or_ref_pat_parent(&self) -> bool { matches!( self.completion_location, @@ -303,11 +308,7 @@ impl<'a> CompletionContext<'a> { ) } - pub(crate) fn has_impl_parent(&self) -> bool { - matches!(self.completion_location, Some(ImmediateLocation::Impl)) - } - - pub(crate) fn has_field_list_parent(&self) -> bool { + pub(crate) fn expect_record_field(&self) -> bool { matches!(self.completion_location, Some(ImmediateLocation::RecordFieldList)) } @@ -320,10 +321,10 @@ impl<'a> CompletionContext<'a> { || self.record_pat_syntax.is_some() || self.attribute_under_caret.is_some() || self.mod_declaration_under_caret.is_some() - || self.has_impl_or_trait_parent() } fn fill_keyword_patterns(&mut self, file_with_fake_ident: &SyntaxNode, offset: TextSize) { + dbg!(file_with_fake_ident); let fake_ident_token = file_with_fake_ident.token_at_offset(offset).right_biased().unwrap(); let syntax_element = NodeOrToken::Token(fake_ident_token); self.previous_token = previous_token(syntax_element.clone()); -- cgit v1.2.3 From 3a16950fd919f46fd879c36423810a40105b2c10 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 27 May 2021 18:15:18 +0200 Subject: Cleanup `ImmediateLocation` determination --- crates/ide_completion/src/context.rs | 44 +++++++----------------------------- 1 file changed, 8 insertions(+), 36 deletions(-) (limited to 'crates/ide_completion/src/context.rs') diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs index b7e116dba..fbef54408 100644 --- a/crates/ide_completion/src/context.rs +++ b/crates/ide_completion/src/context.rs @@ -17,9 +17,8 @@ use text_edit::Indel; use crate::{ patterns::{ - for_is_prev2, has_bind_pat_parent, has_block_expr_parent, has_field_list_parent, - has_impl_parent, has_item_list_or_source_file_parent, has_prev_sibling, has_ref_parent, - has_trait_parent, inside_impl_trait_block, is_in_loop_body, is_match_arm, previous_token, + determine_location, for_is_prev2, has_prev_sibling, inside_impl_trait_block, + is_in_loop_body, is_match_arm, previous_token, ImmediateLocation, }, CompletionConfig, }; @@ -30,18 +29,6 @@ pub(crate) enum PatternRefutability { Irrefutable, } -/// Direct parent container of the cursor position -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -pub(crate) enum ImmediateLocation { - Impl, - Trait, - RecordFieldList, - RefPatOrExpr, - IdentPat, - BlockExpr, - ItemList, -} - #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub(crate) enum PrevSibling { Trait, @@ -301,15 +288,15 @@ impl<'a> CompletionContext<'a> { matches!(self.completion_location, Some(ImmediateLocation::BlockExpr)) } - pub(crate) fn has_ident_or_ref_pat_parent(&self) -> bool { + pub(crate) fn expects_ident_pat_or_ref_expr(&self) -> bool { matches!( self.completion_location, - Some(ImmediateLocation::IdentPat) | Some(ImmediateLocation::RefPatOrExpr) + Some(ImmediateLocation::IdentPat) | Some(ImmediateLocation::RefExpr) ) } pub(crate) fn expect_record_field(&self) -> bool { - matches!(self.completion_location, Some(ImmediateLocation::RecordFieldList)) + matches!(self.completion_location, Some(ImmediateLocation::RecordField)) } pub(crate) fn has_impl_or_trait_prev_sibling(&self) -> bool { @@ -324,9 +311,8 @@ impl<'a> CompletionContext<'a> { } fn fill_keyword_patterns(&mut self, file_with_fake_ident: &SyntaxNode, offset: TextSize) { - dbg!(file_with_fake_ident); let fake_ident_token = file_with_fake_ident.token_at_offset(offset).right_biased().unwrap(); - let syntax_element = NodeOrToken::Token(fake_ident_token); + let syntax_element = NodeOrToken::Token(fake_ident_token.clone()); self.previous_token = previous_token(syntax_element.clone()); self.in_loop_body = is_in_loop_body(syntax_element.clone()); self.is_match_arm = is_match_arm(syntax_element.clone()); @@ -336,22 +322,6 @@ impl<'a> CompletionContext<'a> { self.prev_sibling = Some(PrevSibling::Trait) } - if has_block_expr_parent(syntax_element.clone()) { - self.completion_location = Some(ImmediateLocation::BlockExpr); - } else if has_bind_pat_parent(syntax_element.clone()) { - self.completion_location = Some(ImmediateLocation::IdentPat); - } else if has_ref_parent(syntax_element.clone()) { - self.completion_location = Some(ImmediateLocation::RefPatOrExpr); - } else if has_impl_parent(syntax_element.clone()) { - self.completion_location = Some(ImmediateLocation::Impl); - } else if has_field_list_parent(syntax_element.clone()) { - self.completion_location = Some(ImmediateLocation::RecordFieldList); - } else if has_trait_parent(syntax_element.clone()) { - self.completion_location = Some(ImmediateLocation::Trait); - } else if has_item_list_or_source_file_parent(syntax_element.clone()) { - self.completion_location = Some(ImmediateLocation::ItemList); - } - self.mod_declaration_under_caret = find_node_at_offset::(&file_with_fake_ident, offset) .filter(|module| module.item_list().is_none()); @@ -364,6 +334,8 @@ impl<'a> CompletionContext<'a> { let fn_is_prev = self.previous_token_is(T![fn]); let for_is_prev2 = for_is_prev2(syntax_element.clone()); self.no_completion_required = (fn_is_prev && !inside_impl_trait_block) || for_is_prev2; + + self.completion_location = determine_location(fake_ident_token); } fn fill_impl_def(&mut self) { -- cgit v1.2.3