rust/crates/completion/src/completions/trait_impl.rs

737 lines
15 KiB
Rust
Raw Normal View History

//! 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
2020-07-30 11:02:20 -05:00
//! must be within either a `FN`, `TYPE_ALIAS`, or `CONST` node
2020-07-30 11:28:28 -05:00
//! and an direct child of an `IMPL`.
//!
//! # 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() {}<|>
//! }
//! ```
use hir::{self, HasAttrs, HasSource};
use ide_db::traits::get_missing_assoc_items;
2020-08-12 11:26:51 -05:00
use syntax::{
2020-07-30 11:28:28 -05:00
ast::{self, edit, Impl},
display::function_declaration,
2020-04-10 10:06:57 -05:00
AstNode, SyntaxKind, SyntaxNode, TextRange, T,
2020-02-11 09:40:08 -06:00
};
2020-08-12 10:03:06 -05:00
use text_edit::TextEdit;
2020-02-14 18:54:00 -06:00
use crate::{
CompletionContext,
CompletionItem,
CompletionItemKind,
CompletionKind,
Completions,
// display::function_declaration,
2020-02-14 18:54:00 -06:00
};
#[derive(Debug, PartialEq, Eq)]
enum ImplCompletionKind {
All,
Fn,
TypeAlias,
Const,
}
2020-01-22 22:38:03 -06:00
pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) {
if let Some((kind, trigger, impl_def)) = completion_match(ctx) {
get_missing_assoc_items(&ctx.sema, &impl_def).into_iter().for_each(|item| match item {
hir::AssocItem::Function(fn_item)
if kind == ImplCompletionKind::All || kind == ImplCompletionKind::Fn =>
{
add_function_impl(&trigger, acc, ctx, fn_item)
}
hir::AssocItem::TypeAlias(type_item)
if kind == ImplCompletionKind::All || kind == ImplCompletionKind::TypeAlias =>
{
add_type_alias_impl(&trigger, acc, ctx, type_item)
}
hir::AssocItem::Const(const_item)
if kind == ImplCompletionKind::All || kind == ImplCompletionKind::Const =>
{
add_const_impl(&trigger, acc, ctx, const_item)
}
_ => {}
});
}
}
fn completion_match(ctx: &CompletionContext) -> Option<(ImplCompletionKind, SyntaxNode, Impl)> {
let mut token = ctx.token.clone();
// For keywork without name like `impl .. { fn <|> }`, the current position is inside
// the whitespace token, which is outside `FN` syntax node.
// We need to follow the previous token in this case.
if token.kind() == SyntaxKind::WHITESPACE {
token = token.prev_token()?;
}
let impl_item_offset = match token.kind() {
// `impl .. { const <|> }`
// ERROR 0
// CONST_KW <- *
SyntaxKind::CONST_KW => 0,
// `impl .. { fn/type <|> }`
// FN/TYPE_ALIAS 0
// FN_KW <- *
SyntaxKind::FN_KW | SyntaxKind::TYPE_KW => 0,
// `impl .. { fn/type/const foo<|> }`
// FN/TYPE_ALIAS/CONST 1
// NAME 0
// IDENT <- *
SyntaxKind::IDENT if token.parent().kind() == SyntaxKind::NAME => 1,
// `impl .. { foo<|> }`
// MACRO_CALL 3
// PATH 2
// PATH_SEGMENT 1
// NAME_REF 0
// IDENT <- *
SyntaxKind::IDENT if token.parent().kind() == SyntaxKind::NAME_REF => 3,
_ => return None,
};
let impl_item = token.ancestors().nth(impl_item_offset)?;
// Must directly belong to an impl block.
// IMPL
// ASSOC_ITEM_LIST
// <item>
let impl_def = ast::Impl::cast(impl_item.parent()?.parent()?)?;
let kind = match impl_item.kind() {
// `impl ... { const <|> fn/type/const }`
_ if token.kind() == SyntaxKind::CONST_KW => ImplCompletionKind::Const,
SyntaxKind::CONST | SyntaxKind::ERROR => ImplCompletionKind::Const,
SyntaxKind::TYPE_ALIAS => ImplCompletionKind::TypeAlias,
SyntaxKind::FN => ImplCompletionKind::Fn,
SyntaxKind::MACRO_CALL => ImplCompletionKind::All,
_ => return None,
};
Some((kind, impl_item, impl_def))
}
fn add_function_impl(
fn_def_node: &SyntaxNode,
acc: &mut Completions,
ctx: &CompletionContext,
2020-05-26 13:12:13 -05:00
func: hir::Function,
) {
let fn_name = func.name(ctx.db).to_string();
let label = if func.assoc_fn_params(ctx.db).is_empty() {
format!("fn {}()", fn_name)
2020-08-19 06:33:51 -05:00
} else {
format!("fn {}(..)", fn_name)
};
2020-02-18 07:32:19 -06:00
let builder = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label)
.lookup_by(fn_name)
2020-02-08 15:41:25 -06:00
.set_documentation(func.docs(ctx.db));
2020-08-19 08:16:24 -05:00
let completion_kind = if func.self_param(ctx.db).is_some() {
CompletionItemKind::Method
} else {
CompletionItemKind::Function
};
2020-04-24 16:40:41 -05:00
let range = TextRange::new(fn_def_node.text_range().start(), ctx.source_range().end());
2020-07-16 14:33:11 -05:00
let function_decl = function_declaration(&func.source(ctx.db).value);
match ctx.config.snippet_cap {
Some(cap) => {
let snippet = format!("{} {{\n $0\n}}", function_decl);
builder.snippet_edit(cap, TextEdit::replace(range, snippet))
}
None => {
let header = format!("{} {{", function_decl);
builder.text_edit(TextEdit::replace(range, header))
}
}
.kind(completion_kind)
.add_to(acc);
}
fn add_type_alias_impl(
type_def_node: &SyntaxNode,
acc: &mut Completions,
ctx: &CompletionContext,
2020-05-26 13:12:13 -05:00
type_alias: hir::TypeAlias,
) {
let alias_name = type_alias.name(ctx.db).to_string();
let snippet = format!("type {} = ", alias_name);
2020-02-08 15:41:25 -06:00
2020-04-24 16:40:41 -05:00
let range = TextRange::new(type_def_node.text_range().start(), ctx.source_range().end());
2020-02-08 15:41:25 -06:00
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone())
.text_edit(TextEdit::replace(range, snippet))
.lookup_by(alias_name)
2020-02-08 15:41:25 -06:00
.kind(CompletionItemKind::TypeAlias)
.set_documentation(type_alias.docs(ctx.db))
.add_to(acc);
}
fn add_const_impl(
const_def_node: &SyntaxNode,
acc: &mut Completions,
ctx: &CompletionContext,
2020-05-26 13:12:13 -05:00
const_: hir::Const,
) {
let const_name = const_.name(ctx.db).map(|n| n.to_string());
if let Some(const_name) = const_name {
let snippet = make_const_compl_syntax(&const_.source(ctx.db).value);
2020-04-24 16:51:02 -05:00
let range = TextRange::new(const_def_node.text_range().start(), ctx.source_range().end());
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone())
.text_edit(TextEdit::replace(range, snippet))
.lookup_by(const_name)
.kind(CompletionItemKind::Const)
.set_documentation(const_.docs(ctx.db))
.add_to(acc);
}
}
2020-07-30 11:02:20 -05:00
fn make_const_compl_syntax(const_: &ast::Const) -> String {
2020-03-24 06:56:07 -05:00
let const_ = edit::remove_attrs_and_docs(const_);
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());
let end = const_
.syntax()
.children_with_tokens()
2020-04-10 10:06:57 -05:00
.find(|s| s.kind() == T![;] || s.kind() == T![=])
.map_or(const_end, |f| f.text_range().start());
let len = end - start;
2020-04-24 16:40:41 -05:00
let range = TextRange::new(0.into(), len);
let syntax = const_.syntax().text().slice(range).to_string();
format!("{} = ", syntax.trim_end())
}
#[cfg(test)]
mod tests {
2020-08-21 06:19:31 -05:00
use expect_test::{expect, Expect};
use crate::{
2020-07-04 16:22:07 -05:00
test_utils::{check_edit, completion_list},
CompletionKind,
};
2020-07-04 16:22:07 -05:00
fn check(ra_fixture: &str, expect: Expect) {
let actual = completion_list(ra_fixture, CompletionKind::Magic);
expect.assert_eq(&actual)
}
#[test]
fn name_ref_function_type_const() {
2020-07-04 16:22:07 -05:00
check(
r#"
trait Test {
type TestType;
const TEST_CONST: u16;
fn test();
}
struct T;
2020-07-04 16:22:07 -05:00
impl Test for T {
t<|>
}
"#,
2020-12-19 05:27:10 -06:00
expect![["
ta type TestType = \n\
ct const TEST_CONST: u16 = \n\
fn fn test()
"]],
);
}
#[test]
fn no_completion_inside_fn() {
2020-07-04 16:22:07 -05:00
check(
r"
trait Test { fn test(); fn test2(); }
struct T;
impl Test for T {
fn test() {
t<|>
}
}
",
expect![[""]],
);
check(
r"
trait Test { fn test(); fn test2(); }
struct T;
impl Test for T {
fn test() {
fn t<|>
}
2020-07-04 16:22:07 -05:00
}
",
expect![[""]],
);
check(
r"
trait Test { fn test(); fn test2(); }
2020-07-04 16:22:07 -05:00
struct T;
2020-07-04 16:22:07 -05:00
impl Test for T {
fn test() {
fn <|>
}
}
",
expect![[""]],
);
// https://github.com/rust-analyzer/rust-analyzer/pull/5976#issuecomment-692332191
check(
r"
trait Test { fn test(); fn test2(); }
struct T;
impl Test for T {
fn test() {
foo.<|>
}
}
",
expect![[""]],
);
check(
r"
trait Test { fn test(_: i32); fn test2(); }
struct T;
impl Test for T {
fn test(t<|>)
}
",
expect![[""]],
);
check(
r"
trait Test { fn test(_: fn()); fn test2(); }
struct T;
impl Test for T {
fn test(f: fn <|>)
}
",
expect![[""]],
);
}
#[test]
fn no_completion_inside_const() {
check(
r"
trait Test { const TEST: fn(); const TEST2: u32; type Test; fn test(); }
struct T;
impl Test for T {
const TEST: fn <|>
}
",
expect![[""]],
);
check(
r"
trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); }
struct T;
impl Test for T {
const TEST: T<|>
}
",
expect![[""]],
);
check(
r"
trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); }
struct T;
impl Test for T {
const TEST: u32 = f<|>
}
",
expect![[""]],
);
check(
r"
trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); }
struct T;
impl Test for T {
const TEST: u32 = {
2020-07-04 16:22:07 -05:00
t<|>
};
}
",
expect![[""]],
);
check(
r"
trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); }
struct T;
impl Test for T {
const TEST: u32 = {
fn <|>
};
}
",
expect![[""]],
);
check(
r"
trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); }
struct T;
impl Test for T {
const TEST: u32 = {
fn t<|>
};
}
",
expect![[""]],
);
2020-07-04 16:22:07 -05:00
}
#[test]
fn no_completion_inside_type() {
check(
r"
trait Test { type Test; type Test2; fn test(); }
struct T;
impl Test for T {
type Test = T<|>;
}
",
expect![[""]],
);
check(
r"
trait Test { type Test; type Test2; fn test(); }
struct T;
impl Test for T {
type Test = fn <|>;
2020-07-04 16:22:07 -05:00
}
",
expect![[""]],
);
}
#[test]
fn name_ref_single_function() {
2020-07-04 16:22:07 -05:00
check_edit(
"test",
r#"
trait Test {
fn test();
}
struct T;
2020-07-04 16:22:07 -05:00
impl Test for T {
t<|>
}
"#,
r#"
trait Test {
fn test();
}
struct T;
2020-07-04 16:22:07 -05:00
impl Test for T {
fn test() {
$0
}
}
"#,
);
}
#[test]
fn single_function() {
2020-07-04 16:22:07 -05:00
check_edit(
"test",
r#"
trait Test {
fn test();
}
struct T;
2020-07-04 16:22:07 -05:00
impl Test for T {
fn t<|>
}
"#,
r#"
trait Test {
fn test();
}
struct T;
2020-07-04 16:22:07 -05:00
impl Test for T {
fn test() {
$0
}
}
"#,
);
}
#[test]
fn hide_implemented_fn() {
2020-07-04 16:22:07 -05:00
check(
r#"
trait Test {
fn foo();
fn foo_bar();
}
struct T;
2020-07-04 16:22:07 -05:00
impl Test for T {
fn foo() {}
fn f<|>
}
"#,
expect![[r#"
fn fn foo_bar()
"#]],
);
}
#[test]
fn generic_fn() {
2020-07-04 16:22:07 -05:00
check_edit(
"foo",
r#"
trait Test {
fn foo<T>();
}
struct T;
2020-07-04 16:22:07 -05:00
impl Test for T {
fn f<|>
}
"#,
r#"
trait Test {
fn foo<T>();
}
struct T;
2020-07-04 16:22:07 -05:00
impl Test for T {
fn foo<T>() {
$0
}
}
"#,
);
2020-07-04 16:22:07 -05:00
check_edit(
"foo",
r#"
trait Test {
fn foo<T>() where T: Into<String>;
}
struct T;
2020-07-04 16:22:07 -05:00
impl Test for T {
fn f<|>
}
"#,
r#"
trait Test {
fn foo<T>() where T: Into<String>;
}
struct T;
2020-07-04 16:22:07 -05:00
impl Test for T {
fn foo<T>()
where T: Into<String> {
$0
}
}
"#,
);
}
#[test]
fn associated_type() {
2020-07-04 16:22:07 -05:00
check_edit(
"SomeType",
r#"
trait Test {
type SomeType;
}
2020-07-04 16:22:07 -05:00
impl Test for () {
type S<|>
}
"#,
"
trait Test {
type SomeType;
}
impl Test for () {
type SomeType = \n\
}
",
);
}
#[test]
fn associated_const() {
2020-07-04 16:22:07 -05:00
check_edit(
"SOME_CONST",
r#"
trait Test {
const SOME_CONST: u16;
}
2020-07-04 16:22:07 -05:00
impl Test for () {
const S<|>
}
"#,
"
trait Test {
const SOME_CONST: u16;
}
impl Test for () {
const SOME_CONST: u16 = \n\
}
",
);
2020-07-04 16:22:07 -05:00
check_edit(
"SOME_CONST",
r#"
trait Test {
const SOME_CONST: u16 = 92;
}
2020-07-04 16:22:07 -05:00
impl Test for () {
const S<|>
}
"#,
"
trait Test {
const SOME_CONST: u16 = 92;
}
impl Test for () {
const SOME_CONST: u16 = \n\
}
",
);
}
#[test]
fn complete_without_name() {
let test = |completion: &str, hint: &str, completed: &str, next_sibling: &str| {
println!(
"completion='{}', hint='{}', next_sibling='{}'",
completion, hint, next_sibling
);
check_edit(
completion,
&format!(
r#"
trait Test {{
type Foo;
const CONST: u16;
fn bar();
}}
struct T;
impl Test for T {{
{}
{}
}}
"#,
hint, next_sibling
),
&format!(
r#"
trait Test {{
type Foo;
const CONST: u16;
fn bar();
}}
struct T;
impl Test for T {{
{}
{}
}}
"#,
completed, next_sibling
),
)
};
// Enumerate some possible next siblings.
for next_sibling in &[
"",
"fn other_fn() {}", // `const <|> fn` -> `const fn`
"type OtherType = i32;",
"const OTHER_CONST: i32 = 0;",
"async fn other_fn() {}",
"unsafe fn other_fn() {}",
"default fn other_fn() {}",
"default type OtherType = i32;",
"default const OTHER_CONST: i32 = 0;",
] {
test("bar", "fn <|>", "fn bar() {\n $0\n}", next_sibling);
test("Foo", "type <|>", "type Foo = ", next_sibling);
test("CONST", "const <|>", "const CONST: u16 = ", next_sibling);
}
}
}