From 57a260f579fec4082aa9e7a30d4b190f06d45877 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Tue, 8 Sep 2020 00:54:58 +0300 Subject: Properly reacto to keywords --- crates/ide/src/completion/complete_attribute.rs | 4 ++++ crates/ide/src/completion/complete_mod.rs | 8 +++++++- crates/ide/src/completion/complete_qualified_path.rs | 2 +- crates/ide/src/completion/complete_unqualified_path.rs | 1 + crates/ide/src/completion/completion_context.rs | 7 +++++-- crates/ide/src/completion/patterns.rs | 10 ++++++++++ 6 files changed, 28 insertions(+), 4 deletions(-) (limited to 'crates') diff --git a/crates/ide/src/completion/complete_attribute.rs b/crates/ide/src/completion/complete_attribute.rs index 0abfaebcb..6394189f0 100644 --- a/crates/ide/src/completion/complete_attribute.rs +++ b/crates/ide/src/completion/complete_attribute.rs @@ -13,6 +13,10 @@ use crate::completion::{ }; pub(super) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> { + if ctx.mod_is_prev { + return None; + } + let attribute = ctx.attribute_under_caret.as_ref()?; match (attribute.path(), attribute.token_tree()) { (Some(path), Some(token_tree)) if path.to_string() == "derive" => { diff --git a/crates/ide/src/completion/complete_mod.rs b/crates/ide/src/completion/complete_mod.rs index c5757a310..5d41d0f69 100644 --- a/crates/ide/src/completion/complete_mod.rs +++ b/crates/ide/src/completion/complete_mod.rs @@ -9,6 +9,12 @@ use super::{completion_context::CompletionContext, completion_item::Completions} /// Complete mod declaration, i.e. `mod <|> ;` pub(super) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> { + let _p = profile::span("completion::complete_mod"); + + if !ctx.mod_is_prev { + return None; + } + let current_module = ctx.scope.module()?; let module_definition_file = @@ -63,7 +69,7 @@ pub(super) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Op .collect::>(); dbg!(mod_declaration_candidates); - // TODO kb exlude existing children from the candidates + // TODO kb actually add the results Some(()) } diff --git a/crates/ide/src/completion/complete_qualified_path.rs b/crates/ide/src/completion/complete_qualified_path.rs index accb09f7e..351461351 100644 --- a/crates/ide/src/completion/complete_qualified_path.rs +++ b/crates/ide/src/completion/complete_qualified_path.rs @@ -13,7 +13,7 @@ pub(super) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon None => return, }; - if ctx.attribute_under_caret.is_some() { + if ctx.attribute_under_caret.is_some() || ctx.mod_is_prev { return; } diff --git a/crates/ide/src/completion/complete_unqualified_path.rs b/crates/ide/src/completion/complete_unqualified_path.rs index 1f1b682a7..9f2dc16ab 100644 --- a/crates/ide/src/completion/complete_unqualified_path.rs +++ b/crates/ide/src/completion/complete_unqualified_path.rs @@ -13,6 +13,7 @@ pub(super) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC if ctx.record_lit_syntax.is_some() || ctx.record_pat_syntax.is_some() || ctx.attribute_under_caret.is_some() + || ctx.mod_is_prev { return; } diff --git a/crates/ide/src/completion/completion_context.rs b/crates/ide/src/completion/completion_context.rs index 47355d5dc..d289aac27 100644 --- a/crates/ide/src/completion/completion_context.rs +++ b/crates/ide/src/completion/completion_context.rs @@ -19,7 +19,7 @@ use crate::{ 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, + is_in_loop_body, is_match_arm, mod_is_prev, unsafe_is_prev, }, CompletionConfig, }, @@ -77,6 +77,7 @@ pub(crate) struct CompletionContext<'a> { pub(super) is_path_type: bool, pub(super) has_type_args: bool, pub(super) attribute_under_caret: Option, + pub(super) mod_is_prev: bool, pub(super) unsafe_is_prev: bool, pub(super) if_is_prev: bool, pub(super) block_expr_parent: bool, @@ -152,6 +153,7 @@ impl<'a> CompletionContext<'a> { has_type_args: false, dot_receiver_is_ambiguous_float_literal: false, attribute_under_caret: None, + mod_is_prev: false, unsafe_is_prev: false, in_loop_body: false, ref_pat_parent: false, @@ -238,7 +240,8 @@ impl<'a> CompletionContext<'a> { self.trait_as_prev_sibling = has_trait_as_prev_sibling(syntax_element.clone()); self.is_match_arm = is_match_arm(syntax_element.clone()); self.has_item_list_or_source_file_parent = - has_item_list_or_source_file_parent(syntax_element); + has_item_list_or_source_file_parent(syntax_element.clone()); + self.mod_is_prev = mod_is_prev(syntax_element); } fn fill( diff --git a/crates/ide/src/completion/patterns.rs b/crates/ide/src/completion/patterns.rs index c6ae589db..bc4ce4d6f 100644 --- a/crates/ide/src/completion/patterns.rs +++ b/crates/ide/src/completion/patterns.rs @@ -115,6 +115,16 @@ pub(crate) fn if_is_prev(element: SyntaxElement) -> bool { .filter(|it| it.kind() == IF_KW) .is_some() } + +// TODO kb generify? +pub(crate) fn mod_is_prev(element: SyntaxElement) -> bool { + element + .into_token() + .and_then(|it| previous_non_trivia_token(it)) + .filter(|it| it.kind() == MOD_KW) + .is_some() +} + #[test] fn test_if_is_prev() { check_pattern_is_applicable(r"if l<|>", if_is_prev); -- cgit v1.2.3