From 6941a7faba49b3927df3106b70b780857d1bab17 Mon Sep 17 00:00:00 2001 From: Steffen Lyngbaek Date: Wed, 18 Mar 2020 00:46:42 -0700 Subject: - Exclude Local Scope for BindPats - Exclude BindPats with @ or ref - Remove outdated test and add one testing for ref --- crates/ra_ide/src/completion/complete_scope.rs | 63 +++++----------------- crates/ra_ide/src/completion/completion_context.rs | 11 ++-- 2 files changed, 16 insertions(+), 58 deletions(-) (limited to 'crates/ra_ide/src/completion') diff --git a/crates/ra_ide/src/completion/complete_scope.rs b/crates/ra_ide/src/completion/complete_scope.rs index 82842e7e3..6b0621081 100644 --- a/crates/ra_ide/src/completion/complete_scope.rs +++ b/crates/ra_ide/src/completion/complete_scope.rs @@ -1,13 +1,17 @@ //! Completion of names from the current scope, e.g. locals and imported items. use crate::completion::{CompletionContext, Completions}; +use hir::ScopeDef; pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) { if !ctx.is_trivial_path && !ctx.is_pat_binding_and_path { return; } - ctx.scope().process_all_names(&mut |name, res| acc.add_resolution(ctx, name.to_string(), &res)); + ctx.scope().process_all_names(&mut |name, res| match (ctx.is_pat_binding_and_path, &res) { + (true, ScopeDef::Local(..)) => {} + _ => acc.add_resolution(ctx, name.to_string(), &res), + }); } #[cfg(test)] @@ -21,53 +25,23 @@ mod tests { } #[test] - fn nested_bind_pat_and_path() { + fn bind_pat_and_path_ignore_ref() { assert_debug_snapshot!( do_reference_completion( r" - enum First { + enum Enum { A, B, } - enum Second { - A(First), - B(First), - } - fn quux(x: Option>>) { + fn quux(x: Option) { match x { None => (), - Some(Some(Second(Fi<|>))) => (), + Some(ref en<|>) => (), } } " ), - @r###" - [ - CompletionItem { - label: "First", - source_range: [363; 365), - delete: [363; 365), - insert: "First", - kind: Enum, - }, - CompletionItem { - label: "Second", - source_range: [363; 365), - delete: [363; 365), - insert: "Second", - kind: Enum, - }, - CompletionItem { - label: "quux(…)", - source_range: [363; 365), - delete: [363; 365), - insert: "quux(${1:x})$0", - kind: Function, - lookup: "quux", - detail: "fn quux(x: Option>)", - }, - ] - "### + @r###"[]"### ); } @@ -83,7 +57,7 @@ mod tests { fn quux(x: Option) { match x { None => (), - Some(en<|>) => (), + Some(En<|>) => (), } } " @@ -97,13 +71,6 @@ mod tests { insert: "Enum", kind: Enum, }, - CompletionItem { - label: "None", - source_range: [231; 233), - delete: [231; 233), - insert: "None", - kind: Binding, - }, CompletionItem { label: "quux(…)", source_range: [231; 233), @@ -112,13 +79,7 @@ mod tests { kind: Function, lookup: "quux", detail: "fn quux(x: Option)", - }, - CompletionItem { - label: "x", - source_range: [231; 233), - delete: [231; 233), - insert: "x", - kind: Binding, + trigger_call_info: true, }, ] "### diff --git a/crates/ra_ide/src/completion/completion_context.rs b/crates/ra_ide/src/completion/completion_context.rs index d867ff6b2..f9d4154d8 100644 --- a/crates/ra_ide/src/completion/completion_context.rs +++ b/crates/ra_ide/src/completion/completion_context.rs @@ -190,19 +190,16 @@ 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 mut parent = bind_pat.syntax().parent(); + 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; } - while let Some(_) = parent.clone().and_then(ast::TupleStructPat::cast) { - parent = parent.and_then(|p| p.parent()); - if parent.clone().and_then(ast::MatchArm::cast).is_some() { - self.is_pat_binding_and_path = true; - break; - } + let bind_pat_string = bind_pat.syntax().to_string(); + if !bind_pat_string.contains("ref ") && !bind_pat_string.contains(" @ ") { + self.is_pat_binding_and_path = true; } } if is_node::(name.syntax()) { -- cgit v1.2.3