2020-02-11 10:04:30 -06:00
|
|
|
//! FIXME: write short doc here
|
|
|
|
|
|
|
|
use crate::{
|
2020-02-11 10:07:23 -06:00
|
|
|
completion::{
|
|
|
|
CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions,
|
|
|
|
},
|
2020-02-11 10:04:30 -06:00
|
|
|
display::FunctionSignature,
|
2020-02-09 12:24:34 -06:00
|
|
|
};
|
2020-01-22 22:25:41 -06:00
|
|
|
|
2020-02-09 20:59:12 -06:00
|
|
|
use hir::{self, Docs, HasSource};
|
2020-02-11 09:40:08 -06:00
|
|
|
use ra_syntax::{
|
|
|
|
ast::{self, edit},
|
|
|
|
AstNode, SyntaxKind, TextRange,
|
|
|
|
};
|
2020-01-22 22:25:41 -06:00
|
|
|
|
2020-02-09 12:24:34 -06:00
|
|
|
use ra_assists::utils::get_missing_impl_items;
|
2020-01-22 22:25:41 -06:00
|
|
|
|
2020-01-22 22:38:03 -06:00
|
|
|
pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) {
|
2020-02-12 20:46:12 -06:00
|
|
|
let trigger = ctx.token.ancestors().find(|p| match p.kind() {
|
|
|
|
SyntaxKind::FN_DEF | SyntaxKind::TYPE_ALIAS_DEF | SyntaxKind::CONST_DEF => true,
|
|
|
|
_ => false,
|
|
|
|
});
|
2020-02-12 20:21:43 -06:00
|
|
|
|
|
|
|
let impl_block = trigger
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|node| node.parent())
|
|
|
|
.and_then(|node| node.parent())
|
|
|
|
.and_then(|node| ast::ImplBlock::cast(node));
|
|
|
|
|
|
|
|
if let (Some(trigger), Some(impl_block)) = (trigger, impl_block) {
|
|
|
|
match trigger.kind() {
|
|
|
|
SyntaxKind::FN_DEF => {
|
|
|
|
for missing_fn in get_missing_impl_items(ctx.db, &ctx.analyzer, &impl_block)
|
|
|
|
.iter()
|
2020-02-12 20:46:12 -06:00
|
|
|
.filter_map(|item| match item {
|
|
|
|
hir::AssocItem::Function(fn_item) => Some(fn_item),
|
|
|
|
_ => None,
|
|
|
|
})
|
2020-02-12 20:21:43 -06:00
|
|
|
{
|
|
|
|
add_function_impl(acc, ctx, &missing_fn);
|
|
|
|
}
|
2020-02-12 20:46:12 -06:00
|
|
|
}
|
2020-01-28 20:30:53 -06:00
|
|
|
|
2020-02-12 20:21:43 -06:00
|
|
|
SyntaxKind::TYPE_ALIAS_DEF => {
|
|
|
|
for missing_fn in get_missing_impl_items(ctx.db, &ctx.analyzer, &impl_block)
|
|
|
|
.iter()
|
|
|
|
.filter_map(|item| match item {
|
|
|
|
hir::AssocItem::TypeAlias(type_item) => Some(type_item),
|
2020-02-12 20:46:12 -06:00
|
|
|
_ => None,
|
|
|
|
})
|
2020-02-12 20:21:43 -06:00
|
|
|
{
|
|
|
|
add_type_alias_impl(acc, ctx, &missing_fn);
|
|
|
|
}
|
2020-02-12 20:46:12 -06:00
|
|
|
}
|
2020-02-12 20:21:43 -06:00
|
|
|
|
|
|
|
SyntaxKind::CONST_DEF => {
|
|
|
|
for missing_fn in get_missing_impl_items(ctx.db, &ctx.analyzer, &impl_block)
|
|
|
|
.iter()
|
|
|
|
.filter_map(|item| match item {
|
|
|
|
hir::AssocItem::Const(const_item) => Some(const_item),
|
2020-02-12 20:46:12 -06:00
|
|
|
_ => None,
|
|
|
|
})
|
2020-02-12 20:21:43 -06:00
|
|
|
{
|
|
|
|
add_const_impl(acc, ctx, &missing_fn);
|
|
|
|
}
|
2020-02-12 20:46:12 -06:00
|
|
|
}
|
2020-02-12 20:21:43 -06:00
|
|
|
|
|
|
|
_ => {}
|
2020-01-22 22:25:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-08 15:41:25 -06:00
|
|
|
fn add_function_impl(acc: &mut Completions, ctx: &CompletionContext, func: &hir::Function) {
|
2020-02-04 22:04:57 -06:00
|
|
|
let display = FunctionSignature::from_hir(ctx.db, func.clone());
|
|
|
|
|
|
|
|
let func_name = func.name(ctx.db);
|
|
|
|
|
|
|
|
let label = if func.params(ctx.db).len() > 0 {
|
|
|
|
format!("fn {}(..)", func_name.to_string())
|
|
|
|
} else {
|
|
|
|
format!("fn {}()", func_name.to_string())
|
|
|
|
};
|
|
|
|
|
2020-02-08 13:22:31 -06:00
|
|
|
let builder = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label.clone())
|
2020-02-08 15:41:25 -06:00
|
|
|
.lookup_by(label)
|
|
|
|
.set_documentation(func.docs(ctx.db));
|
2020-02-04 22:04:57 -06:00
|
|
|
|
|
|
|
let completion_kind = if func.has_self_param(ctx.db) {
|
|
|
|
CompletionItemKind::Method
|
|
|
|
} else {
|
|
|
|
CompletionItemKind::Function
|
|
|
|
};
|
2020-02-09 12:24:34 -06:00
|
|
|
|
2020-02-11 10:04:30 -06:00
|
|
|
let snippet = format!("{} {{}}", display);
|
2020-02-04 22:04:57 -06:00
|
|
|
|
2020-02-09 12:24:34 -06:00
|
|
|
builder.insert_text(snippet).kind(completion_kind).add_to(acc);
|
2020-02-04 22:04:57 -06:00
|
|
|
}
|
|
|
|
|
2020-02-09 12:24:34 -06:00
|
|
|
fn add_type_alias_impl(
|
|
|
|
acc: &mut Completions,
|
|
|
|
ctx: &CompletionContext,
|
|
|
|
type_alias: &hir::TypeAlias,
|
|
|
|
) {
|
2020-02-08 15:41:25 -06:00
|
|
|
let snippet = format!("type {} = ", type_alias.name(ctx.db).to_string());
|
|
|
|
|
|
|
|
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone())
|
|
|
|
.insert_text(snippet)
|
|
|
|
.kind(CompletionItemKind::TypeAlias)
|
|
|
|
.set_documentation(type_alias.docs(ctx.db))
|
|
|
|
.add_to(acc);
|
|
|
|
}
|
|
|
|
|
2020-02-11 09:40:08 -06:00
|
|
|
fn add_const_impl(acc: &mut Completions, ctx: &CompletionContext, const_: &hir::Const) {
|
2020-02-09 20:59:12 -06:00
|
|
|
let snippet = make_const_compl_syntax(&const_.source(ctx.db).value);
|
|
|
|
|
|
|
|
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone())
|
|
|
|
.insert_text(snippet)
|
|
|
|
.kind(CompletionItemKind::Const)
|
|
|
|
.set_documentation(const_.docs(ctx.db))
|
|
|
|
.add_to(acc);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_const_compl_syntax(const_: &ast::ConstDef) -> String {
|
|
|
|
let const_ = edit::strip_attrs_and_docs(const_);
|
2020-02-10 20:55:49 -06:00
|
|
|
|
2020-02-09 20:59:12 -06:00
|
|
|
let const_start = const_.syntax().text_range().start();
|
|
|
|
let const_end = const_.syntax().text_range().end();
|
|
|
|
|
2020-02-11 09:40:08 -06:00
|
|
|
let start =
|
|
|
|
const_.syntax().first_child_or_token().map_or(const_start, |f| f.text_range().start());
|
2020-02-09 20:59:12 -06:00
|
|
|
|
|
|
|
let end = const_
|
|
|
|
.syntax()
|
|
|
|
.children_with_tokens()
|
|
|
|
.find(|s| s.kind() == SyntaxKind::SEMI || s.kind() == SyntaxKind::EQ)
|
|
|
|
.map_or(const_end, |f| f.text_range().start());
|
|
|
|
|
|
|
|
let len = end - start;
|
|
|
|
let range = TextRange::from_to(0.into(), len);
|
|
|
|
|
|
|
|
let syntax = const_.syntax().text().slice(range).to_string();
|
|
|
|
|
|
|
|
format!("{} = ", syntax.trim_end())
|
|
|
|
}
|
|
|
|
|
2020-01-22 22:25:41 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::completion::{do_completion, CompletionItem, CompletionKind};
|
|
|
|
use insta::assert_debug_snapshot;
|
|
|
|
|
|
|
|
fn complete(code: &str) -> Vec<CompletionItem> {
|
2020-02-07 21:02:01 -06:00
|
|
|
do_completion(code, CompletionKind::Magic)
|
2020-01-22 22:25:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn single_function() {
|
|
|
|
let completions = complete(
|
|
|
|
r"
|
|
|
|
trait Test {
|
|
|
|
fn foo();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct T1;
|
|
|
|
|
|
|
|
impl Test for T1 {
|
2020-02-12 20:21:43 -06:00
|
|
|
fn<|>
|
2020-01-22 22:25:41 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
assert_debug_snapshot!(completions, @r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "fn foo()",
|
2020-02-12 20:21:43 -06:00
|
|
|
source_range: [140; 140),
|
|
|
|
delete: [140; 140),
|
2020-02-04 22:04:57 -06:00
|
|
|
insert: "fn foo() {}",
|
2020-01-22 22:25:41 -06:00
|
|
|
kind: Function,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###);
|
|
|
|
}
|
2020-01-28 20:30:53 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn hide_implemented_fn() {
|
|
|
|
let completions = complete(
|
|
|
|
r"
|
|
|
|
trait Test {
|
|
|
|
fn foo();
|
|
|
|
fn bar();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct T1;
|
|
|
|
|
|
|
|
impl Test for T1 {
|
|
|
|
fn foo() {}
|
|
|
|
|
2020-02-12 20:21:43 -06:00
|
|
|
fn<|>
|
2020-01-28 20:30:53 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
assert_debug_snapshot!(completions, @r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "fn bar()",
|
2020-02-12 20:21:43 -06:00
|
|
|
source_range: [195; 195),
|
|
|
|
delete: [195; 195),
|
2020-02-04 22:04:57 -06:00
|
|
|
insert: "fn bar() {}",
|
2020-01-28 20:30:53 -06:00
|
|
|
kind: Function,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###);
|
|
|
|
}
|
2020-02-04 22:04:57 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn generic_fn() {
|
|
|
|
let completions = complete(
|
|
|
|
r"
|
|
|
|
trait Test {
|
|
|
|
fn foo<T>();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct T1;
|
|
|
|
|
|
|
|
impl Test for T1 {
|
2020-02-12 20:21:43 -06:00
|
|
|
fn<|>
|
2020-02-04 22:04:57 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
assert_debug_snapshot!(completions, @r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "fn foo()",
|
2020-02-12 20:21:43 -06:00
|
|
|
source_range: [143; 143),
|
|
|
|
delete: [143; 143),
|
2020-02-04 22:04:57 -06:00
|
|
|
insert: "fn foo<T>() {}",
|
|
|
|
kind: Function,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn generic_constrait_fn() {
|
|
|
|
let completions = complete(
|
|
|
|
r"
|
|
|
|
trait Test {
|
|
|
|
fn foo<T>() where T: Into<String>;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct T1;
|
|
|
|
|
|
|
|
impl Test for T1 {
|
2020-02-12 20:21:43 -06:00
|
|
|
fn<|>
|
2020-02-04 22:04:57 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
assert_debug_snapshot!(completions, @r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "fn foo()",
|
2020-02-12 20:21:43 -06:00
|
|
|
source_range: [165; 165),
|
|
|
|
delete: [165; 165),
|
2020-02-04 22:04:57 -06:00
|
|
|
insert: "fn foo<T>()\nwhere T: Into<String> {}",
|
|
|
|
kind: Function,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###);
|
|
|
|
}
|
2020-02-10 21:02:51 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn associated_type() {
|
|
|
|
let completions = complete(
|
|
|
|
r"
|
|
|
|
trait Test {
|
|
|
|
type SomeType;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Test for () {
|
2020-02-12 20:21:43 -06:00
|
|
|
type<|>
|
2020-02-10 21:02:51 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
assert_debug_snapshot!(completions, @r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "type SomeType = ",
|
2020-02-12 20:21:43 -06:00
|
|
|
source_range: [123; 123),
|
|
|
|
delete: [123; 123),
|
2020-02-10 21:02:51 -06:00
|
|
|
insert: "type SomeType = ",
|
|
|
|
kind: TypeAlias,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn associated_const() {
|
|
|
|
let completions = complete(
|
|
|
|
r"
|
|
|
|
trait Test {
|
|
|
|
const SOME_CONST: u16;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Test for () {
|
2020-02-12 20:46:12 -06:00
|
|
|
const S<|>
|
2020-02-10 21:02:51 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
assert_debug_snapshot!(completions, @r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "const SOME_CONST: u16 = ",
|
2020-02-12 20:46:12 -06:00
|
|
|
source_range: [133; 134),
|
|
|
|
delete: [133; 134),
|
2020-02-10 21:02:51 -06:00
|
|
|
insert: "const SOME_CONST: u16 = ",
|
|
|
|
kind: Const,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn associated_const_with_default() {
|
|
|
|
let completions = complete(
|
|
|
|
r"
|
|
|
|
trait Test {
|
|
|
|
const SOME_CONST: u16 = 42;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Test for () {
|
2020-02-12 20:46:12 -06:00
|
|
|
const S<|>
|
2020-02-10 21:02:51 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
assert_debug_snapshot!(completions, @r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "const SOME_CONST: u16 = ",
|
2020-02-12 20:46:12 -06:00
|
|
|
source_range: [138; 139),
|
|
|
|
delete: [138; 139),
|
2020-02-10 21:02:51 -06:00
|
|
|
insert: "const SOME_CONST: u16 = ",
|
|
|
|
kind: Const,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###);
|
|
|
|
}
|
2020-02-09 12:24:34 -06:00
|
|
|
}
|