2020-02-15 16:27:04 +00:00
|
|
|
//! Completion for associated items in a trait implementation.
|
|
|
|
//!
|
|
|
|
//! This module adds the completion items related to implementing associated
|
|
|
|
//! items within a `impl Trait for Struct` block. The current context node
|
|
|
|
//! must be within either a `FN_DEF`, `TYPE_ALIAS_DEF`, or `CONST_DEF` node
|
2020-02-29 20:24:40 +00:00
|
|
|
//! and an direct child of an `IMPL_DEF`.
|
2020-02-15 16:27:04 +00:00
|
|
|
//!
|
|
|
|
//! # Examples
|
|
|
|
//!
|
|
|
|
//! Considering the following trait `impl`:
|
|
|
|
//!
|
|
|
|
//! ```ignore
|
|
|
|
//! trait SomeTrait {
|
|
|
|
//! fn foo();
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! impl SomeTrait for () {
|
|
|
|
//! fn f<|>
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! may result in the completion of the following method:
|
|
|
|
//!
|
|
|
|
//! ```ignore
|
|
|
|
//! # trait SomeTrait {
|
|
|
|
//! # fn foo();
|
|
|
|
//! # }
|
|
|
|
//!
|
|
|
|
//! impl SomeTrait for () {
|
|
|
|
//! fn foo() {}<|>
|
|
|
|
//! }
|
|
|
|
//! ```
|
2020-02-11 16:04:30 +00:00
|
|
|
|
2020-02-10 02:59:12 +00:00
|
|
|
use hir::{self, Docs, HasSource};
|
2020-02-15 00:54:00 +00:00
|
|
|
use ra_assists::utils::get_missing_impl_items;
|
2020-02-11 15:40:08 +00:00
|
|
|
use ra_syntax::{
|
2020-03-09 00:50:41 +00:00
|
|
|
ast::{self, edit, ImplDef},
|
2020-04-10 15:06:57 +00:00
|
|
|
AstNode, SyntaxKind, SyntaxNode, TextRange, T,
|
2020-02-11 15:40:08 +00:00
|
|
|
};
|
2020-02-13 03:00:47 +00:00
|
|
|
use ra_text_edit::TextEdit;
|
2020-01-23 04:25:41 +00:00
|
|
|
|
2020-02-15 00:54:00 +00:00
|
|
|
use crate::{
|
|
|
|
completion::{
|
|
|
|
CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions,
|
|
|
|
},
|
|
|
|
display::FunctionSignature,
|
|
|
|
};
|
|
|
|
|
2020-01-23 04:38:03 +00:00
|
|
|
pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) {
|
2020-03-09 00:50:41 +00:00
|
|
|
if let Some((trigger, impl_def)) = completion_match(ctx) {
|
2020-02-13 02:21:43 +00:00
|
|
|
match trigger.kind() {
|
2020-03-07 01:35:39 +00:00
|
|
|
SyntaxKind::NAME_REF => {
|
|
|
|
get_missing_impl_items(&ctx.sema, &impl_def).iter().for_each(|item| match item {
|
|
|
|
hir::AssocItem::Function(fn_item) => {
|
|
|
|
add_function_impl(&trigger, acc, ctx, &fn_item)
|
|
|
|
}
|
|
|
|
hir::AssocItem::TypeAlias(type_item) => {
|
|
|
|
add_type_alias_impl(&trigger, acc, ctx, &type_item)
|
|
|
|
}
|
|
|
|
hir::AssocItem::Const(const_item) => {
|
|
|
|
add_const_impl(&trigger, acc, ctx, &const_item)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-13 02:21:43 +00:00
|
|
|
SyntaxKind::FN_DEF => {
|
2020-02-29 20:24:40 +00:00
|
|
|
for missing_fn in get_missing_impl_items(&ctx.sema, &impl_def).iter().filter_map(
|
|
|
|
|item| match item {
|
|
|
|
hir::AssocItem::Function(fn_item) => Some(fn_item),
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
) {
|
2020-02-13 03:00:47 +00:00
|
|
|
add_function_impl(&trigger, acc, ctx, &missing_fn);
|
2020-02-13 02:21:43 +00:00
|
|
|
}
|
2020-02-13 02:46:12 +00:00
|
|
|
}
|
2020-01-29 02:30:53 +00:00
|
|
|
|
2020-02-13 02:21:43 +00:00
|
|
|
SyntaxKind::TYPE_ALIAS_DEF => {
|
2020-02-29 20:24:40 +00:00
|
|
|
for missing_fn in get_missing_impl_items(&ctx.sema, &impl_def).iter().filter_map(
|
|
|
|
|item| match item {
|
|
|
|
hir::AssocItem::TypeAlias(type_item) => Some(type_item),
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
) {
|
2020-02-13 03:00:47 +00:00
|
|
|
add_type_alias_impl(&trigger, acc, ctx, &missing_fn);
|
2020-02-13 02:21:43 +00:00
|
|
|
}
|
2020-02-13 02:46:12 +00:00
|
|
|
}
|
2020-02-13 02:21:43 +00:00
|
|
|
|
|
|
|
SyntaxKind::CONST_DEF => {
|
2020-02-29 20:24:40 +00:00
|
|
|
for missing_fn in get_missing_impl_items(&ctx.sema, &impl_def).iter().filter_map(
|
|
|
|
|item| match item {
|
|
|
|
hir::AssocItem::Const(const_item) => Some(const_item),
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
) {
|
2020-02-13 03:00:47 +00:00
|
|
|
add_const_impl(&trigger, acc, ctx, &missing_fn);
|
2020-02-13 02:21:43 +00:00
|
|
|
}
|
2020-02-13 02:46:12 +00:00
|
|
|
}
|
2020-02-13 02:21:43 +00:00
|
|
|
|
|
|
|
_ => {}
|
2020-01-23 04:25:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-09 00:50:41 +00:00
|
|
|
fn completion_match(ctx: &CompletionContext) -> Option<(SyntaxNode, ImplDef)> {
|
2020-03-09 20:01:40 +00:00
|
|
|
let (trigger, impl_def_offset) = ctx.token.ancestors().find_map(|p| match p.kind() {
|
|
|
|
SyntaxKind::FN_DEF
|
|
|
|
| SyntaxKind::TYPE_ALIAS_DEF
|
|
|
|
| SyntaxKind::CONST_DEF
|
|
|
|
| SyntaxKind::BLOCK_EXPR => Some((p, 2)),
|
|
|
|
SyntaxKind::NAME_REF => Some((p, 5)),
|
|
|
|
_ => None,
|
|
|
|
})?;
|
|
|
|
let impl_def = (0..impl_def_offset - 1)
|
|
|
|
.try_fold(trigger.parent()?, |t, _| t.parent())
|
|
|
|
.and_then(ast::ImplDef::cast)?;
|
2020-03-09 00:50:41 +00:00
|
|
|
Some((trigger, impl_def))
|
|
|
|
}
|
|
|
|
|
2020-02-13 03:00:47 +00:00
|
|
|
fn add_function_impl(
|
|
|
|
fn_def_node: &SyntaxNode,
|
|
|
|
acc: &mut Completions,
|
|
|
|
ctx: &CompletionContext,
|
|
|
|
func: &hir::Function,
|
|
|
|
) {
|
2020-02-18 13:32:19 +00:00
|
|
|
let display = FunctionSignature::from_hir(ctx.db, *func);
|
2020-02-05 04:04:57 +00:00
|
|
|
|
2020-02-14 01:10:08 +00:00
|
|
|
let fn_name = func.name(ctx.db).to_string();
|
2020-02-05 04:04:57 +00:00
|
|
|
|
2020-02-18 13:32:19 +00:00
|
|
|
let label = if !func.params(ctx.db).is_empty() {
|
2020-02-14 01:10:08 +00:00
|
|
|
format!("fn {}(..)", fn_name)
|
2020-02-05 04:04:57 +00:00
|
|
|
} else {
|
2020-02-14 01:10:08 +00:00
|
|
|
format!("fn {}()", fn_name)
|
2020-02-05 04:04:57 +00:00
|
|
|
};
|
|
|
|
|
2020-02-18 13:32:19 +00:00
|
|
|
let builder = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label)
|
2020-02-14 01:10:08 +00:00
|
|
|
.lookup_by(fn_name)
|
2020-02-08 21:41:25 +00:00
|
|
|
.set_documentation(func.docs(ctx.db));
|
2020-02-05 04:04:57 +00:00
|
|
|
|
|
|
|
let completion_kind = if func.has_self_param(ctx.db) {
|
|
|
|
CompletionItemKind::Method
|
|
|
|
} else {
|
|
|
|
CompletionItemKind::Function
|
|
|
|
};
|
2020-02-09 18:24:34 +00:00
|
|
|
|
2020-04-17 09:55:06 +00:00
|
|
|
let snippet = format!("{} {{\n $0\n}}", display);
|
2020-02-05 04:04:57 +00:00
|
|
|
|
2020-02-15 15:50:07 +00:00
|
|
|
let range = TextRange::from_to(fn_def_node.text_range().start(), ctx.source_range().end());
|
|
|
|
|
2020-04-17 09:55:06 +00:00
|
|
|
builder.snippet_edit(TextEdit::replace(range, snippet)).kind(completion_kind).add_to(acc);
|
2020-02-05 04:04:57 +00:00
|
|
|
}
|
|
|
|
|
2020-02-09 18:24:34 +00:00
|
|
|
fn add_type_alias_impl(
|
2020-02-13 03:00:47 +00:00
|
|
|
type_def_node: &SyntaxNode,
|
2020-02-09 18:24:34 +00:00
|
|
|
acc: &mut Completions,
|
|
|
|
ctx: &CompletionContext,
|
|
|
|
type_alias: &hir::TypeAlias,
|
|
|
|
) {
|
2020-02-14 01:10:08 +00:00
|
|
|
let alias_name = type_alias.name(ctx.db).to_string();
|
|
|
|
|
|
|
|
let snippet = format!("type {} = ", alias_name);
|
2020-02-08 21:41:25 +00:00
|
|
|
|
2020-02-15 15:50:07 +00:00
|
|
|
let range = TextRange::from_to(type_def_node.text_range().start(), ctx.source_range().end());
|
|
|
|
|
2020-02-08 21:41:25 +00:00
|
|
|
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone())
|
2020-02-15 15:50:07 +00:00
|
|
|
.text_edit(TextEdit::replace(range, snippet))
|
2020-02-14 01:10:08 +00:00
|
|
|
.lookup_by(alias_name)
|
2020-02-08 21:41:25 +00:00
|
|
|
.kind(CompletionItemKind::TypeAlias)
|
|
|
|
.set_documentation(type_alias.docs(ctx.db))
|
|
|
|
.add_to(acc);
|
|
|
|
}
|
|
|
|
|
2020-02-13 03:00:47 +00:00
|
|
|
fn add_const_impl(
|
|
|
|
const_def_node: &SyntaxNode,
|
|
|
|
acc: &mut Completions,
|
|
|
|
ctx: &CompletionContext,
|
|
|
|
const_: &hir::Const,
|
|
|
|
) {
|
2020-02-14 01:10:08 +00:00
|
|
|
let const_name = const_.name(ctx.db).map(|n| n.to_string());
|
2020-02-10 02:59:12 +00:00
|
|
|
|
2020-02-14 01:10:08 +00:00
|
|
|
if let Some(const_name) = const_name {
|
|
|
|
let snippet = make_const_compl_syntax(&const_.source(ctx.db).value);
|
|
|
|
|
2020-02-15 15:50:07 +00:00
|
|
|
let range =
|
|
|
|
TextRange::from_to(const_def_node.text_range().start(), ctx.source_range().end());
|
|
|
|
|
2020-02-14 01:10:08 +00:00
|
|
|
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone())
|
2020-02-15 15:50:07 +00:00
|
|
|
.text_edit(TextEdit::replace(range, snippet))
|
2020-02-14 01:10:08 +00:00
|
|
|
.lookup_by(const_name)
|
|
|
|
.kind(CompletionItemKind::Const)
|
|
|
|
.set_documentation(const_.docs(ctx.db))
|
|
|
|
.add_to(acc);
|
|
|
|
}
|
2020-02-10 02:59:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn make_const_compl_syntax(const_: &ast::ConstDef) -> String {
|
2020-03-24 11:56:07 +00:00
|
|
|
let const_ = edit::remove_attrs_and_docs(const_);
|
2020-02-11 02:55:49 +00:00
|
|
|
|
2020-02-10 02:59:12 +00:00
|
|
|
let const_start = const_.syntax().text_range().start();
|
|
|
|
let const_end = const_.syntax().text_range().end();
|
|
|
|
|
2020-02-11 15:40:08 +00:00
|
|
|
let start =
|
|
|
|
const_.syntax().first_child_or_token().map_or(const_start, |f| f.text_range().start());
|
2020-02-10 02:59:12 +00:00
|
|
|
|
|
|
|
let end = const_
|
|
|
|
.syntax()
|
|
|
|
.children_with_tokens()
|
2020-04-10 15:06:57 +00:00
|
|
|
.find(|s| s.kind() == T![;] || s.kind() == T![=])
|
2020-02-10 02:59:12 +00:00
|
|
|
.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-23 04:25:41 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use insta::assert_debug_snapshot;
|
|
|
|
|
2020-04-17 09:55:06 +00:00
|
|
|
use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
|
|
|
|
|
2020-01-23 04:25:41 +00:00
|
|
|
fn complete(code: &str) -> Vec<CompletionItem> {
|
2020-02-08 03:02:01 +00:00
|
|
|
do_completion(code, CompletionKind::Magic)
|
2020-01-23 04:25:41 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 01:35:39 +00:00
|
|
|
#[test]
|
|
|
|
fn name_ref_function_type_const() {
|
|
|
|
let completions = complete(
|
|
|
|
r"
|
|
|
|
trait Test {
|
|
|
|
type TestType;
|
|
|
|
const TEST_CONST: u16;
|
|
|
|
fn test();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct T1;
|
|
|
|
|
|
|
|
impl Test for T1 {
|
|
|
|
t<|>
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
assert_debug_snapshot!(completions, @r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "const TEST_CONST: u16 = ",
|
|
|
|
source_range: [209; 210),
|
|
|
|
delete: [209; 210),
|
|
|
|
insert: "const TEST_CONST: u16 = ",
|
|
|
|
kind: Const,
|
|
|
|
lookup: "TEST_CONST",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "fn test()",
|
|
|
|
source_range: [209; 210),
|
|
|
|
delete: [209; 210),
|
2020-04-17 09:55:06 +00:00
|
|
|
insert: "fn test() {\n $0\n}",
|
2020-03-07 01:35:39 +00:00
|
|
|
kind: Function,
|
|
|
|
lookup: "test",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "type TestType = ",
|
|
|
|
source_range: [209; 210),
|
|
|
|
delete: [209; 210),
|
|
|
|
insert: "type TestType = ",
|
|
|
|
kind: TypeAlias,
|
|
|
|
lookup: "TestType",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###);
|
|
|
|
}
|
|
|
|
|
2020-03-09 00:50:41 +00:00
|
|
|
#[test]
|
|
|
|
fn no_nested_fn_completions() {
|
|
|
|
let completions = complete(
|
|
|
|
r"
|
|
|
|
trait Test {
|
|
|
|
fn test();
|
|
|
|
fn test2();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct T1;
|
|
|
|
|
|
|
|
impl Test for T1 {
|
|
|
|
fn test() {
|
|
|
|
t<|>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
assert_debug_snapshot!(completions, @r###"[]"###);
|
|
|
|
}
|
|
|
|
|
2020-03-07 01:35:39 +00:00
|
|
|
#[test]
|
|
|
|
fn name_ref_single_function() {
|
|
|
|
let completions = complete(
|
|
|
|
r"
|
|
|
|
trait Test {
|
|
|
|
fn test();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct T1;
|
|
|
|
|
|
|
|
impl Test for T1 {
|
|
|
|
t<|>
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
assert_debug_snapshot!(completions, @r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "fn test()",
|
|
|
|
source_range: [139; 140),
|
|
|
|
delete: [139; 140),
|
2020-04-17 09:55:06 +00:00
|
|
|
insert: "fn test() {\n $0\n}",
|
2020-03-07 01:35:39 +00:00
|
|
|
kind: Function,
|
|
|
|
lookup: "test",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###);
|
|
|
|
}
|
|
|
|
|
2020-01-23 04:25:41 +00:00
|
|
|
#[test]
|
|
|
|
fn single_function() {
|
|
|
|
let completions = complete(
|
|
|
|
r"
|
|
|
|
trait Test {
|
|
|
|
fn foo();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct T1;
|
|
|
|
|
|
|
|
impl Test for T1 {
|
2020-02-14 01:10:08 +00:00
|
|
|
fn f<|>
|
2020-01-23 04:25:41 +00:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
assert_debug_snapshot!(completions, @r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "fn foo()",
|
2020-02-14 01:10:08 +00:00
|
|
|
source_range: [141; 142),
|
|
|
|
delete: [138; 142),
|
2020-04-17 09:55:06 +00:00
|
|
|
insert: "fn foo() {\n $0\n}",
|
2020-01-23 04:25:41 +00:00
|
|
|
kind: Function,
|
2020-02-14 01:10:08 +00:00
|
|
|
lookup: "foo",
|
2020-01-23 04:25:41 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
"###);
|
|
|
|
}
|
2020-01-29 02:30:53 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn hide_implemented_fn() {
|
|
|
|
let completions = complete(
|
|
|
|
r"
|
|
|
|
trait Test {
|
|
|
|
fn foo();
|
2020-02-14 01:10:08 +00:00
|
|
|
fn foo_bar();
|
2020-01-29 02:30:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct T1;
|
|
|
|
|
|
|
|
impl Test for T1 {
|
|
|
|
fn foo() {}
|
|
|
|
|
2020-02-14 01:10:08 +00:00
|
|
|
fn f<|>
|
2020-01-29 02:30:53 +00:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
assert_debug_snapshot!(completions, @r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
2020-02-14 01:10:08 +00:00
|
|
|
label: "fn foo_bar()",
|
|
|
|
source_range: [200; 201),
|
|
|
|
delete: [197; 201),
|
2020-04-17 09:55:06 +00:00
|
|
|
insert: "fn foo_bar() {\n $0\n}",
|
2020-01-29 02:30:53 +00:00
|
|
|
kind: Function,
|
2020-02-14 01:10:08 +00:00
|
|
|
lookup: "foo_bar",
|
2020-01-29 02:30:53 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
"###);
|
|
|
|
}
|
2020-02-05 04:04:57 +00:00
|
|
|
|
2020-02-15 15:50:07 +00:00
|
|
|
#[test]
|
|
|
|
fn completes_only_on_top_level() {
|
|
|
|
let completions = complete(
|
|
|
|
r"
|
|
|
|
trait Test {
|
|
|
|
fn foo();
|
|
|
|
|
|
|
|
fn foo_bar();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct T1;
|
|
|
|
|
|
|
|
impl Test for T1 {
|
|
|
|
fn foo() {
|
|
|
|
<|>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
assert_debug_snapshot!(completions, @r###"[]"###);
|
|
|
|
}
|
|
|
|
|
2020-02-05 04:04:57 +00:00
|
|
|
#[test]
|
|
|
|
fn generic_fn() {
|
|
|
|
let completions = complete(
|
|
|
|
r"
|
|
|
|
trait Test {
|
|
|
|
fn foo<T>();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct T1;
|
|
|
|
|
|
|
|
impl Test for T1 {
|
2020-02-14 01:10:08 +00:00
|
|
|
fn f<|>
|
2020-02-05 04:04:57 +00:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
assert_debug_snapshot!(completions, @r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "fn foo()",
|
2020-02-14 01:10:08 +00:00
|
|
|
source_range: [144; 145),
|
|
|
|
delete: [141; 145),
|
2020-04-17 09:55:06 +00:00
|
|
|
insert: "fn foo<T>() {\n $0\n}",
|
2020-02-05 04:04:57 +00:00
|
|
|
kind: Function,
|
2020-02-14 01:10:08 +00:00
|
|
|
lookup: "foo",
|
2020-02-05 04:04:57 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
"###);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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-14 01:10:08 +00:00
|
|
|
fn f<|>
|
2020-02-05 04:04:57 +00:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
assert_debug_snapshot!(completions, @r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "fn foo()",
|
2020-02-14 01:10:08 +00:00
|
|
|
source_range: [166; 167),
|
|
|
|
delete: [163; 167),
|
2020-04-17 09:55:06 +00:00
|
|
|
insert: "fn foo<T>()\nwhere T: Into<String> {\n $0\n}",
|
2020-02-05 04:04:57 +00:00
|
|
|
kind: Function,
|
2020-02-14 01:10:08 +00:00
|
|
|
lookup: "foo",
|
2020-02-05 04:04:57 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
"###);
|
|
|
|
}
|
2020-02-11 03:02:51 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn associated_type() {
|
|
|
|
let completions = complete(
|
|
|
|
r"
|
|
|
|
trait Test {
|
|
|
|
type SomeType;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Test for () {
|
2020-02-14 01:10:08 +00:00
|
|
|
type S<|>
|
2020-02-11 03:02:51 +00:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
assert_debug_snapshot!(completions, @r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "type SomeType = ",
|
2020-02-14 01:10:08 +00:00
|
|
|
source_range: [124; 125),
|
|
|
|
delete: [119; 125),
|
2020-02-11 03:02:51 +00:00
|
|
|
insert: "type SomeType = ",
|
|
|
|
kind: TypeAlias,
|
2020-02-14 01:10:08 +00:00
|
|
|
lookup: "SomeType",
|
2020-02-11 03:02:51 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
"###);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn associated_const() {
|
|
|
|
let completions = complete(
|
|
|
|
r"
|
|
|
|
trait Test {
|
|
|
|
const SOME_CONST: u16;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Test for () {
|
2020-02-13 02:46:12 +00:00
|
|
|
const S<|>
|
2020-02-11 03:02:51 +00:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
assert_debug_snapshot!(completions, @r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "const SOME_CONST: u16 = ",
|
2020-02-13 02:46:12 +00:00
|
|
|
source_range: [133; 134),
|
2020-02-13 03:00:47 +00:00
|
|
|
delete: [127; 134),
|
2020-02-11 03:02:51 +00:00
|
|
|
insert: "const SOME_CONST: u16 = ",
|
|
|
|
kind: Const,
|
2020-02-14 01:10:08 +00:00
|
|
|
lookup: "SOME_CONST",
|
2020-02-11 03:02:51 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
"###);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn associated_const_with_default() {
|
|
|
|
let completions = complete(
|
|
|
|
r"
|
|
|
|
trait Test {
|
|
|
|
const SOME_CONST: u16 = 42;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Test for () {
|
2020-02-13 02:46:12 +00:00
|
|
|
const S<|>
|
2020-02-11 03:02:51 +00:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
assert_debug_snapshot!(completions, @r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "const SOME_CONST: u16 = ",
|
2020-02-13 02:46:12 +00:00
|
|
|
source_range: [138; 139),
|
2020-02-13 03:00:47 +00:00
|
|
|
delete: [132; 139),
|
2020-02-11 03:02:51 +00:00
|
|
|
insert: "const SOME_CONST: u16 = ",
|
|
|
|
kind: Const,
|
2020-02-14 01:10:08 +00:00
|
|
|
lookup: "SOME_CONST",
|
2020-02-11 03:02:51 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
"###);
|
|
|
|
}
|
2020-02-09 18:24:34 +00:00
|
|
|
}
|