2020-11-01 03:35:04 -06:00
|
|
|
//! `render` module provides utilities for rendering completion suggestions
|
|
|
|
//! into code pieces that will be presented to user.
|
|
|
|
|
2020-11-03 01:36:01 -06:00
|
|
|
pub(crate) mod macro_;
|
|
|
|
pub(crate) mod function;
|
|
|
|
pub(crate) mod enum_variant;
|
|
|
|
pub(crate) mod const_;
|
2020-12-20 11:19:23 -06:00
|
|
|
pub(crate) mod pattern;
|
2020-11-03 01:36:01 -06:00
|
|
|
pub(crate) mod type_alias;
|
|
|
|
|
2020-11-01 03:35:04 -06:00
|
|
|
mod builder_ext;
|
|
|
|
|
2021-03-13 06:34:11 -06:00
|
|
|
use base_db::Upcast;
|
2021-01-18 17:08:59 -06:00
|
|
|
use hir::{
|
2021-03-13 06:34:11 -06:00
|
|
|
db::HirDatabase, AsAssocItem, Documentation, HasAttrs, HirDisplay, ModuleDef, Mutability,
|
|
|
|
ScopeDef, Type,
|
2021-01-18 17:08:59 -06:00
|
|
|
};
|
2021-02-24 17:53:59 -06:00
|
|
|
use ide_db::{
|
|
|
|
helpers::{item_name, SnippetCap},
|
|
|
|
RootDatabase, SymbolKind,
|
|
|
|
};
|
2020-11-01 03:35:04 -06:00
|
|
|
use syntax::TextRange;
|
|
|
|
|
2020-11-01 04:36:30 -06:00
|
|
|
use crate::{
|
2021-03-11 21:11:14 -06:00
|
|
|
item::{CompletionRelevance, ImportEdit},
|
2021-03-09 11:24:09 -06:00
|
|
|
CompletionContext, CompletionItem, CompletionItemKind, CompletionKind,
|
2020-11-01 04:36:30 -06:00
|
|
|
};
|
2020-11-01 03:35:04 -06:00
|
|
|
|
2020-12-20 01:05:24 -06:00
|
|
|
use crate::render::{enum_variant::render_variant, function::render_fn, macro_::render_macro};
|
2020-11-01 03:35:04 -06:00
|
|
|
|
2020-11-03 01:33:13 -06:00
|
|
|
pub(crate) fn render_field<'a>(
|
2020-11-01 04:39:05 -06:00
|
|
|
ctx: RenderContext<'a>,
|
2020-11-03 01:33:13 -06:00
|
|
|
field: hir::Field,
|
|
|
|
ty: &Type,
|
|
|
|
) -> CompletionItem {
|
|
|
|
Render::new(ctx).add_field(field, ty)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn render_tuple_field<'a>(
|
|
|
|
ctx: RenderContext<'a>,
|
|
|
|
field: usize,
|
|
|
|
ty: &Type,
|
|
|
|
) -> CompletionItem {
|
|
|
|
Render::new(ctx).add_tuple_field(field, ty)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn render_resolution<'a>(
|
|
|
|
ctx: RenderContext<'a>,
|
|
|
|
local_name: String,
|
|
|
|
resolution: &ScopeDef,
|
|
|
|
) -> Option<CompletionItem> {
|
2020-11-16 15:16:41 -06:00
|
|
|
Render::new(ctx).render_resolution(local_name, None, resolution)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn render_resolution_with_import<'a>(
|
|
|
|
ctx: RenderContext<'a>,
|
2020-12-04 08:03:22 -06:00
|
|
|
import_edit: ImportEdit,
|
2020-11-16 15:16:41 -06:00
|
|
|
) -> Option<CompletionItem> {
|
2021-03-02 17:26:53 -06:00
|
|
|
let resolution = ScopeDef::from(import_edit.import.original_item);
|
2021-01-05 02:34:03 -06:00
|
|
|
let local_name = match resolution {
|
|
|
|
ScopeDef::ModuleDef(ModuleDef::Function(f)) => f.name(ctx.completion.db).to_string(),
|
|
|
|
ScopeDef::ModuleDef(ModuleDef::Const(c)) => c.name(ctx.completion.db)?.to_string(),
|
|
|
|
ScopeDef::ModuleDef(ModuleDef::TypeAlias(t)) => t.name(ctx.completion.db).to_string(),
|
2021-03-02 17:26:53 -06:00
|
|
|
_ => item_name(ctx.db(), import_edit.import.original_item)?.to_string(),
|
2021-01-05 02:34:03 -06:00
|
|
|
};
|
2021-03-02 17:26:53 -06:00
|
|
|
Render::new(ctx).render_resolution(local_name, Some(import_edit), &resolution).map(
|
|
|
|
|mut item| {
|
|
|
|
item.completion_kind = CompletionKind::Magic;
|
|
|
|
item
|
|
|
|
},
|
|
|
|
)
|
2020-11-01 04:39:05 -06:00
|
|
|
}
|
|
|
|
|
2020-11-01 04:48:42 -06:00
|
|
|
/// Interface for data and methods required for items rendering.
|
2020-11-01 03:35:04 -06:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) struct RenderContext<'a> {
|
|
|
|
completion: &'a CompletionContext<'a>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> RenderContext<'a> {
|
2020-11-03 01:17:59 -06:00
|
|
|
pub(crate) fn new(completion: &'a CompletionContext<'a>) -> RenderContext<'a> {
|
2020-11-01 03:35:04 -06:00
|
|
|
RenderContext { completion }
|
|
|
|
}
|
|
|
|
|
2020-11-01 04:36:30 -06:00
|
|
|
fn snippet_cap(&self) -> Option<SnippetCap> {
|
2020-11-01 03:35:04 -06:00
|
|
|
self.completion.config.snippet_cap.clone()
|
|
|
|
}
|
|
|
|
|
2020-11-01 04:36:30 -06:00
|
|
|
fn db(&self) -> &'a RootDatabase {
|
2020-11-01 03:35:04 -06:00
|
|
|
&self.completion.db
|
|
|
|
}
|
|
|
|
|
2020-11-01 04:36:30 -06:00
|
|
|
fn source_range(&self) -> TextRange {
|
2020-11-01 03:35:04 -06:00
|
|
|
self.completion.source_range()
|
|
|
|
}
|
|
|
|
|
2020-11-01 04:36:30 -06:00
|
|
|
fn is_deprecated(&self, node: impl HasAttrs) -> bool {
|
2021-01-18 16:02:59 -06:00
|
|
|
let attrs = node.attrs(self.db());
|
|
|
|
attrs.by_key("deprecated").exists() || attrs.by_key("rustc_deprecated").exists()
|
2020-11-01 03:35:04 -06:00
|
|
|
}
|
2020-11-01 03:59:43 -06:00
|
|
|
|
2021-01-18 17:08:59 -06:00
|
|
|
fn is_deprecated_assoc_item(&self, as_assoc_item: impl AsAssocItem) -> bool {
|
|
|
|
let db = self.db();
|
|
|
|
let assoc = match as_assoc_item.as_assoc_item(db) {
|
|
|
|
Some(assoc) => assoc,
|
|
|
|
None => return false,
|
|
|
|
};
|
|
|
|
|
|
|
|
let is_assoc_deprecated = match assoc {
|
|
|
|
hir::AssocItem::Function(it) => self.is_deprecated(it),
|
|
|
|
hir::AssocItem::Const(it) => self.is_deprecated(it),
|
|
|
|
hir::AssocItem::TypeAlias(it) => self.is_deprecated(it),
|
|
|
|
};
|
|
|
|
is_assoc_deprecated
|
|
|
|
|| assoc.containing_trait(db).map(|trait_| self.is_deprecated(trait_)).unwrap_or(false)
|
|
|
|
}
|
|
|
|
|
2020-11-01 04:36:30 -06:00
|
|
|
fn docs(&self, node: impl HasAttrs) -> Option<Documentation> {
|
2020-11-01 03:59:43 -06:00
|
|
|
node.docs(self.db())
|
|
|
|
}
|
2020-11-01 03:35:04 -06:00
|
|
|
}
|
|
|
|
|
2020-11-03 01:33:13 -06:00
|
|
|
/// Generic renderer for completion items.
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct Render<'a> {
|
|
|
|
ctx: RenderContext<'a>,
|
|
|
|
}
|
|
|
|
|
2020-11-01 04:36:30 -06:00
|
|
|
impl<'a> Render<'a> {
|
2020-11-03 01:33:13 -06:00
|
|
|
fn new(ctx: RenderContext<'a>) -> Render<'a> {
|
2020-11-01 04:36:30 -06:00
|
|
|
Render { ctx }
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:33:13 -06:00
|
|
|
fn add_field(&mut self, field: hir::Field, ty: &Type) -> CompletionItem {
|
2020-11-01 04:36:30 -06:00
|
|
|
let is_deprecated = self.ctx.is_deprecated(field);
|
|
|
|
let name = field.name(self.ctx.db());
|
2021-03-12 03:12:32 -06:00
|
|
|
let mut item = CompletionItem::new(
|
2020-11-01 04:36:30 -06:00
|
|
|
CompletionKind::Reference,
|
|
|
|
self.ctx.source_range(),
|
|
|
|
name.to_string(),
|
2021-03-11 09:46:41 -06:00
|
|
|
);
|
2021-03-12 03:12:32 -06:00
|
|
|
item.kind(SymbolKind::Field)
|
2021-03-11 09:46:41 -06:00
|
|
|
.detail(ty.display(self.ctx.db()).to_string())
|
|
|
|
.set_documentation(field.docs(self.ctx.db()))
|
|
|
|
.set_deprecated(is_deprecated);
|
2020-11-01 04:36:30 -06:00
|
|
|
|
2021-03-13 08:13:30 -06:00
|
|
|
item.set_relevance(compute_relevance(&self.ctx, &ty, &name.to_string()));
|
2020-11-01 04:36:30 -06:00
|
|
|
|
2021-03-12 03:12:32 -06:00
|
|
|
item.build()
|
2020-11-01 04:36:30 -06:00
|
|
|
}
|
|
|
|
|
2020-11-03 01:33:13 -06:00
|
|
|
fn add_tuple_field(&mut self, field: usize, ty: &Type) -> CompletionItem {
|
2021-03-12 03:12:32 -06:00
|
|
|
let mut item = CompletionItem::new(
|
2021-03-11 09:46:41 -06:00
|
|
|
CompletionKind::Reference,
|
|
|
|
self.ctx.source_range(),
|
|
|
|
field.to_string(),
|
|
|
|
);
|
|
|
|
|
2021-03-12 03:12:32 -06:00
|
|
|
item.kind(SymbolKind::Field).detail(ty.display(self.ctx.db()).to_string());
|
2021-03-11 09:46:41 -06:00
|
|
|
|
2021-03-12 03:12:32 -06:00
|
|
|
item.build()
|
2020-11-01 04:36:30 -06:00
|
|
|
}
|
|
|
|
|
2020-11-03 01:33:13 -06:00
|
|
|
fn render_resolution(
|
2020-11-01 04:36:30 -06:00
|
|
|
self,
|
|
|
|
local_name: String,
|
2020-12-03 03:13:28 -06:00
|
|
|
import_to_add: Option<ImportEdit>,
|
2020-11-01 04:36:30 -06:00
|
|
|
resolution: &ScopeDef,
|
|
|
|
) -> Option<CompletionItem> {
|
2020-11-23 18:26:16 -06:00
|
|
|
let _p = profile::span("render_resolution");
|
2020-11-01 04:36:30 -06:00
|
|
|
use hir::ModuleDef::*;
|
|
|
|
|
|
|
|
let completion_kind = match resolution {
|
|
|
|
ScopeDef::ModuleDef(BuiltinType(..)) => CompletionKind::BuiltinType,
|
|
|
|
_ => CompletionKind::Reference,
|
|
|
|
};
|
|
|
|
|
|
|
|
let kind = match resolution {
|
|
|
|
ScopeDef::ModuleDef(Function(func)) => {
|
2021-01-01 00:26:39 -06:00
|
|
|
return render_fn(self.ctx, import_to_add, Some(local_name), *func);
|
2020-11-01 04:36:30 -06:00
|
|
|
}
|
2020-12-22 12:00:38 -06:00
|
|
|
ScopeDef::ModuleDef(Variant(_))
|
|
|
|
if self.ctx.completion.is_pat_binding_or_const
|
|
|
|
| self.ctx.completion.is_irrefutable_pat_binding =>
|
|
|
|
{
|
2021-01-20 11:38:12 -06:00
|
|
|
CompletionItemKind::SymbolKind(SymbolKind::Variant)
|
2020-12-22 12:00:38 -06:00
|
|
|
}
|
2020-12-20 01:05:24 -06:00
|
|
|
ScopeDef::ModuleDef(Variant(var)) => {
|
|
|
|
let item = render_variant(self.ctx, import_to_add, Some(local_name), *var, None);
|
2020-11-01 04:36:30 -06:00
|
|
|
return Some(item);
|
|
|
|
}
|
|
|
|
ScopeDef::MacroDef(mac) => {
|
2020-11-27 04:22:10 -06:00
|
|
|
let item = render_macro(self.ctx, import_to_add, local_name, *mac);
|
2020-11-01 04:36:30 -06:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2021-01-20 11:38:12 -06:00
|
|
|
ScopeDef::ModuleDef(Module(..)) => CompletionItemKind::SymbolKind(SymbolKind::Module),
|
|
|
|
ScopeDef::ModuleDef(Adt(adt)) => CompletionItemKind::SymbolKind(match adt {
|
|
|
|
hir::Adt::Struct(_) => SymbolKind::Struct,
|
2021-01-23 18:32:52 -06:00
|
|
|
hir::Adt::Union(_) => SymbolKind::Union,
|
2021-01-20 11:38:12 -06:00
|
|
|
hir::Adt::Enum(_) => SymbolKind::Enum,
|
|
|
|
}),
|
|
|
|
ScopeDef::ModuleDef(Const(..)) => CompletionItemKind::SymbolKind(SymbolKind::Const),
|
|
|
|
ScopeDef::ModuleDef(Static(..)) => CompletionItemKind::SymbolKind(SymbolKind::Static),
|
|
|
|
ScopeDef::ModuleDef(Trait(..)) => CompletionItemKind::SymbolKind(SymbolKind::Trait),
|
|
|
|
ScopeDef::ModuleDef(TypeAlias(..)) => {
|
|
|
|
CompletionItemKind::SymbolKind(SymbolKind::TypeAlias)
|
|
|
|
}
|
2020-11-01 04:36:30 -06:00
|
|
|
ScopeDef::ModuleDef(BuiltinType(..)) => CompletionItemKind::BuiltinType,
|
2021-01-20 11:38:12 -06:00
|
|
|
ScopeDef::GenericParam(param) => CompletionItemKind::SymbolKind(match param {
|
|
|
|
hir::GenericParam::TypeParam(_) => SymbolKind::TypeParam,
|
|
|
|
hir::GenericParam::LifetimeParam(_) => SymbolKind::LifetimeParam,
|
|
|
|
hir::GenericParam::ConstParam(_) => SymbolKind::ConstParam,
|
|
|
|
}),
|
|
|
|
ScopeDef::Local(..) => CompletionItemKind::SymbolKind(SymbolKind::Local),
|
|
|
|
ScopeDef::AdtSelfType(..) | ScopeDef::ImplSelfType(..) => {
|
|
|
|
CompletionItemKind::SymbolKind(SymbolKind::SelfParam)
|
|
|
|
}
|
2020-11-01 04:36:30 -06:00
|
|
|
ScopeDef::Unknown => {
|
2021-03-11 09:46:41 -06:00
|
|
|
let mut item = CompletionItem::new(
|
2020-11-01 04:36:30 -06:00
|
|
|
CompletionKind::Reference,
|
|
|
|
self.ctx.source_range(),
|
|
|
|
local_name,
|
2021-03-11 09:46:41 -06:00
|
|
|
);
|
|
|
|
item.kind(CompletionItemKind::UnresolvedReference).add_import(import_to_add);
|
|
|
|
return Some(item.build());
|
2020-11-01 04:36:30 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut item =
|
|
|
|
CompletionItem::new(completion_kind, self.ctx.source_range(), local_name.clone());
|
|
|
|
if let ScopeDef::Local(local) = resolution {
|
|
|
|
let ty = local.ty(self.ctx.db());
|
|
|
|
if !ty.is_unknown() {
|
2021-03-11 09:46:41 -06:00
|
|
|
item.detail(ty.display(self.ctx.db()).to_string());
|
2020-11-01 04:36:30 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if let ScopeDef::Local(local) = resolution {
|
2021-03-09 11:24:09 -06:00
|
|
|
let ty = local.ty(self.ctx.db());
|
2021-03-13 09:25:41 -06:00
|
|
|
|
2021-03-13 08:13:30 -06:00
|
|
|
let mut relevance = compute_relevance(&self.ctx, &ty, &local_name);
|
|
|
|
relevance.is_local = true;
|
|
|
|
item.set_relevance(relevance);
|
2021-03-13 09:25:41 -06:00
|
|
|
|
2021-03-15 08:25:39 -05:00
|
|
|
if let Some(expected_type) = self.ctx.completion.expected_type.as_ref() {
|
|
|
|
if &ty != expected_type {
|
2021-03-14 05:25:37 -05:00
|
|
|
if let Some(ty_without_ref) = expected_type.remove_ref() {
|
|
|
|
if relevance_type_match(self.ctx.db().upcast(), &ty, &ty_without_ref) {
|
|
|
|
cov_mark::hit!(suggest_ref);
|
|
|
|
let mutability = if expected_type.is_mutable_reference() {
|
|
|
|
Mutability::Mut
|
|
|
|
} else {
|
|
|
|
Mutability::Shared
|
|
|
|
};
|
|
|
|
item.ref_match(mutability);
|
|
|
|
}
|
2021-03-09 09:06:08 -06:00
|
|
|
}
|
2021-03-09 08:42:05 -06:00
|
|
|
}
|
2020-11-01 04:36:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add `<>` for generic types
|
|
|
|
if self.ctx.completion.is_path_type
|
|
|
|
&& !self.ctx.completion.has_type_args
|
|
|
|
&& self.ctx.completion.config.add_call_parenthesis
|
|
|
|
{
|
|
|
|
if let Some(cap) = self.ctx.snippet_cap() {
|
|
|
|
let has_non_default_type_params = match resolution {
|
|
|
|
ScopeDef::ModuleDef(Adt(it)) => it.has_non_default_type_params(self.ctx.db()),
|
|
|
|
ScopeDef::ModuleDef(TypeAlias(it)) => {
|
|
|
|
it.has_non_default_type_params(self.ctx.db())
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
if has_non_default_type_params {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::hit!(inserts_angle_brackets_for_generics);
|
2021-03-11 09:46:41 -06:00
|
|
|
item.lookup_by(local_name.clone())
|
2020-11-01 04:36:30 -06:00
|
|
|
.label(format!("{}<…>", local_name))
|
|
|
|
.insert_snippet(cap, format!("{}<$0>", local_name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-11 09:46:41 -06:00
|
|
|
item.kind(kind)
|
|
|
|
.add_import(import_to_add)
|
|
|
|
.set_documentation(self.docs(resolution))
|
|
|
|
.set_deprecated(self.is_deprecated(resolution));
|
|
|
|
Some(item.build())
|
2020-11-01 04:36:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn docs(&self, resolution: &ScopeDef) -> Option<Documentation> {
|
|
|
|
use hir::ModuleDef::*;
|
|
|
|
match resolution {
|
|
|
|
ScopeDef::ModuleDef(Module(it)) => it.docs(self.ctx.db()),
|
|
|
|
ScopeDef::ModuleDef(Adt(it)) => it.docs(self.ctx.db()),
|
2020-12-20 01:05:24 -06:00
|
|
|
ScopeDef::ModuleDef(Variant(it)) => it.docs(self.ctx.db()),
|
2020-11-01 04:36:30 -06:00
|
|
|
ScopeDef::ModuleDef(Const(it)) => it.docs(self.ctx.db()),
|
|
|
|
ScopeDef::ModuleDef(Static(it)) => it.docs(self.ctx.db()),
|
|
|
|
ScopeDef::ModuleDef(Trait(it)) => it.docs(self.ctx.db()),
|
|
|
|
ScopeDef::ModuleDef(TypeAlias(it)) => it.docs(self.ctx.db()),
|
|
|
|
_ => None,
|
2021-01-18 17:08:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_deprecated(&self, resolution: &ScopeDef) -> bool {
|
|
|
|
match resolution {
|
|
|
|
ScopeDef::ModuleDef(it) => self.ctx.is_deprecated_assoc_item(*it),
|
|
|
|
ScopeDef::MacroDef(it) => self.ctx.is_deprecated(*it),
|
|
|
|
ScopeDef::GenericParam(it) => self.ctx.is_deprecated(*it),
|
|
|
|
ScopeDef::AdtSelfType(it) => self.ctx.is_deprecated(*it),
|
|
|
|
_ => false,
|
2020-11-01 04:36:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-13 08:13:30 -06:00
|
|
|
fn compute_relevance(ctx: &RenderContext, ty: &Type, name: &str) -> CompletionRelevance {
|
2021-03-11 21:11:14 -06:00
|
|
|
let mut res = CompletionRelevance::default();
|
2021-03-13 08:13:30 -06:00
|
|
|
|
2021-03-15 08:25:39 -05:00
|
|
|
res.exact_type_match = Some(ty) == ctx.completion.expected_type.as_ref();
|
|
|
|
res.exact_name_match = Some(name) == ctx.completion.expected_name.as_deref();
|
2021-03-13 08:13:30 -06:00
|
|
|
|
|
|
|
res
|
2020-11-01 04:36:30 -06:00
|
|
|
}
|
|
|
|
|
2021-03-13 09:25:41 -06:00
|
|
|
fn relevance_type_match(db: &dyn HirDatabase, ty: &Type, expected_type: &Type) -> bool {
|
|
|
|
ty == expected_type || ty.autoderef(db).any(|deref_ty| &deref_ty == expected_type)
|
2021-03-13 06:34:11 -06:00
|
|
|
}
|
|
|
|
|
2020-11-01 04:36:30 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use expect_test::{expect, Expect};
|
2021-03-13 08:13:30 -06:00
|
|
|
use itertools::Itertools;
|
2020-11-01 04:36:30 -06:00
|
|
|
|
|
|
|
use crate::{
|
2021-01-06 11:43:46 -06:00
|
|
|
test_utils::{check_edit, do_completion, get_all_items, TEST_CONFIG},
|
2021-03-11 21:11:14 -06:00
|
|
|
CompletionKind, CompletionRelevance,
|
2020-11-01 04:36:30 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
fn check(ra_fixture: &str, expect: Expect) {
|
|
|
|
let actual = do_completion(ra_fixture, CompletionKind::Reference);
|
|
|
|
expect.assert_debug_eq(&actual);
|
|
|
|
}
|
|
|
|
|
2021-03-09 11:24:09 -06:00
|
|
|
fn check_relevance(ra_fixture: &str, expect: Expect) {
|
2021-03-13 08:13:30 -06:00
|
|
|
fn display_relevance(relevance: CompletionRelevance) -> String {
|
|
|
|
let relevance_factors = vec![
|
|
|
|
(relevance.exact_type_match, "type"),
|
|
|
|
(relevance.exact_name_match, "name"),
|
|
|
|
(relevance.is_local, "local"),
|
|
|
|
]
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|(cond, desc)| if cond { Some(desc) } else { None })
|
|
|
|
.join("+");
|
|
|
|
|
|
|
|
format!("[{}]", relevance_factors)
|
2020-11-01 04:36:30 -06:00
|
|
|
}
|
|
|
|
|
2021-03-14 05:25:37 -05:00
|
|
|
let actual = get_all_items(TEST_CONFIG, ra_fixture)
|
2020-11-01 04:36:30 -06:00
|
|
|
.into_iter()
|
|
|
|
.filter(|it| it.completion_kind == CompletionKind::Reference)
|
2021-03-13 10:31:52 -06:00
|
|
|
.flat_map(|it| {
|
|
|
|
let mut items = vec![];
|
|
|
|
|
2020-11-01 04:36:30 -06:00
|
|
|
let tag = it.kind().unwrap().tag();
|
2021-03-13 10:31:52 -06:00
|
|
|
let relevance = display_relevance(it.relevance());
|
|
|
|
items.push(format!("{} {} {}\n", tag, it.label(), relevance));
|
|
|
|
|
|
|
|
if let Some((mutability, relevance)) = it.ref_match() {
|
|
|
|
let label = format!("&{}{}", mutability.as_keyword_for_ref(), it.label());
|
|
|
|
let relevance = display_relevance(relevance);
|
|
|
|
|
|
|
|
items.push(format!("{} {} {}\n", tag, label, relevance));
|
|
|
|
}
|
|
|
|
|
|
|
|
items
|
2020-11-01 04:36:30 -06:00
|
|
|
})
|
|
|
|
.collect::<String>();
|
2021-03-13 10:31:52 -06:00
|
|
|
|
2020-11-01 04:36:30 -06:00
|
|
|
expect.assert_eq(&actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn enum_detail_includes_record_fields() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
enum Foo { Foo { x: i32, y: i32 } }
|
|
|
|
|
2021-01-06 14:15:48 -06:00
|
|
|
fn main() { Foo::Fo$0 }
|
2020-11-01 04:36:30 -06:00
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "Foo",
|
|
|
|
source_range: 54..56,
|
|
|
|
delete: 54..56,
|
|
|
|
insert: "Foo",
|
2021-01-20 11:46:14 -06:00
|
|
|
kind: SymbolKind(
|
|
|
|
Variant,
|
|
|
|
),
|
2020-11-01 04:36:30 -06:00
|
|
|
detail: "{ x: i32, y: i32 }",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn enum_detail_doesnt_include_tuple_fields() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
enum Foo { Foo (i32, i32) }
|
|
|
|
|
2021-01-06 14:15:48 -06:00
|
|
|
fn main() { Foo::Fo$0 }
|
2020-11-01 04:36:30 -06:00
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "Foo(…)",
|
|
|
|
source_range: 46..48,
|
|
|
|
delete: 46..48,
|
|
|
|
insert: "Foo($0)",
|
2021-01-20 11:46:14 -06:00
|
|
|
kind: SymbolKind(
|
|
|
|
Variant,
|
|
|
|
),
|
2020-11-01 04:36:30 -06:00
|
|
|
lookup: "Foo",
|
|
|
|
detail: "(i32, i32)",
|
|
|
|
trigger_call_info: true,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn enum_detail_just_parentheses_for_unit() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
enum Foo { Foo }
|
|
|
|
|
2021-01-06 14:15:48 -06:00
|
|
|
fn main() { Foo::Fo$0 }
|
2020-11-01 04:36:30 -06:00
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "Foo",
|
|
|
|
source_range: 35..37,
|
|
|
|
delete: 35..37,
|
|
|
|
insert: "Foo",
|
2021-01-20 11:46:14 -06:00
|
|
|
kind: SymbolKind(
|
|
|
|
Variant,
|
|
|
|
),
|
2020-11-01 04:36:30 -06:00
|
|
|
detail: "()",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lookup_enums_by_two_qualifiers() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
mod m {
|
|
|
|
pub enum Spam { Foo, Bar(i32) }
|
|
|
|
}
|
2021-01-06 14:15:48 -06:00
|
|
|
fn main() { let _: m::Spam = S$0 }
|
2020-11-01 04:36:30 -06:00
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "Spam::Bar(…)",
|
|
|
|
source_range: 75..76,
|
|
|
|
delete: 75..76,
|
|
|
|
insert: "Spam::Bar($0)",
|
2021-01-20 11:46:14 -06:00
|
|
|
kind: SymbolKind(
|
|
|
|
Variant,
|
|
|
|
),
|
2020-11-01 04:36:30 -06:00
|
|
|
lookup: "Spam::Bar",
|
|
|
|
detail: "(i32)",
|
|
|
|
trigger_call_info: true,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "m",
|
|
|
|
source_range: 75..76,
|
|
|
|
delete: 75..76,
|
|
|
|
insert: "m",
|
2021-01-20 11:46:14 -06:00
|
|
|
kind: SymbolKind(
|
|
|
|
Module,
|
|
|
|
),
|
2020-11-01 04:36:30 -06:00
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "m::Spam::Foo",
|
|
|
|
source_range: 75..76,
|
|
|
|
delete: 75..76,
|
|
|
|
insert: "m::Spam::Foo",
|
2021-01-20 11:46:14 -06:00
|
|
|
kind: SymbolKind(
|
|
|
|
Variant,
|
|
|
|
),
|
2020-11-01 04:36:30 -06:00
|
|
|
lookup: "Spam::Foo",
|
|
|
|
detail: "()",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "main()",
|
|
|
|
source_range: 75..76,
|
|
|
|
delete: 75..76,
|
|
|
|
insert: "main()$0",
|
2021-01-20 11:46:14 -06:00
|
|
|
kind: SymbolKind(
|
|
|
|
Function,
|
|
|
|
),
|
2020-11-01 04:36:30 -06:00
|
|
|
lookup: "main",
|
2021-01-22 09:59:22 -06:00
|
|
|
detail: "-> ()",
|
2020-11-01 04:36:30 -06:00
|
|
|
},
|
|
|
|
]
|
|
|
|
"#]],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sets_deprecated_flag_in_items() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
#[deprecated]
|
|
|
|
fn something_deprecated() {}
|
2021-01-18 16:02:59 -06:00
|
|
|
#[rustc_deprecated(since = "1.0.0")]
|
2020-11-01 04:36:30 -06:00
|
|
|
fn something_else_deprecated() {}
|
|
|
|
|
2021-01-06 14:15:48 -06:00
|
|
|
fn main() { som$0 }
|
2020-11-01 04:36:30 -06:00
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "main()",
|
2021-01-18 16:02:59 -06:00
|
|
|
source_range: 127..130,
|
|
|
|
delete: 127..130,
|
2020-11-01 04:36:30 -06:00
|
|
|
insert: "main()$0",
|
2021-01-20 11:46:14 -06:00
|
|
|
kind: SymbolKind(
|
|
|
|
Function,
|
|
|
|
),
|
2020-11-01 04:36:30 -06:00
|
|
|
lookup: "main",
|
2021-01-22 09:59:22 -06:00
|
|
|
detail: "-> ()",
|
2020-11-01 04:36:30 -06:00
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "something_deprecated()",
|
2021-01-18 16:02:59 -06:00
|
|
|
source_range: 127..130,
|
|
|
|
delete: 127..130,
|
2020-11-01 04:36:30 -06:00
|
|
|
insert: "something_deprecated()$0",
|
2021-01-20 11:46:14 -06:00
|
|
|
kind: SymbolKind(
|
|
|
|
Function,
|
|
|
|
),
|
2020-11-01 04:36:30 -06:00
|
|
|
lookup: "something_deprecated",
|
2021-01-22 09:59:22 -06:00
|
|
|
detail: "-> ()",
|
2020-11-01 04:36:30 -06:00
|
|
|
deprecated: true,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "something_else_deprecated()",
|
2021-01-18 16:02:59 -06:00
|
|
|
source_range: 127..130,
|
|
|
|
delete: 127..130,
|
2020-11-01 04:36:30 -06:00
|
|
|
insert: "something_else_deprecated()$0",
|
2021-01-20 11:46:14 -06:00
|
|
|
kind: SymbolKind(
|
|
|
|
Function,
|
|
|
|
),
|
2020-11-01 04:36:30 -06:00
|
|
|
lookup: "something_else_deprecated",
|
2021-01-22 09:59:22 -06:00
|
|
|
detail: "-> ()",
|
2020-11-01 04:36:30 -06:00
|
|
|
deprecated: true,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct A { #[deprecated] the_field: u32 }
|
2021-01-06 14:15:48 -06:00
|
|
|
fn foo() { A { the$0 } }
|
2020-11-01 04:36:30 -06:00
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "the_field",
|
|
|
|
source_range: 57..60,
|
|
|
|
delete: 57..60,
|
|
|
|
insert: "the_field",
|
2021-01-20 11:46:14 -06:00
|
|
|
kind: SymbolKind(
|
|
|
|
Field,
|
|
|
|
),
|
2020-11-01 04:36:30 -06:00
|
|
|
detail: "u32",
|
|
|
|
deprecated: true,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn renders_docs() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct S {
|
|
|
|
/// Field docs
|
|
|
|
foo:
|
|
|
|
}
|
|
|
|
impl S {
|
|
|
|
/// Method docs
|
2021-01-06 14:15:48 -06:00
|
|
|
fn bar(self) { self.$0 }
|
2020-11-01 04:36:30 -06:00
|
|
|
}"#,
|
|
|
|
expect![[r#"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "bar()",
|
|
|
|
source_range: 94..94,
|
|
|
|
delete: 94..94,
|
|
|
|
insert: "bar()$0",
|
|
|
|
kind: Method,
|
|
|
|
lookup: "bar",
|
2021-01-22 09:59:22 -06:00
|
|
|
detail: "-> ()",
|
2020-11-01 04:36:30 -06:00
|
|
|
documentation: Documentation(
|
|
|
|
"Method docs",
|
|
|
|
),
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "foo",
|
|
|
|
source_range: 94..94,
|
|
|
|
delete: 94..94,
|
|
|
|
insert: "foo",
|
2021-01-20 11:46:14 -06:00
|
|
|
kind: SymbolKind(
|
|
|
|
Field,
|
|
|
|
),
|
2020-11-01 04:36:30 -06:00
|
|
|
detail: "{unknown}",
|
|
|
|
documentation: Documentation(
|
|
|
|
"Field docs",
|
|
|
|
),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
|
|
|
|
check(
|
|
|
|
r#"
|
2021-01-06 14:15:48 -06:00
|
|
|
use self::my$0;
|
2020-11-01 04:36:30 -06:00
|
|
|
|
|
|
|
/// mod docs
|
|
|
|
mod my { }
|
|
|
|
|
|
|
|
/// enum docs
|
|
|
|
enum E {
|
|
|
|
/// variant docs
|
|
|
|
V
|
|
|
|
}
|
|
|
|
use self::E::*;
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "E",
|
|
|
|
source_range: 10..12,
|
|
|
|
delete: 10..12,
|
|
|
|
insert: "E",
|
2021-01-20 11:46:14 -06:00
|
|
|
kind: SymbolKind(
|
|
|
|
Enum,
|
|
|
|
),
|
2020-11-01 04:36:30 -06:00
|
|
|
documentation: Documentation(
|
|
|
|
"enum docs",
|
|
|
|
),
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "V",
|
|
|
|
source_range: 10..12,
|
|
|
|
delete: 10..12,
|
|
|
|
insert: "V",
|
2021-01-20 11:46:14 -06:00
|
|
|
kind: SymbolKind(
|
|
|
|
Variant,
|
|
|
|
),
|
2020-11-01 04:36:30 -06:00
|
|
|
detail: "()",
|
|
|
|
documentation: Documentation(
|
|
|
|
"variant docs",
|
|
|
|
),
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "my",
|
|
|
|
source_range: 10..12,
|
|
|
|
delete: 10..12,
|
|
|
|
insert: "my",
|
2021-01-20 11:46:14 -06:00
|
|
|
kind: SymbolKind(
|
|
|
|
Module,
|
|
|
|
),
|
2020-11-01 04:36:30 -06:00
|
|
|
documentation: Documentation(
|
|
|
|
"mod docs",
|
|
|
|
),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"#]],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dont_render_attrs() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct S;
|
|
|
|
impl S {
|
|
|
|
#[inline]
|
|
|
|
fn the_method(&self) { }
|
|
|
|
}
|
2021-01-06 14:15:48 -06:00
|
|
|
fn foo(s: S) { s.$0 }
|
2020-11-01 04:36:30 -06:00
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "the_method()",
|
|
|
|
source_range: 81..81,
|
|
|
|
delete: 81..81,
|
|
|
|
insert: "the_method()$0",
|
|
|
|
kind: Method,
|
|
|
|
lookup: "the_method",
|
2021-01-22 09:59:22 -06:00
|
|
|
detail: "-> ()",
|
2020-11-01 04:36:30 -06:00
|
|
|
},
|
|
|
|
]
|
|
|
|
"#]],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_call_parens_if_fn_ptr_needed() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::check!(no_call_parens_if_fn_ptr_needed);
|
2020-11-01 04:36:30 -06:00
|
|
|
check_edit(
|
|
|
|
"foo",
|
|
|
|
r#"
|
|
|
|
fn foo(foo: u8, bar: u8) {}
|
|
|
|
struct ManualVtable { f: fn(u8, u8) }
|
|
|
|
|
|
|
|
fn main() -> ManualVtable {
|
2021-01-06 14:15:48 -06:00
|
|
|
ManualVtable { f: f$0 }
|
2020-11-01 04:36:30 -06:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn foo(foo: u8, bar: u8) {}
|
|
|
|
struct ManualVtable { f: fn(u8, u8) }
|
|
|
|
|
|
|
|
fn main() -> ManualVtable {
|
|
|
|
ManualVtable { f: foo }
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_parens_in_use_item() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::check!(no_parens_in_use_item);
|
2020-11-01 04:36:30 -06:00
|
|
|
check_edit(
|
|
|
|
"foo",
|
|
|
|
r#"
|
|
|
|
mod m { pub fn foo() {} }
|
2021-01-06 14:15:48 -06:00
|
|
|
use crate::m::f$0;
|
2020-11-01 04:36:30 -06:00
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
mod m { pub fn foo() {} }
|
|
|
|
use crate::m::foo;
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_parens_in_call() {
|
|
|
|
check_edit(
|
|
|
|
"foo",
|
|
|
|
r#"
|
|
|
|
fn foo(x: i32) {}
|
2021-01-06 14:15:48 -06:00
|
|
|
fn main() { f$0(); }
|
2020-11-01 04:36:30 -06:00
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn foo(x: i32) {}
|
|
|
|
fn main() { foo(); }
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
check_edit(
|
|
|
|
"foo",
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
impl Foo { fn foo(&self){} }
|
2021-01-06 14:15:48 -06:00
|
|
|
fn f(foo: &Foo) { foo.f$0(); }
|
2020-11-01 04:36:30 -06:00
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
impl Foo { fn foo(&self){} }
|
|
|
|
fn f(foo: &Foo) { foo.foo(); }
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inserts_angle_brackets_for_generics() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::check!(inserts_angle_brackets_for_generics);
|
2020-11-01 04:36:30 -06:00
|
|
|
check_edit(
|
|
|
|
"Vec",
|
|
|
|
r#"
|
|
|
|
struct Vec<T> {}
|
2021-01-06 14:15:48 -06:00
|
|
|
fn foo(xs: Ve$0)
|
2020-11-01 04:36:30 -06:00
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Vec<T> {}
|
|
|
|
fn foo(xs: Vec<$0>)
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
check_edit(
|
|
|
|
"Vec",
|
|
|
|
r#"
|
|
|
|
type Vec<T> = (T,);
|
2021-01-06 14:15:48 -06:00
|
|
|
fn foo(xs: Ve$0)
|
2020-11-01 04:36:30 -06:00
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
type Vec<T> = (T,);
|
|
|
|
fn foo(xs: Vec<$0>)
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
check_edit(
|
|
|
|
"Vec",
|
|
|
|
r#"
|
|
|
|
struct Vec<T = i128> {}
|
2021-01-06 14:15:48 -06:00
|
|
|
fn foo(xs: Ve$0)
|
2020-11-01 04:36:30 -06:00
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Vec<T = i128> {}
|
|
|
|
fn foo(xs: Vec)
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
check_edit(
|
|
|
|
"Vec",
|
|
|
|
r#"
|
|
|
|
struct Vec<T> {}
|
2021-01-06 14:15:48 -06:00
|
|
|
fn foo(xs: Ve$0<i128>)
|
2020-11-01 04:36:30 -06:00
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Vec<T> {}
|
|
|
|
fn foo(xs: Vec<i128>)
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-03-09 11:24:09 -06:00
|
|
|
fn active_param_relevance() {
|
|
|
|
check_relevance(
|
2020-11-01 04:36:30 -06:00
|
|
|
r#"
|
|
|
|
struct S { foo: i64, bar: u32, baz: u32 }
|
|
|
|
fn test(bar: u32) { }
|
2021-01-06 14:15:48 -06:00
|
|
|
fn foo(s: S) { test(s.$0) }
|
2020-11-01 04:36:30 -06:00
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2021-03-14 05:25:37 -05:00
|
|
|
fd foo []
|
2020-11-01 04:36:30 -06:00
|
|
|
fd bar [type+name]
|
|
|
|
fd baz [type]
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-03-09 11:24:09 -06:00
|
|
|
fn record_field_relevances() {
|
|
|
|
check_relevance(
|
2020-11-01 04:36:30 -06:00
|
|
|
r#"
|
|
|
|
struct A { foo: i64, bar: u32, baz: u32 }
|
|
|
|
struct B { x: (), y: f32, bar: u32 }
|
2021-01-06 14:15:48 -06:00
|
|
|
fn foo(a: A) { B { bar: a.$0 }; }
|
2020-11-01 04:36:30 -06:00
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2021-03-14 05:25:37 -05:00
|
|
|
fd foo []
|
2020-11-01 04:36:30 -06:00
|
|
|
fd bar [type+name]
|
|
|
|
fd baz [type]
|
|
|
|
"#]],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-03-09 11:24:09 -06:00
|
|
|
fn record_field_and_call_relevances() {
|
|
|
|
check_relevance(
|
2020-11-01 04:36:30 -06:00
|
|
|
r#"
|
|
|
|
struct A { foo: i64, bar: u32, baz: u32 }
|
|
|
|
struct B { x: (), y: f32, bar: u32 }
|
|
|
|
fn f(foo: i64) { }
|
2021-01-06 14:15:48 -06:00
|
|
|
fn foo(a: A) { B { bar: f(a.$0) }; }
|
2020-11-01 04:36:30 -06:00
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
fd foo [type+name]
|
2021-03-13 10:31:52 -06:00
|
|
|
fd bar []
|
2021-03-14 05:25:37 -05:00
|
|
|
fd baz []
|
2020-11-01 04:36:30 -06:00
|
|
|
"#]],
|
|
|
|
);
|
2021-03-09 11:24:09 -06:00
|
|
|
check_relevance(
|
2020-11-01 04:36:30 -06:00
|
|
|
r#"
|
|
|
|
struct A { foo: i64, bar: u32, baz: u32 }
|
|
|
|
struct B { x: (), y: f32, bar: u32 }
|
|
|
|
fn f(foo: i64) { }
|
2021-01-06 14:15:48 -06:00
|
|
|
fn foo(a: A) { f(B { bar: a.$0 }); }
|
2020-11-01 04:36:30 -06:00
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2021-03-14 05:25:37 -05:00
|
|
|
fd foo []
|
2020-11-01 04:36:30 -06:00
|
|
|
fd bar [type+name]
|
|
|
|
fd baz [type]
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn prioritize_exact_ref_match() {
|
2021-03-09 11:24:09 -06:00
|
|
|
check_relevance(
|
2020-11-01 04:36:30 -06:00
|
|
|
r#"
|
|
|
|
struct WorldSnapshot { _f: () };
|
2021-01-06 14:15:48 -06:00
|
|
|
fn go(world: &WorldSnapshot) { go(w$0) }
|
2020-11-01 04:36:30 -06:00
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2021-03-13 08:13:30 -06:00
|
|
|
lc world [type+name+local]
|
2021-03-13 10:31:52 -06:00
|
|
|
st WorldSnapshot []
|
2021-03-14 05:25:37 -05:00
|
|
|
fn go(…) []
|
2020-11-01 04:36:30 -06:00
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn too_many_arguments() {
|
2021-03-09 11:24:09 -06:00
|
|
|
check_relevance(
|
2020-11-01 04:36:30 -06:00
|
|
|
r#"
|
|
|
|
struct Foo;
|
2021-01-06 14:15:48 -06:00
|
|
|
fn f(foo: &Foo) { f(foo, w$0) }
|
2020-11-01 04:36:30 -06:00
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2021-03-13 08:13:30 -06:00
|
|
|
lc foo [local]
|
2021-03-13 10:31:52 -06:00
|
|
|
st Foo []
|
2021-03-14 05:25:37 -05:00
|
|
|
fn f(…) []
|
2020-11-01 04:36:30 -06:00
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
2021-03-09 09:06:08 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn suggest_ref_mut() {
|
|
|
|
cov_mark::check!(suggest_ref);
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct S;
|
|
|
|
fn foo(s: &mut S) {}
|
|
|
|
fn main() {
|
|
|
|
let mut s = S;
|
|
|
|
foo($0);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "S",
|
|
|
|
source_range: 70..70,
|
|
|
|
delete: 70..70,
|
|
|
|
insert: "S",
|
|
|
|
kind: SymbolKind(
|
|
|
|
Struct,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "foo(…)",
|
|
|
|
source_range: 70..70,
|
|
|
|
delete: 70..70,
|
|
|
|
insert: "foo(${1:&mut s})$0",
|
|
|
|
kind: SymbolKind(
|
|
|
|
Function,
|
|
|
|
),
|
|
|
|
lookup: "foo",
|
|
|
|
detail: "-> ()",
|
|
|
|
trigger_call_info: true,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "main()",
|
|
|
|
source_range: 70..70,
|
|
|
|
delete: 70..70,
|
|
|
|
insert: "main()$0",
|
|
|
|
kind: SymbolKind(
|
|
|
|
Function,
|
|
|
|
),
|
|
|
|
lookup: "main",
|
|
|
|
detail: "-> ()",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "s",
|
|
|
|
source_range: 70..70,
|
|
|
|
delete: 70..70,
|
|
|
|
insert: "s",
|
|
|
|
kind: SymbolKind(
|
|
|
|
Local,
|
|
|
|
),
|
|
|
|
detail: "S",
|
2021-03-11 21:11:14 -06:00
|
|
|
relevance: CompletionRelevance {
|
2021-03-09 11:24:09 -06:00
|
|
|
exact_name_match: true,
|
2021-03-13 09:25:41 -06:00
|
|
|
exact_type_match: false,
|
2021-03-13 08:13:30 -06:00
|
|
|
is_local: true,
|
2021-03-09 11:24:09 -06:00
|
|
|
},
|
2021-03-09 09:06:08 -06:00
|
|
|
ref_match: "&mut ",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"#]],
|
|
|
|
)
|
|
|
|
}
|
2021-03-13 06:34:11 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn suggest_deref() {
|
2021-03-13 07:32:37 -06:00
|
|
|
check_relevance(
|
2021-03-13 06:34:11 -06:00
|
|
|
r#"
|
|
|
|
#[lang = "deref"]
|
|
|
|
trait Deref {
|
|
|
|
type Target;
|
|
|
|
fn deref(&self) -> &Self::Target;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct S;
|
|
|
|
struct T(S);
|
|
|
|
|
|
|
|
impl Deref for T {
|
|
|
|
type Target = S;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo(s: &S) {}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let t = T(S);
|
|
|
|
let m = 123;
|
|
|
|
|
|
|
|
foo($0);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2021-03-13 08:13:30 -06:00
|
|
|
lc m [local]
|
|
|
|
lc t [local]
|
|
|
|
lc &t [type+local]
|
2021-03-13 10:31:52 -06:00
|
|
|
st T []
|
|
|
|
st S []
|
2021-03-14 05:25:37 -05:00
|
|
|
fn main() []
|
2021-03-13 10:31:52 -06:00
|
|
|
tt Deref []
|
2021-03-14 05:25:37 -05:00
|
|
|
fn foo(…) []
|
2021-03-13 09:25:41 -06:00
|
|
|
"#]],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn suggest_deref_mut() {
|
|
|
|
check_relevance(
|
|
|
|
r#"
|
|
|
|
#[lang = "deref"]
|
|
|
|
trait Deref {
|
|
|
|
type Target;
|
|
|
|
fn deref(&self) -> &Self::Target;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[lang = "deref_mut"]
|
|
|
|
pub trait DerefMut: Deref {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct S;
|
|
|
|
struct T(S);
|
|
|
|
|
|
|
|
impl Deref for T {
|
|
|
|
type Target = S;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for T {
|
2021-03-13 09:28:05 -06:00
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
2021-03-13 09:25:41 -06:00
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo(s: &mut S) {}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let t = T(S);
|
|
|
|
let m = 123;
|
|
|
|
|
|
|
|
foo($0);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2021-03-13 08:13:30 -06:00
|
|
|
lc m [local]
|
|
|
|
lc t [local]
|
|
|
|
lc &mut t [type+local]
|
2021-03-14 05:25:37 -05:00
|
|
|
tt DerefMut []
|
|
|
|
tt Deref []
|
2021-03-13 10:31:52 -06:00
|
|
|
fn foo(…) []
|
|
|
|
st T []
|
|
|
|
st S []
|
2021-03-14 05:25:37 -05:00
|
|
|
fn main() []
|
2021-03-13 06:34:11 -06:00
|
|
|
"#]],
|
|
|
|
)
|
|
|
|
}
|
2021-03-13 08:13:30 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn locals() {
|
|
|
|
check_relevance(
|
|
|
|
r#"
|
|
|
|
fn foo(bar: u32) {
|
|
|
|
let baz = 0;
|
|
|
|
|
|
|
|
f$0
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
lc baz [local]
|
|
|
|
lc bar [local]
|
|
|
|
fn foo(…) []
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
2020-11-01 04:36:30 -06:00
|
|
|
}
|