From adbcedde1812b728726419f24000bf123b22fef9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 3 Apr 2020 19:59:28 +0200 Subject: Remove the second code-path for completing names in patterns --- crates/ra_ide/src/completion/complete_pattern.rs | 57 +++++----------------- crates/ra_ide/src/completion/complete_scope.rs | 10 +--- crates/ra_ide/src/completion/completion_context.rs | 31 ++++++------ 3 files changed, 28 insertions(+), 70 deletions(-) (limited to 'crates/ra_ide/src') diff --git a/crates/ra_ide/src/completion/complete_pattern.rs b/crates/ra_ide/src/completion/complete_pattern.rs index bc8fade6f..1c8b50eec 100644 --- a/crates/ra_ide/src/completion/complete_pattern.rs +++ b/crates/ra_ide/src/completion/complete_pattern.rs @@ -4,23 +4,25 @@ use crate::completion::{CompletionContext, Completions}; /// Completes constats and paths in patterns. pub(super) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) { - if !ctx.is_pat_binding { + if !ctx.is_pat_binding_or_const { return; } // FIXME: ideally, we should look at the type we are matching against and // suggest variants + auto-imports ctx.scope().process_all_names(&mut |name, res| { - let def = match &res { - hir::ScopeDef::ModuleDef(def) => def, + match &res { + hir::ScopeDef::ModuleDef(def) => match def { + hir::ModuleDef::Adt(hir::Adt::Enum(..)) + | hir::ModuleDef::Adt(hir::Adt::Struct(..)) + | hir::ModuleDef::EnumVariant(..) + | hir::ModuleDef::Const(..) + | hir::ModuleDef::Module(..) => (), + _ => return, + }, + hir::ScopeDef::MacroDef(_) => (), _ => return, }; - match def { - hir::ModuleDef::Adt(hir::Adt::Enum(..)) - | hir::ModuleDef::EnumVariant(..) - | hir::ModuleDef::Const(..) - | hir::ModuleDef::Module(..) => (), - _ => return, - } + acc.add_resolution(ctx, name.to_string(), &res) }); } @@ -69,13 +71,6 @@ mod tests { insert: "E", kind: Enum, }, - CompletionItem { - label: "E", - source_range: [246; 246), - delete: [246; 246), - insert: "E", - kind: Enum, - }, CompletionItem { label: "X", source_range: [246; 246), @@ -83,20 +78,6 @@ mod tests { insert: "X", kind: EnumVariant, }, - CompletionItem { - label: "X", - source_range: [246; 246), - delete: [246; 246), - insert: "X", - kind: EnumVariant, - }, - CompletionItem { - label: "Z", - source_range: [246; 246), - delete: [246; 246), - insert: "Z", - kind: Const, - }, CompletionItem { label: "Z", source_range: [246; 246), @@ -111,13 +92,6 @@ mod tests { insert: "m", kind: Module, }, - CompletionItem { - label: "m", - source_range: [246; 246), - delete: [246; 246), - insert: "m", - kind: Module, - }, ] "###); } @@ -138,13 +112,6 @@ mod tests { ); assert_debug_snapshot!(completions, @r###" [ - CompletionItem { - label: "E", - source_range: [151; 151), - delete: [151; 151), - insert: "E", - kind: Enum, - }, CompletionItem { label: "E", source_range: [151; 151), diff --git a/crates/ra_ide/src/completion/complete_scope.rs b/crates/ra_ide/src/completion/complete_scope.rs index 2ca552733..665597e4c 100644 --- a/crates/ra_ide/src/completion/complete_scope.rs +++ b/crates/ra_ide/src/completion/complete_scope.rs @@ -1,19 +1,13 @@ //! Completion of names from the current scope, e.g. locals and imported items. use crate::completion::{CompletionContext, Completions}; -use hir::{ModuleDef, ScopeDef}; pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) { - if !ctx.is_trivial_path && !ctx.is_pat_binding_and_path { + if !(ctx.is_trivial_path && !ctx.is_pat_binding_or_const) { return; } - ctx.scope().process_all_names(&mut |name, res| match (ctx.is_pat_binding_and_path, &res) { - (true, ScopeDef::ModuleDef(ModuleDef::Function(..))) => (), - (true, ScopeDef::ModuleDef(ModuleDef::Static(..))) => (), - (true, ScopeDef::Local(..)) => (), - _ => acc.add_resolution(ctx, name.to_string(), &res), - }); + ctx.scope().process_all_names(&mut |name, res| acc.add_resolution(ctx, name.to_string(), &res)); } #[cfg(test)] diff --git a/crates/ra_ide/src/completion/completion_context.rs b/crates/ra_ide/src/completion/completion_context.rs index fdc0da2c5..b8213d62f 100644 --- a/crates/ra_ide/src/completion/completion_context.rs +++ b/crates/ra_ide/src/completion/completion_context.rs @@ -35,10 +35,7 @@ pub(crate) struct CompletionContext<'a> { pub(super) is_param: bool, /// If a name-binding or reference to a const in a pattern. /// Irrefutable patterns (like let) are excluded. - pub(super) is_pat_binding: bool, - // A bind battern which may also be part of a path. - // if let Some(En<|>) = Some(Enum::A) - pub(super) is_pat_binding_and_path: bool, + pub(super) is_pat_binding_or_const: bool, /// A single-indent path, like `foo`. `::foo` should not be considered a trivial path. pub(super) is_trivial_path: bool, /// If not a trivial path, the prefix (qualifier). @@ -97,8 +94,7 @@ impl<'a> CompletionContext<'a> { record_lit_pat: None, impl_def: None, is_param: false, - is_pat_binding: false, - is_pat_binding_and_path: false, + is_pat_binding_or_const: false, is_trivial_path: false, path_prefix: None, after_if: false, @@ -190,18 +186,19 @@ impl<'a> CompletionContext<'a> { // suggest declaration names, see `CompletionKind::Magic`. if let Some(name) = find_node_at_offset::(&file_with_fake_ident, offset) { if let Some(bind_pat) = name.syntax().ancestors().find_map(ast::BindPat::cast) { - let parent = bind_pat.syntax().parent(); - if parent.clone().and_then(ast::MatchArm::cast).is_some() - || parent.clone().and_then(ast::Condition::cast).is_some() - { - self.is_pat_binding = true; + self.is_pat_binding_or_const = true; + if bind_pat.has_at() || bind_pat.is_ref() || bind_pat.is_mutable() { + self.is_pat_binding_or_const = false; } - - if parent.and_then(ast::RecordFieldPatList::cast).is_none() - && bind_pat.pat().is_none() - && !bind_pat.is_ref() - { - self.is_pat_binding_and_path = true; + if bind_pat.syntax().parent().and_then(ast::RecordFieldPatList::cast).is_some() { + self.is_pat_binding_or_const = false; + } + if let Some(let_stmt) = bind_pat.syntax().ancestors().find_map(ast::LetStmt::cast) { + if let Some(pat) = let_stmt.pat() { + if bind_pat.syntax().text_range().is_subrange(&pat.syntax().text_range()) { + self.is_pat_binding_or_const = false; + } + } } } if is_node::(name.syntax()) { -- cgit v1.2.3