From f801723dd2e4a518c1608909509f47f03d75fe1a Mon Sep 17 00:00:00 2001 From: Kevin DeLorey <2295721+kdelorey@users.noreply.github.com> Date: Sat, 8 Feb 2020 11:28:39 -0600 Subject: Got the magic completion working. --- .../ra_ide/src/completion/complete_trait_impl.rs | 48 ++++------------------ crates/ra_ide/src/completion/completion_context.rs | 9 ++++ 2 files changed, 16 insertions(+), 41 deletions(-) diff --git a/crates/ra_ide/src/completion/complete_trait_impl.rs b/crates/ra_ide/src/completion/complete_trait_impl.rs index 8f38c6325..7af485cdd 100644 --- a/crates/ra_ide/src/completion/complete_trait_impl.rs +++ b/crates/ra_ide/src/completion/complete_trait_impl.rs @@ -6,13 +6,12 @@ use hir::{ self, db::HirDatabase }; use ra_syntax::{ SyntaxKind, ast, ast::AstNode, TextRange }; pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) { - let item_list = ast::ItemList::cast(ctx.token.parent()); - let impl_block = item_list - .clone() - .and_then(|i| i.syntax().parent()) - .and_then(|p| ast::ImplBlock::cast(p)); + let impl_block = ctx.impl_block.as_ref(); + let item_list = impl_block.and_then(|i| i.item_list()); - if item_list.is_none() || impl_block.is_none() { + if item_list.is_none() + || impl_block.is_none() + || ctx.function_syntax.is_some() { return; } @@ -166,7 +165,8 @@ pub(crate) fn add_function_impl(acc: &mut Completions, ctx: &CompletionContext, format!("fn {}()", func_name.to_string()) }; - let builder = CompletionItem::new(CompletionKind::Magic, start, label); + let builder = CompletionItem::new(CompletionKind::Magic, start, label.clone()) + .lookup_by(label); let completion_kind = if func.has_self_param(ctx.db) { CompletionItemKind::Method @@ -183,7 +183,6 @@ pub(crate) fn add_function_impl(acc: &mut Completions, ctx: &CompletionContext, builder .insert_text(snippet) .kind(completion_kind) - .lookup_by(func_name.to_string()) .add_to(acc); } @@ -219,7 +218,6 @@ mod tests { delete: [138; 138), insert: "fn foo() {}", kind: Function, - lookup: "foo", }, ] "###); @@ -251,7 +249,6 @@ mod tests { delete: [193; 193), insert: "fn bar() {}", kind: Function, - lookup: "bar", }, ] "###); @@ -280,7 +277,6 @@ mod tests { delete: [141; 141), insert: "fn foo() {}", kind: Function, - lookup: "foo", }, ] "###); @@ -309,36 +305,6 @@ mod tests { delete: [163; 163), insert: "fn foo()\nwhere T: Into {}", kind: Function, - lookup: "foo", - }, - ] - "###); - } - - #[test] - fn start_from_fn_kw() { - let completions = complete( - r" - trait Test { - fn foo(); - } - - struct T1; - - impl Test for T1 { - fn <|> - } - ", - ); - assert_debug_snapshot!(completions, @r###" - [ - CompletionItem { - label: "fn foo()", - source_range: [138; 140), - delete: [138; 140), - insert: "fn foo() {}", - kind: Function, - lookup: "foo", }, ] "###); diff --git a/crates/ra_ide/src/completion/completion_context.rs b/crates/ra_ide/src/completion/completion_context.rs index deaacda6c..18c91a840 100644 --- a/crates/ra_ide/src/completion/completion_context.rs +++ b/crates/ra_ide/src/completion/completion_context.rs @@ -24,6 +24,7 @@ pub(crate) struct CompletionContext<'a> { pub(super) use_item_syntax: Option, pub(super) record_lit_syntax: Option, pub(super) record_lit_pat: Option, + pub(super) impl_block: Option, pub(super) is_param: bool, /// If a name-binding or reference to a const in a pattern. /// Irrefutable patterns (like let) are excluded. @@ -71,6 +72,7 @@ impl<'a> CompletionContext<'a> { use_item_syntax: None, record_lit_syntax: None, record_lit_pat: None, + impl_block: None, is_param: false, is_pat_binding: false, is_trivial_path: false, @@ -147,6 +149,13 @@ impl<'a> CompletionContext<'a> { self.record_lit_syntax = find_node_at_offset(original_file.syntax(), self.offset); } + self.impl_block = self + .token + .parent() + .ancestors() + .take_while(|it| it.kind() != SOURCE_FILE && it.kind() != MODULE) + .find_map(ast::ImplBlock::cast); + let top_node = name_ref .syntax() .ancestors() -- cgit v1.2.3