Got the magic completion working.

This commit is contained in:
Kevin DeLorey 2020-02-08 11:28:39 -06:00
parent 5216b09ed6
commit f801723dd2
2 changed files with 16 additions and 41 deletions

View file

@ -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<T>() {}",
kind: Function,
lookup: "foo",
},
]
"###);
@ -309,36 +305,6 @@ mod tests {
delete: [163; 163),
insert: "fn foo<T>()\nwhere T: Into<String> {}",
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",
},
]
"###);

View file

@ -24,6 +24,7 @@ pub(crate) struct CompletionContext<'a> {
pub(super) use_item_syntax: Option<ast::UseItem>,
pub(super) record_lit_syntax: Option<ast::RecordLit>,
pub(super) record_lit_pat: Option<ast::RecordPat>,
pub(super) impl_block: Option<ast::ImplBlock>,
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()