2020-02-15 10:27:04 -06: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
|
2020-07-30 18:02:20 +02:00
|
|
|
//! must be within either a `FN`, `TYPE_ALIAS`, or `CONST` node
|
2020-07-30 18:28:28 +02:00
|
|
|
//! and an direct child of an `IMPL`.
|
2020-02-15 10:27:04 -06:00
|
|
|
//!
|
|
|
|
//! # Examples
|
|
|
|
//!
|
|
|
|
//! Considering the following trait `impl`:
|
|
|
|
//!
|
|
|
|
//! ```ignore
|
|
|
|
//! trait SomeTrait {
|
|
|
|
//! fn foo();
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! impl SomeTrait for () {
|
2021-01-06 20:15:48 +00:00
|
|
|
//! fn f$0
|
2020-02-15 10:27:04 -06:00
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! may result in the completion of the following method:
|
|
|
|
//!
|
|
|
|
//! ```ignore
|
|
|
|
//! # trait SomeTrait {
|
|
|
|
//! # fn foo();
|
|
|
|
//! # }
|
|
|
|
//!
|
|
|
|
//! impl SomeTrait for () {
|
2021-01-06 20:15:48 +00:00
|
|
|
//! fn foo() {}$0
|
2020-02-15 10:27:04 -06:00
|
|
|
//! }
|
|
|
|
//! ```
|
2020-02-11 10:04:30 -06:00
|
|
|
|
2020-08-25 12:13:31 +02:00
|
|
|
use hir::{self, HasAttrs, HasSource};
|
2021-06-17 19:54:28 -04:00
|
|
|
use ide_db::{path_transform::PathTransform, traits::get_missing_assoc_items, SymbolKind};
|
2020-08-12 18:26:51 +02:00
|
|
|
use syntax::{
|
2021-08-14 20:38:31 +03:00
|
|
|
ast::{self, edit_in_place::AttrsOwnerEdit},
|
2020-10-18 13:09:00 +03:00
|
|
|
display::function_declaration,
|
2021-06-07 20:45:17 +02:00
|
|
|
AstNode, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, T,
|
2020-02-11 09:40:08 -06:00
|
|
|
};
|
2020-08-12 17:03:06 +02:00
|
|
|
use text_edit::TextEdit;
|
2020-01-22 22:25:41 -06:00
|
|
|
|
2021-06-07 20:45:17 +02:00
|
|
|
use crate::{CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions};
|
2020-02-14 18:54:00 -06:00
|
|
|
|
2020-09-11 23:05:10 +08: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) {
|
2021-06-07 20:45:17 +02:00
|
|
|
if let Some((kind, trigger, impl_def)) = completion_match(ctx.token.clone()) {
|
2021-06-18 12:42:13 -04:00
|
|
|
if let Some(hir_impl) = ctx.sema.to_def(&impl_def) {
|
|
|
|
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_impl)
|
2021-06-17 19:54:28 -04:00
|
|
|
}
|
2021-06-18 12:42:13 -04:00
|
|
|
hir::AssocItem::TypeAlias(type_item)
|
|
|
|
if kind == ImplCompletionKind::All || kind == ImplCompletionKind::TypeAlias =>
|
|
|
|
{
|
|
|
|
add_type_alias_impl(&trigger, acc, ctx, type_item)
|
2021-06-17 19:54:28 -04:00
|
|
|
}
|
2021-06-18 12:42:13 -04:00
|
|
|
hir::AssocItem::Const(const_item)
|
|
|
|
if kind == ImplCompletionKind::All || kind == ImplCompletionKind::Const =>
|
|
|
|
{
|
|
|
|
add_const_impl(&trigger, acc, ctx, const_item, hir_impl)
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
});
|
|
|
|
}
|
2020-01-22 22:25:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 20:45:17 +02:00
|
|
|
fn completion_match(mut token: SyntaxToken) -> Option<(ImplCompletionKind, SyntaxNode, ast::Impl)> {
|
2021-01-30 18:19:21 +03:00
|
|
|
// For keyword without name like `impl .. { fn $0 }`, the current position is inside
|
2020-09-11 23:05:10 +08:00
|
|
|
// 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()?;
|
|
|
|
}
|
|
|
|
|
2021-01-30 18:19:21 +03:00
|
|
|
let parent_kind = token.parent().map_or(SyntaxKind::EOF, |it| it.kind());
|
2020-09-16 01:16:06 +08:00
|
|
|
let impl_item_offset = match token.kind() {
|
2021-01-06 20:15:48 +00:00
|
|
|
// `impl .. { const $0 }`
|
2020-09-16 01:16:06 +08:00
|
|
|
// ERROR 0
|
|
|
|
// CONST_KW <- *
|
2021-01-10 16:40:52 +01:00
|
|
|
T![const] => 0,
|
2021-01-06 20:15:48 +00:00
|
|
|
// `impl .. { fn/type $0 }`
|
2020-09-16 01:16:06 +08:00
|
|
|
// FN/TYPE_ALIAS 0
|
|
|
|
// FN_KW <- *
|
2021-01-10 16:40:52 +01:00
|
|
|
T![fn] | T![type] => 0,
|
2021-01-06 20:15:48 +00:00
|
|
|
// `impl .. { fn/type/const foo$0 }`
|
2020-09-16 01:16:06 +08:00
|
|
|
// FN/TYPE_ALIAS/CONST 1
|
|
|
|
// NAME 0
|
|
|
|
// IDENT <- *
|
2021-01-30 18:19:21 +03:00
|
|
|
SyntaxKind::IDENT if parent_kind == SyntaxKind::NAME => 1,
|
2021-01-06 20:15:48 +00:00
|
|
|
// `impl .. { foo$0 }`
|
2020-09-16 01:16:06 +08:00
|
|
|
// MACRO_CALL 3
|
|
|
|
// PATH 2
|
|
|
|
// PATH_SEGMENT 1
|
|
|
|
// NAME_REF 0
|
|
|
|
// IDENT <- *
|
2021-01-30 18:19:21 +03:00
|
|
|
SyntaxKind::IDENT if parent_kind == SyntaxKind::NAME_REF => 3,
|
2020-09-16 01:16:06 +08:00
|
|
|
_ => return None,
|
|
|
|
};
|
2020-09-11 23:05:10 +08:00
|
|
|
|
2020-09-16 01:16:06 +08:00
|
|
|
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() {
|
2021-01-06 20:15:48 +00:00
|
|
|
// `impl ... { const $0 fn/type/const }`
|
2021-01-10 16:40:52 +01:00
|
|
|
_ if token.kind() == T![const] => ImplCompletionKind::Const,
|
2020-09-16 01:16:06 +08:00
|
|
|
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))
|
2020-03-08 17:50:41 -07:00
|
|
|
}
|
|
|
|
|
2020-02-12 21:00:47 -06:00
|
|
|
fn add_function_impl(
|
|
|
|
fn_def_node: &SyntaxNode,
|
|
|
|
acc: &mut Completions,
|
|
|
|
ctx: &CompletionContext,
|
2020-05-26 14:12:13 -04:00
|
|
|
func: hir::Function,
|
2021-06-17 19:54:28 -04:00
|
|
|
impl_def: hir::Impl,
|
2020-02-12 21:00:47 -06:00
|
|
|
) {
|
2020-02-13 19:10:08 -06:00
|
|
|
let fn_name = func.name(ctx.db).to_string();
|
2020-02-04 22:04:57 -06:00
|
|
|
|
2020-12-01 13:53:12 +03:00
|
|
|
let label = if func.assoc_fn_params(ctx.db).is_empty() {
|
2020-02-13 19:10:08 -06:00
|
|
|
format!("fn {}()", fn_name)
|
2020-08-19 13:33:51 +02:00
|
|
|
} else {
|
|
|
|
format!("fn {}(..)", fn_name)
|
2020-02-04 22:04:57 -06:00
|
|
|
};
|
|
|
|
|
2021-03-12 12:12:32 +03:00
|
|
|
let mut item = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label);
|
|
|
|
item.lookup_by(fn_name).set_documentation(func.docs(ctx.db));
|
2020-02-04 22:04:57 -06:00
|
|
|
|
2020-08-19 15:16:24 +02:00
|
|
|
let completion_kind = if func.self_param(ctx.db).is_some() {
|
2020-02-04 22:04:57 -06:00
|
|
|
CompletionItemKind::Method
|
|
|
|
} else {
|
2021-01-20 18:38:12 +01:00
|
|
|
CompletionItemKind::SymbolKind(SymbolKind::Function)
|
2020-02-04 22:04:57 -06:00
|
|
|
};
|
2021-05-06 15:56:48 +02:00
|
|
|
let range = replacement_range(ctx, fn_def_node);
|
2021-06-17 19:54:28 -04:00
|
|
|
|
|
|
|
if let Some(source) = func.source(ctx.db) {
|
|
|
|
let assoc_item = ast::AssocItem::Fn(source.value);
|
|
|
|
if let Some(transformed_item) = get_transformed_assoc_item(ctx, assoc_item, impl_def) {
|
|
|
|
let transformed_fn = match transformed_item {
|
|
|
|
ast::AssocItem::Fn(func) => func,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let function_decl = function_declaration(&transformed_fn);
|
|
|
|
match ctx.config.snippet_cap {
|
|
|
|
Some(cap) => {
|
|
|
|
let snippet = format!("{} {{\n $0\n}}", function_decl);
|
|
|
|
item.snippet_edit(cap, TextEdit::replace(range, snippet));
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
let header = format!("{} {{", function_decl);
|
|
|
|
item.text_edit(TextEdit::replace(range, header));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
item.kind(completion_kind);
|
|
|
|
item.add_to(acc);
|
|
|
|
}
|
2020-04-24 02:26:38 +02:00
|
|
|
}
|
2020-02-04 22:04:57 -06:00
|
|
|
}
|
|
|
|
|
2021-06-17 19:54:28 -04:00
|
|
|
/// Transform a relevant associated item to inline generics from the impl, remove attrs and docs, etc.
|
|
|
|
fn get_transformed_assoc_item(
|
|
|
|
ctx: &CompletionContext,
|
|
|
|
assoc_item: ast::AssocItem,
|
|
|
|
impl_def: hir::Impl,
|
|
|
|
) -> Option<ast::AssocItem> {
|
|
|
|
let assoc_item = assoc_item.clone_for_update();
|
|
|
|
let trait_ = impl_def.trait_(ctx.db)?;
|
|
|
|
let source_scope = &ctx.sema.scope_for_def(trait_);
|
|
|
|
let target_scope = &ctx.sema.scope(impl_def.source(ctx.db)?.syntax().value);
|
2021-08-10 14:39:56 +02:00
|
|
|
let transform = PathTransform::trait_impl(
|
2021-06-17 19:54:28 -04:00
|
|
|
target_scope,
|
2021-08-10 14:39:56 +02:00
|
|
|
source_scope,
|
|
|
|
trait_,
|
|
|
|
impl_def.source(ctx.db)?.value,
|
|
|
|
);
|
2021-06-17 19:54:28 -04:00
|
|
|
|
2021-08-10 14:39:56 +02:00
|
|
|
transform.apply(assoc_item.syntax());
|
2021-08-14 20:17:16 +03:00
|
|
|
if let ast::AssocItem::Fn(func) = &assoc_item {
|
2021-08-14 20:38:31 +03:00
|
|
|
func.remove_attrs_and_docs()
|
2021-08-14 20:17:16 +03:00
|
|
|
}
|
|
|
|
Some(assoc_item)
|
2021-06-17 19:54:28 -04:00
|
|
|
}
|
|
|
|
|
2020-02-09 12:24:34 -06:00
|
|
|
fn add_type_alias_impl(
|
2020-02-12 21:00:47 -06:00
|
|
|
type_def_node: &SyntaxNode,
|
2020-02-09 12:24:34 -06:00
|
|
|
acc: &mut Completions,
|
|
|
|
ctx: &CompletionContext,
|
2020-05-26 14:12:13 -04:00
|
|
|
type_alias: hir::TypeAlias,
|
2020-02-09 12:24:34 -06:00
|
|
|
) {
|
2020-02-13 19:10:08 -06:00
|
|
|
let alias_name = type_alias.name(ctx.db).to_string();
|
|
|
|
|
|
|
|
let snippet = format!("type {} = ", alias_name);
|
2020-02-08 15:41:25 -06:00
|
|
|
|
2021-05-06 15:56:48 +02:00
|
|
|
let range = replacement_range(ctx, type_def_node);
|
2021-03-12 12:12:32 +03:00
|
|
|
let mut item = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone());
|
|
|
|
item.text_edit(TextEdit::replace(range, snippet))
|
2020-02-13 19:10:08 -06:00
|
|
|
.lookup_by(alias_name)
|
2021-01-20 18:38:12 +01:00
|
|
|
.kind(SymbolKind::TypeAlias)
|
2021-03-11 17:46:41 +02:00
|
|
|
.set_documentation(type_alias.docs(ctx.db));
|
2021-03-12 12:12:32 +03:00
|
|
|
item.add_to(acc);
|
2020-02-08 15:41:25 -06:00
|
|
|
}
|
|
|
|
|
2020-02-12 21:00:47 -06:00
|
|
|
fn add_const_impl(
|
|
|
|
const_def_node: &SyntaxNode,
|
|
|
|
acc: &mut Completions,
|
|
|
|
ctx: &CompletionContext,
|
2020-05-26 14:12:13 -04:00
|
|
|
const_: hir::Const,
|
2021-06-17 19:54:28 -04:00
|
|
|
impl_def: hir::Impl,
|
2020-02-12 21:00:47 -06:00
|
|
|
) {
|
2020-02-13 19:10:08 -06:00
|
|
|
let const_name = const_.name(ctx.db).map(|n| n.to_string());
|
2020-02-09 20:59:12 -06:00
|
|
|
|
2020-02-13 19:10:08 -06:00
|
|
|
if let Some(const_name) = const_name {
|
2021-01-01 17:30:13 +11:00
|
|
|
if let Some(source) = const_.source(ctx.db) {
|
2021-06-17 19:54:28 -04:00
|
|
|
let assoc_item = ast::AssocItem::Const(source.value);
|
|
|
|
if let Some(transformed_item) = get_transformed_assoc_item(ctx, assoc_item, impl_def) {
|
|
|
|
let transformed_const = match transformed_item {
|
|
|
|
ast::AssocItem::Const(const_) => const_,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let snippet = make_const_compl_syntax(&transformed_const);
|
|
|
|
|
|
|
|
let range = replacement_range(ctx, const_def_node);
|
|
|
|
let mut item =
|
|
|
|
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone());
|
|
|
|
item.text_edit(TextEdit::replace(range, snippet))
|
|
|
|
.lookup_by(const_name)
|
|
|
|
.kind(SymbolKind::Const)
|
|
|
|
.set_documentation(const_.docs(ctx.db));
|
|
|
|
item.add_to(acc);
|
|
|
|
}
|
2021-01-01 17:30:13 +11:00
|
|
|
}
|
2020-02-13 19:10:08 -06:00
|
|
|
}
|
2020-02-09 20:59:12 -06:00
|
|
|
}
|
|
|
|
|
2020-07-30 18:02:20 +02:00
|
|
|
fn make_const_compl_syntax(const_: &ast::Const) -> String {
|
2021-08-14 20:38:31 +03:00
|
|
|
const_.remove_attrs_and_docs();
|
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()
|
2020-04-10 17:06:57 +02:00
|
|
|
.find(|s| s.kind() == T![;] || s.kind() == T![=])
|
2020-02-09 20:59:12 -06:00
|
|
|
.map_or(const_end, |f| f.text_range().start());
|
|
|
|
|
|
|
|
let len = end - start;
|
2020-04-24 23:40:41 +02:00
|
|
|
let range = TextRange::new(0.into(), len);
|
2020-02-09 20:59:12 -06:00
|
|
|
|
|
|
|
let syntax = const_.syntax().text().slice(range).to_string();
|
|
|
|
|
|
|
|
format!("{} = ", syntax.trim_end())
|
|
|
|
}
|
|
|
|
|
2021-05-06 15:56:48 +02:00
|
|
|
fn replacement_range(ctx: &CompletionContext, item: &SyntaxNode) -> TextRange {
|
|
|
|
let first_child = item
|
|
|
|
.children_with_tokens()
|
|
|
|
.find(|child| {
|
2021-05-06 22:39:51 +08:00
|
|
|
!matches!(child.kind(), SyntaxKind::COMMENT | SyntaxKind::WHITESPACE | SyntaxKind::ATTR)
|
2021-05-06 15:56:48 +02:00
|
|
|
})
|
2021-05-06 22:39:51 +08:00
|
|
|
.unwrap_or_else(|| SyntaxElement::Node(item.clone()));
|
2021-05-06 15:56:48 +02:00
|
|
|
|
|
|
|
TextRange::new(first_child.text_range().start(), ctx.source_range().end())
|
|
|
|
}
|
|
|
|
|
2020-01-22 22:25:41 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-08-21 13:19:31 +02:00
|
|
|
use expect_test::{expect, Expect};
|
2020-01-22 22:25:41 -06:00
|
|
|
|
2020-10-18 13:09:00 +03:00
|
|
|
use crate::{
|
2021-06-16 21:45:02 +02:00
|
|
|
tests::{check_edit, filtered_completion_list},
|
2020-07-04 23:22:07 +02:00
|
|
|
CompletionKind,
|
|
|
|
};
|
2020-04-17 11:55:06 +02:00
|
|
|
|
2020-07-04 23:22:07 +02:00
|
|
|
fn check(ra_fixture: &str, expect: Expect) {
|
2021-06-16 21:45:02 +02:00
|
|
|
let actual = filtered_completion_list(ra_fixture, CompletionKind::Magic);
|
2020-07-04 23:22:07 +02:00
|
|
|
expect.assert_eq(&actual)
|
2020-01-22 22:25:41 -06:00
|
|
|
}
|
|
|
|
|
2020-03-08 17:50:41 -07:00
|
|
|
#[test]
|
2020-09-16 01:16:06 +08:00
|
|
|
fn no_completion_inside_fn() {
|
2020-07-04 23:22:07 +02:00
|
|
|
check(
|
2020-03-08 17:50:41 -07:00
|
|
|
r"
|
2020-09-16 01:16:06 +08:00
|
|
|
trait Test { fn test(); fn test2(); }
|
|
|
|
struct T;
|
|
|
|
|
|
|
|
impl Test for T {
|
|
|
|
fn test() {
|
2021-01-06 20:15:48 +00:00
|
|
|
t$0
|
2020-09-16 01:16:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
expect![[""]],
|
|
|
|
);
|
|
|
|
|
|
|
|
check(
|
|
|
|
r"
|
|
|
|
trait Test { fn test(); fn test2(); }
|
|
|
|
struct T;
|
|
|
|
|
|
|
|
impl Test for T {
|
|
|
|
fn test() {
|
2021-01-06 20:15:48 +00:00
|
|
|
fn t$0
|
2020-09-16 01:16:06 +08:00
|
|
|
}
|
2020-07-04 23:22:07 +02:00
|
|
|
}
|
2020-09-16 01:16:06 +08:00
|
|
|
",
|
|
|
|
expect![[""]],
|
|
|
|
);
|
|
|
|
|
|
|
|
check(
|
|
|
|
r"
|
|
|
|
trait Test { fn test(); fn test2(); }
|
2020-07-04 23:22:07 +02:00
|
|
|
struct T;
|
2020-03-08 17:50:41 -07:00
|
|
|
|
2020-07-04 23:22:07 +02:00
|
|
|
impl Test for T {
|
|
|
|
fn test() {
|
2021-01-06 20:15:48 +00:00
|
|
|
fn $0
|
2020-09-16 01:16:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
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() {
|
2021-01-06 20:15:48 +00:00
|
|
|
foo.$0
|
2020-09-16 01:16:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
expect![[""]],
|
|
|
|
);
|
|
|
|
|
|
|
|
check(
|
|
|
|
r"
|
|
|
|
trait Test { fn test(_: i32); fn test2(); }
|
|
|
|
struct T;
|
|
|
|
|
|
|
|
impl Test for T {
|
2021-01-06 20:15:48 +00:00
|
|
|
fn test(t$0)
|
2020-09-16 01:16:06 +08:00
|
|
|
}
|
|
|
|
",
|
|
|
|
expect![[""]],
|
|
|
|
);
|
|
|
|
|
|
|
|
check(
|
|
|
|
r"
|
|
|
|
trait Test { fn test(_: fn()); fn test2(); }
|
|
|
|
struct T;
|
|
|
|
|
|
|
|
impl Test for T {
|
2021-01-06 20:15:48 +00:00
|
|
|
fn test(f: fn $0)
|
2020-09-16 01:16:06 +08:00
|
|
|
}
|
|
|
|
",
|
|
|
|
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 {
|
2021-01-06 20:15:48 +00:00
|
|
|
const TEST: fn $0
|
2020-09-16 01:16:06 +08:00
|
|
|
}
|
|
|
|
",
|
|
|
|
expect![[""]],
|
|
|
|
);
|
|
|
|
|
|
|
|
check(
|
|
|
|
r"
|
|
|
|
trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); }
|
|
|
|
struct T;
|
|
|
|
|
|
|
|
impl Test for T {
|
2021-01-06 20:15:48 +00:00
|
|
|
const TEST: T$0
|
2020-09-16 01:16:06 +08:00
|
|
|
}
|
|
|
|
",
|
|
|
|
expect![[""]],
|
|
|
|
);
|
|
|
|
|
|
|
|
check(
|
|
|
|
r"
|
|
|
|
trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); }
|
|
|
|
struct T;
|
|
|
|
|
|
|
|
impl Test for T {
|
2021-01-06 20:15:48 +00:00
|
|
|
const TEST: u32 = f$0
|
2020-09-16 01:16:06 +08:00
|
|
|
}
|
|
|
|
",
|
|
|
|
expect![[""]],
|
|
|
|
);
|
|
|
|
|
|
|
|
check(
|
|
|
|
r"
|
|
|
|
trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); }
|
|
|
|
struct T;
|
|
|
|
|
|
|
|
impl Test for T {
|
|
|
|
const TEST: u32 = {
|
2021-01-06 20:15:48 +00:00
|
|
|
t$0
|
2020-09-16 01:16:06 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
",
|
|
|
|
expect![[""]],
|
|
|
|
);
|
|
|
|
|
|
|
|
check(
|
|
|
|
r"
|
|
|
|
trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); }
|
|
|
|
struct T;
|
|
|
|
|
|
|
|
impl Test for T {
|
|
|
|
const TEST: u32 = {
|
2021-01-06 20:15:48 +00:00
|
|
|
fn $0
|
2020-09-16 01:16:06 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
",
|
|
|
|
expect![[""]],
|
|
|
|
);
|
|
|
|
|
|
|
|
check(
|
|
|
|
r"
|
|
|
|
trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); }
|
|
|
|
struct T;
|
|
|
|
|
|
|
|
impl Test for T {
|
|
|
|
const TEST: u32 = {
|
2021-01-06 20:15:48 +00:00
|
|
|
fn t$0
|
2020-09-16 01:16:06 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
",
|
|
|
|
expect![[""]],
|
|
|
|
);
|
2020-07-04 23:22:07 +02:00
|
|
|
}
|
2020-09-16 01:16:06 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_completion_inside_type() {
|
|
|
|
check(
|
|
|
|
r"
|
|
|
|
trait Test { type Test; type Test2; fn test(); }
|
|
|
|
struct T;
|
|
|
|
|
|
|
|
impl Test for T {
|
2021-01-06 20:15:48 +00:00
|
|
|
type Test = T$0;
|
2020-09-16 01:16:06 +08:00
|
|
|
}
|
|
|
|
",
|
|
|
|
expect![[""]],
|
|
|
|
);
|
|
|
|
|
|
|
|
check(
|
|
|
|
r"
|
|
|
|
trait Test { type Test; type Test2; fn test(); }
|
|
|
|
struct T;
|
|
|
|
|
|
|
|
impl Test for T {
|
2021-01-06 20:15:48 +00:00
|
|
|
type Test = fn $0;
|
2020-07-04 23:22:07 +02:00
|
|
|
}
|
|
|
|
",
|
|
|
|
expect![[""]],
|
2020-03-08 17:50:41 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-06 17:35:39 -08:00
|
|
|
#[test]
|
|
|
|
fn name_ref_single_function() {
|
2020-07-04 23:22:07 +02:00
|
|
|
check_edit(
|
|
|
|
"test",
|
|
|
|
r#"
|
|
|
|
trait Test {
|
|
|
|
fn test();
|
|
|
|
}
|
|
|
|
struct T;
|
2020-03-06 17:35:39 -08:00
|
|
|
|
2020-07-04 23:22:07 +02:00
|
|
|
impl Test for T {
|
2021-01-06 20:15:48 +00:00
|
|
|
t$0
|
2020-07-04 23:22:07 +02:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
trait Test {
|
|
|
|
fn test();
|
|
|
|
}
|
|
|
|
struct T;
|
2020-03-06 17:35:39 -08:00
|
|
|
|
2020-07-04 23:22:07 +02:00
|
|
|
impl Test for T {
|
|
|
|
fn test() {
|
|
|
|
$0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2020-03-06 17:35:39 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-22 22:25:41 -06:00
|
|
|
#[test]
|
|
|
|
fn single_function() {
|
2020-07-04 23:22:07 +02:00
|
|
|
check_edit(
|
|
|
|
"test",
|
|
|
|
r#"
|
|
|
|
trait Test {
|
|
|
|
fn test();
|
|
|
|
}
|
|
|
|
struct T;
|
2020-01-22 22:25:41 -06:00
|
|
|
|
2020-07-04 23:22:07 +02:00
|
|
|
impl Test for T {
|
2021-01-06 20:15:48 +00:00
|
|
|
fn t$0
|
2020-07-04 23:22:07 +02:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
trait Test {
|
|
|
|
fn test();
|
|
|
|
}
|
|
|
|
struct T;
|
2020-01-22 22:25:41 -06:00
|
|
|
|
2020-07-04 23:22:07 +02:00
|
|
|
impl Test for T {
|
|
|
|
fn test() {
|
|
|
|
$0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2020-01-22 22:25:41 -06:00
|
|
|
);
|
|
|
|
}
|
2020-01-28 20:30:53 -06:00
|
|
|
|
2020-02-04 22:04:57 -06:00
|
|
|
#[test]
|
|
|
|
fn generic_fn() {
|
2020-07-04 23:22:07 +02:00
|
|
|
check_edit(
|
|
|
|
"foo",
|
|
|
|
r#"
|
|
|
|
trait Test {
|
|
|
|
fn foo<T>();
|
|
|
|
}
|
|
|
|
struct T;
|
2020-02-04 22:04:57 -06:00
|
|
|
|
2020-07-04 23:22:07 +02:00
|
|
|
impl Test for T {
|
2021-01-06 20:15:48 +00:00
|
|
|
fn f$0
|
2020-07-04 23:22:07 +02:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
trait Test {
|
|
|
|
fn foo<T>();
|
|
|
|
}
|
|
|
|
struct T;
|
2020-02-04 22:04:57 -06:00
|
|
|
|
2020-07-04 23:22:07 +02:00
|
|
|
impl Test for T {
|
|
|
|
fn foo<T>() {
|
|
|
|
$0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2020-02-04 22:04:57 -06:00
|
|
|
);
|
2020-07-04 23:22:07 +02:00
|
|
|
check_edit(
|
|
|
|
"foo",
|
|
|
|
r#"
|
|
|
|
trait Test {
|
|
|
|
fn foo<T>() where T: Into<String>;
|
|
|
|
}
|
|
|
|
struct T;
|
2020-02-04 22:04:57 -06:00
|
|
|
|
2020-07-04 23:22:07 +02:00
|
|
|
impl Test for T {
|
2021-01-06 20:15:48 +00:00
|
|
|
fn f$0
|
2020-07-04 23:22:07 +02:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
trait Test {
|
|
|
|
fn foo<T>() where T: Into<String>;
|
|
|
|
}
|
|
|
|
struct T;
|
2020-02-04 22:04:57 -06:00
|
|
|
|
2020-07-04 23:22:07 +02:00
|
|
|
impl Test for T {
|
|
|
|
fn foo<T>()
|
|
|
|
where T: Into<String> {
|
|
|
|
$0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2020-02-04 22:04:57 -06:00
|
|
|
);
|
|
|
|
}
|
2020-02-10 21:02:51 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn associated_type() {
|
2020-07-04 23:22:07 +02:00
|
|
|
check_edit(
|
|
|
|
"SomeType",
|
|
|
|
r#"
|
|
|
|
trait Test {
|
|
|
|
type SomeType;
|
|
|
|
}
|
2020-02-10 21:02:51 -06:00
|
|
|
|
2020-07-04 23:22:07 +02:00
|
|
|
impl Test for () {
|
2021-01-06 20:15:48 +00:00
|
|
|
type S$0
|
2020-07-04 23:22:07 +02:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
"
|
|
|
|
trait Test {
|
|
|
|
type SomeType;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Test for () {
|
|
|
|
type SomeType = \n\
|
|
|
|
}
|
|
|
|
",
|
2020-02-10 21:02:51 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn associated_const() {
|
2020-07-04 23:22:07 +02:00
|
|
|
check_edit(
|
|
|
|
"SOME_CONST",
|
|
|
|
r#"
|
|
|
|
trait Test {
|
|
|
|
const SOME_CONST: u16;
|
|
|
|
}
|
2020-02-10 21:02:51 -06:00
|
|
|
|
2020-07-04 23:22:07 +02:00
|
|
|
impl Test for () {
|
2021-01-06 20:15:48 +00:00
|
|
|
const S$0
|
2020-07-04 23:22:07 +02:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
"
|
|
|
|
trait Test {
|
|
|
|
const SOME_CONST: u16;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Test for () {
|
|
|
|
const SOME_CONST: u16 = \n\
|
|
|
|
}
|
|
|
|
",
|
2020-02-10 21:02:51 -06:00
|
|
|
);
|
|
|
|
|
2020-07-04 23:22:07 +02:00
|
|
|
check_edit(
|
|
|
|
"SOME_CONST",
|
|
|
|
r#"
|
|
|
|
trait Test {
|
|
|
|
const SOME_CONST: u16 = 92;
|
|
|
|
}
|
2020-02-10 21:02:51 -06:00
|
|
|
|
2020-07-04 23:22:07 +02:00
|
|
|
impl Test for () {
|
2021-01-06 20:15:48 +00:00
|
|
|
const S$0
|
2020-07-04 23:22:07 +02:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
"
|
|
|
|
trait Test {
|
|
|
|
const SOME_CONST: u16 = 92;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Test for () {
|
|
|
|
const SOME_CONST: u16 = \n\
|
|
|
|
}
|
|
|
|
",
|
2020-02-10 21:02:51 -06:00
|
|
|
);
|
|
|
|
}
|
2020-09-11 23:05:10 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn complete_without_name() {
|
|
|
|
let test = |completion: &str, hint: &str, completed: &str, next_sibling: &str| {
|
|
|
|
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 &[
|
|
|
|
"",
|
2021-01-06 20:15:48 +00:00
|
|
|
"fn other_fn() {}", // `const $0 fn` -> `const fn`
|
2020-09-11 23:05:10 +08:00
|
|
|
"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;",
|
|
|
|
] {
|
2021-01-06 20:15:48 +00:00
|
|
|
test("bar", "fn $0", "fn bar() {\n $0\n}", next_sibling);
|
|
|
|
test("Foo", "type $0", "type Foo = ", next_sibling);
|
|
|
|
test("CONST", "const $0", "const CONST: u16 = ", next_sibling);
|
2020-09-11 23:05:10 +08:00
|
|
|
}
|
|
|
|
}
|
2021-05-06 15:56:48 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn snippet_does_not_overwrite_comment_or_attr() {
|
|
|
|
let test = |completion: &str, hint: &str, completed: &str| {
|
|
|
|
check_edit(
|
|
|
|
completion,
|
|
|
|
&format!(
|
|
|
|
r#"
|
|
|
|
trait Foo {{
|
|
|
|
type Type;
|
|
|
|
fn function();
|
|
|
|
const CONST: i32 = 0;
|
|
|
|
}}
|
|
|
|
struct T;
|
|
|
|
|
|
|
|
impl Foo for T {{
|
|
|
|
// Comment
|
|
|
|
#[bar]
|
|
|
|
{}
|
|
|
|
}}
|
|
|
|
"#,
|
|
|
|
hint
|
|
|
|
),
|
|
|
|
&format!(
|
|
|
|
r#"
|
|
|
|
trait Foo {{
|
|
|
|
type Type;
|
|
|
|
fn function();
|
|
|
|
const CONST: i32 = 0;
|
|
|
|
}}
|
|
|
|
struct T;
|
|
|
|
|
|
|
|
impl Foo for T {{
|
|
|
|
// Comment
|
|
|
|
#[bar]
|
|
|
|
{}
|
|
|
|
}}
|
|
|
|
"#,
|
|
|
|
completed
|
|
|
|
),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
test("function", "fn f$0", "fn function() {\n $0\n}");
|
|
|
|
test("Type", "type T$0", "type Type = ");
|
|
|
|
test("CONST", "const C$0", "const CONST: i32 = ");
|
|
|
|
}
|
2021-06-17 19:54:28 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn generics_are_inlined_in_return_type() {
|
|
|
|
check_edit(
|
|
|
|
"function",
|
|
|
|
r#"
|
|
|
|
trait Foo<T> {
|
|
|
|
fn function() -> T;
|
|
|
|
}
|
|
|
|
struct Bar;
|
|
|
|
|
|
|
|
impl Foo<u32> for Bar {
|
|
|
|
fn f$0
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
trait Foo<T> {
|
|
|
|
fn function() -> T;
|
|
|
|
}
|
|
|
|
struct Bar;
|
|
|
|
|
|
|
|
impl Foo<u32> for Bar {
|
|
|
|
fn function() -> u32 {
|
|
|
|
$0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn generics_are_inlined_in_parameter() {
|
|
|
|
check_edit(
|
|
|
|
"function",
|
|
|
|
r#"
|
|
|
|
trait Foo<T> {
|
|
|
|
fn function(bar: T);
|
|
|
|
}
|
|
|
|
struct Bar;
|
|
|
|
|
|
|
|
impl Foo<u32> for Bar {
|
|
|
|
fn f$0
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
trait Foo<T> {
|
|
|
|
fn function(bar: T);
|
|
|
|
}
|
|
|
|
struct Bar;
|
|
|
|
|
|
|
|
impl Foo<u32> for Bar {
|
|
|
|
fn function(bar: u32) {
|
|
|
|
$0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn generics_are_inlined_when_part_of_other_types() {
|
|
|
|
check_edit(
|
|
|
|
"function",
|
|
|
|
r#"
|
|
|
|
trait Foo<T> {
|
|
|
|
fn function(bar: Vec<T>);
|
|
|
|
}
|
|
|
|
struct Bar;
|
|
|
|
|
|
|
|
impl Foo<u32> for Bar {
|
|
|
|
fn f$0
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
trait Foo<T> {
|
|
|
|
fn function(bar: Vec<T>);
|
|
|
|
}
|
|
|
|
struct Bar;
|
|
|
|
|
|
|
|
impl Foo<u32> for Bar {
|
|
|
|
fn function(bar: Vec<u32>) {
|
|
|
|
$0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn generics_are_inlined_complex() {
|
|
|
|
check_edit(
|
|
|
|
"function",
|
|
|
|
r#"
|
|
|
|
trait Foo<T, U, V> {
|
|
|
|
fn function(bar: Vec<T>, baz: U) -> Arc<Vec<V>>;
|
|
|
|
}
|
|
|
|
struct Bar;
|
|
|
|
|
|
|
|
impl Foo<u32, Vec<usize>, u8> for Bar {
|
|
|
|
fn f$0
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
trait Foo<T, U, V> {
|
|
|
|
fn function(bar: Vec<T>, baz: U) -> Arc<Vec<V>>;
|
|
|
|
}
|
|
|
|
struct Bar;
|
|
|
|
|
|
|
|
impl Foo<u32, Vec<usize>, u8> for Bar {
|
|
|
|
fn function(bar: Vec<u32>, baz: Vec<usize>) -> Arc<Vec<u8>> {
|
|
|
|
$0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn generics_are_inlined_in_associated_const() {
|
|
|
|
check_edit(
|
|
|
|
"BAR",
|
|
|
|
r#"
|
|
|
|
trait Foo<T> {
|
|
|
|
const BAR: T;
|
|
|
|
}
|
|
|
|
struct Bar;
|
|
|
|
|
|
|
|
impl Foo<u32> for Bar {
|
2021-06-18 02:14:00 -04:00
|
|
|
const B$0;
|
2021-06-17 19:54:28 -04:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
trait Foo<T> {
|
|
|
|
const BAR: T;
|
|
|
|
}
|
|
|
|
struct Bar;
|
|
|
|
|
|
|
|
impl Foo<u32> for Bar {
|
2021-06-18 02:14:00 -04:00
|
|
|
const BAR: u32 = ;
|
2021-06-17 19:54:28 -04:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn generics_are_inlined_in_where_clause() {
|
|
|
|
check_edit(
|
|
|
|
"function",
|
|
|
|
r#"
|
|
|
|
trait SomeTrait<T> {}
|
|
|
|
|
|
|
|
trait Foo<T> {
|
|
|
|
fn function()
|
|
|
|
where Self: SomeTrait<T>;
|
|
|
|
}
|
|
|
|
struct Bar;
|
|
|
|
|
|
|
|
impl Foo<u32> for Bar {
|
|
|
|
fn f$0
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
trait SomeTrait<T> {}
|
|
|
|
|
|
|
|
trait Foo<T> {
|
|
|
|
fn function()
|
|
|
|
where Self: SomeTrait<T>;
|
|
|
|
}
|
|
|
|
struct Bar;
|
|
|
|
|
|
|
|
impl Foo<u32> for Bar {
|
|
|
|
fn function()
|
|
|
|
where Self: SomeTrait<u32> {
|
|
|
|
$0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
2020-02-09 12:24:34 -06:00
|
|
|
}
|