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;
|
2021-08-03 17:46:50 -05:00
|
|
|
pub(crate) mod struct_literal;
|
2020-11-03 01:36:01 -06:00
|
|
|
|
2020-11-01 03:35:04 -06:00
|
|
|
mod builder_ext;
|
|
|
|
|
2021-12-21 07:07:48 -06:00
|
|
|
use hir::{AsAssocItem, HasAttrs, HirDisplay, ScopeDef};
|
2021-02-24 17:53:59 -06:00
|
|
|
use ide_db::{
|
|
|
|
helpers::{item_name, SnippetCap},
|
|
|
|
RootDatabase, SymbolKind,
|
|
|
|
};
|
2021-11-08 12:41:16 -06:00
|
|
|
use syntax::{SmolStr, SyntaxKind, TextRange};
|
2020-11-01 03:35:04 -06:00
|
|
|
|
2020-11-01 04:36:30 -06:00
|
|
|
use crate::{
|
2022-02-02 09:01:46 -06:00
|
|
|
context::{PathCompletionCtx, PathKind},
|
2021-03-22 23:18:34 -05:00
|
|
|
item::{CompletionRelevanceTypeMatch, ImportEdit},
|
2021-05-31 07:13:09 -05:00
|
|
|
render::{enum_variant::render_variant, function::render_fn, macro_::render_macro},
|
2021-10-27 10:18:42 -05:00
|
|
|
CompletionContext, CompletionItem, CompletionItemKind, CompletionRelevance,
|
2020-11-01 04:36:30 -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>,
|
2022-02-01 19:05:49 -06:00
|
|
|
is_private_editable: bool,
|
2020-11-01 03:35:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> RenderContext<'a> {
|
2022-02-01 19:05:49 -06:00
|
|
|
pub(crate) fn new(
|
|
|
|
completion: &'a CompletionContext<'a>,
|
|
|
|
is_private_editable: bool,
|
|
|
|
) -> RenderContext<'a> {
|
|
|
|
RenderContext { completion, is_private_editable }
|
2020-11-01 03:35:04 -06:00
|
|
|
}
|
|
|
|
|
2020-11-01 04:36:30 -06:00
|
|
|
fn snippet_cap(&self) -> Option<SnippetCap> {
|
2021-03-16 19:56:31 -05:00
|
|
|
self.completion.config.snippet_cap
|
2020-11-01 03:35:04 -06:00
|
|
|
}
|
|
|
|
|
2020-11-01 04:36:30 -06:00
|
|
|
fn db(&self) -> &'a RootDatabase {
|
2021-06-12 22:54:16 -05:00
|
|
|
self.completion.db
|
2020-11-01 03:35:04 -06:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2022-02-01 19:05:49 -06:00
|
|
|
fn completion_relevance(&self) -> CompletionRelevance {
|
|
|
|
CompletionRelevance { is_private_editable: self.is_private_editable, ..Default::default() }
|
|
|
|
}
|
|
|
|
|
2021-12-21 08:20:15 -06:00
|
|
|
fn is_deprecated(&self, def: impl HasAttrs) -> bool {
|
|
|
|
let attrs = def.attrs(self.db());
|
2021-01-18 16:02:59 -06:00
|
|
|
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
|
2021-06-11 12:55:24 -05:00
|
|
|
|| assoc
|
|
|
|
.containing_trait_or_trait_impl(db)
|
|
|
|
.map(|trait_| self.is_deprecated(trait_))
|
|
|
|
.unwrap_or(false)
|
2021-01-18 17:08:59 -06:00
|
|
|
}
|
|
|
|
|
2021-12-21 19:25:38 -06:00
|
|
|
// FIXME: remove this
|
2021-12-21 08:20:15 -06:00
|
|
|
fn docs(&self, def: impl HasAttrs) -> Option<hir::Documentation> {
|
|
|
|
def.docs(self.db())
|
2020-11-01 03:59:43 -06:00
|
|
|
}
|
2020-11-01 03:35:04 -06:00
|
|
|
}
|
|
|
|
|
2021-06-16 08:46:58 -05:00
|
|
|
pub(crate) fn render_field(
|
2021-06-09 16:43:22 -05:00
|
|
|
ctx: RenderContext<'_>,
|
|
|
|
receiver: Option<hir::Name>,
|
|
|
|
field: hir::Field,
|
|
|
|
ty: &hir::Type,
|
|
|
|
) -> CompletionItem {
|
|
|
|
let is_deprecated = ctx.is_deprecated(field);
|
2021-11-05 06:30:39 -05:00
|
|
|
let name = field.name(ctx.db()).to_smol_str();
|
2021-06-09 16:43:22 -05:00
|
|
|
let mut item = CompletionItem::new(
|
2021-10-27 10:18:42 -05:00
|
|
|
SymbolKind::Field,
|
2021-06-09 16:43:22 -05:00
|
|
|
ctx.source_range(),
|
2021-11-05 06:30:39 -05:00
|
|
|
receiver.map_or_else(|| name.clone(), |receiver| format!("{}.{}", receiver, name).into()),
|
2021-06-09 16:43:22 -05:00
|
|
|
);
|
|
|
|
item.set_relevance(CompletionRelevance {
|
|
|
|
type_match: compute_type_match(ctx.completion, ty),
|
2021-09-04 18:28:59 -05:00
|
|
|
exact_name_match: compute_exact_name_match(ctx.completion, name.as_str()),
|
2021-06-09 16:43:22 -05:00
|
|
|
..CompletionRelevance::default()
|
|
|
|
});
|
2021-10-27 10:18:42 -05:00
|
|
|
item.detail(ty.display(ctx.db()).to_string())
|
2021-06-11 11:26:52 -05:00
|
|
|
.set_documentation(field.docs(ctx.db()))
|
|
|
|
.set_deprecated(is_deprecated)
|
2021-11-05 06:30:39 -05:00
|
|
|
.lookup_by(name.clone());
|
2021-09-04 18:28:59 -05:00
|
|
|
let is_keyword = SyntaxKind::from_keyword(name.as_str()).is_some();
|
|
|
|
if is_keyword && !matches!(name.as_str(), "self" | "crate" | "super" | "Self") {
|
2021-09-06 09:55:07 -05:00
|
|
|
item.insert_text(format!("r#{}", name));
|
2021-09-04 18:28:59 -05:00
|
|
|
}
|
2021-06-09 16:43:22 -05:00
|
|
|
if let Some(_ref_match) = compute_ref_match(ctx.completion, ty) {
|
|
|
|
// FIXME
|
|
|
|
// For now we don't properly calculate the edits for ref match
|
|
|
|
// completions on struct fields, so we've disabled them. See #8058.
|
2020-11-01 04:36:30 -06:00
|
|
|
}
|
2021-06-09 16:43:22 -05:00
|
|
|
item.build()
|
|
|
|
}
|
2020-11-01 04:36:30 -06:00
|
|
|
|
2021-06-16 08:46:58 -05:00
|
|
|
pub(crate) fn render_tuple_field(
|
2021-06-09 16:43:22 -05:00
|
|
|
ctx: RenderContext<'_>,
|
|
|
|
receiver: Option<hir::Name>,
|
|
|
|
field: usize,
|
|
|
|
ty: &hir::Type,
|
|
|
|
) -> CompletionItem {
|
|
|
|
let mut item = CompletionItem::new(
|
2021-10-27 10:18:42 -05:00
|
|
|
SymbolKind::Field,
|
2021-06-09 16:43:22 -05:00
|
|
|
ctx.source_range(),
|
|
|
|
receiver.map_or_else(|| field.to_string(), |receiver| format!("{}.{}", receiver, field)),
|
|
|
|
);
|
2021-10-27 10:18:42 -05:00
|
|
|
item.detail(ty.display(ctx.db()).to_string()).lookup_by(field.to_string());
|
2021-06-09 16:43:22 -05:00
|
|
|
item.build()
|
|
|
|
}
|
2020-11-01 04:36:30 -06:00
|
|
|
|
2021-06-16 08:46:58 -05:00
|
|
|
pub(crate) fn render_resolution(
|
|
|
|
ctx: RenderContext<'_>,
|
|
|
|
local_name: hir::Name,
|
2021-12-21 07:07:48 -06:00
|
|
|
resolution: ScopeDef,
|
2022-01-04 03:56:17 -06:00
|
|
|
) -> CompletionItem {
|
2021-06-16 08:46:58 -05:00
|
|
|
render_resolution_(ctx, local_name, None, resolution)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn render_resolution_with_import(
|
|
|
|
ctx: RenderContext<'_>,
|
|
|
|
import_edit: ImportEdit,
|
|
|
|
) -> Option<CompletionItem> {
|
2021-12-21 07:07:48 -06:00
|
|
|
let resolution = ScopeDef::from(import_edit.import.original_item);
|
2021-06-16 08:46:58 -05:00
|
|
|
let local_name = match resolution {
|
2021-12-21 07:07:48 -06:00
|
|
|
ScopeDef::ModuleDef(hir::ModuleDef::Function(f)) => f.name(ctx.completion.db),
|
|
|
|
ScopeDef::ModuleDef(hir::ModuleDef::Const(c)) => c.name(ctx.completion.db)?,
|
|
|
|
ScopeDef::ModuleDef(hir::ModuleDef::TypeAlias(t)) => t.name(ctx.completion.db),
|
2021-06-16 08:46:58 -05:00
|
|
|
_ => item_name(ctx.db(), import_edit.import.original_item)?,
|
|
|
|
};
|
2022-01-04 03:56:17 -06:00
|
|
|
Some(render_resolution_(ctx, local_name, Some(import_edit), resolution))
|
2021-06-16 08:46:58 -05:00
|
|
|
}
|
|
|
|
|
2021-06-09 16:43:22 -05:00
|
|
|
fn render_resolution_(
|
|
|
|
ctx: RenderContext<'_>,
|
|
|
|
local_name: hir::Name,
|
|
|
|
import_to_add: Option<ImportEdit>,
|
2021-12-21 07:07:48 -06:00
|
|
|
resolution: ScopeDef,
|
2022-01-04 03:56:17 -06:00
|
|
|
) -> CompletionItem {
|
2021-06-09 16:43:22 -05:00
|
|
|
let _p = profile::span("render_resolution");
|
|
|
|
use hir::ModuleDef::*;
|
2020-11-01 04:36:30 -06:00
|
|
|
|
2021-12-21 07:51:06 -06:00
|
|
|
let db = ctx.db();
|
|
|
|
|
2021-06-09 16:43:22 -05:00
|
|
|
let kind = match resolution {
|
2021-12-21 07:07:48 -06:00
|
|
|
ScopeDef::ModuleDef(Function(func)) => {
|
2022-01-04 03:56:17 -06:00
|
|
|
return render_fn(ctx, import_to_add, Some(local_name), func);
|
2021-06-09 16:43:22 -05:00
|
|
|
}
|
2021-12-21 07:07:48 -06:00
|
|
|
ScopeDef::ModuleDef(Variant(var)) if ctx.completion.pattern_ctx.is_none() => {
|
2022-01-04 03:56:17 -06:00
|
|
|
return render_variant(ctx, import_to_add, Some(local_name), var, None);
|
2021-06-09 16:43:22 -05:00
|
|
|
}
|
2021-12-21 07:51:06 -06:00
|
|
|
ScopeDef::MacroDef(mac) => return render_macro(ctx, import_to_add, local_name, mac),
|
2021-12-21 07:07:48 -06:00
|
|
|
ScopeDef::Unknown => {
|
|
|
|
let mut item = CompletionItem::new(
|
|
|
|
CompletionItemKind::UnresolvedReference,
|
|
|
|
ctx.source_range(),
|
|
|
|
local_name.to_smol_str(),
|
|
|
|
);
|
|
|
|
if let Some(import_to_add) = import_to_add {
|
|
|
|
item.add_import(import_to_add);
|
|
|
|
}
|
2022-01-04 03:56:17 -06:00
|
|
|
return item.build();
|
2021-12-21 07:07:48 -06:00
|
|
|
}
|
2020-11-01 04:36:30 -06:00
|
|
|
|
2021-12-21 07:07:48 -06:00
|
|
|
ScopeDef::ModuleDef(Variant(_)) => CompletionItemKind::SymbolKind(SymbolKind::Variant),
|
|
|
|
ScopeDef::ModuleDef(Module(..)) => CompletionItemKind::SymbolKind(SymbolKind::Module),
|
|
|
|
ScopeDef::ModuleDef(Adt(adt)) => CompletionItemKind::SymbolKind(match adt {
|
2021-06-09 16:43:22 -05:00
|
|
|
hir::Adt::Struct(_) => SymbolKind::Struct,
|
|
|
|
hir::Adt::Union(_) => SymbolKind::Union,
|
|
|
|
hir::Adt::Enum(_) => SymbolKind::Enum,
|
|
|
|
}),
|
2021-12-21 07:07:48 -06:00
|
|
|
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),
|
|
|
|
ScopeDef::ModuleDef(BuiltinType(..)) => CompletionItemKind::BuiltinType,
|
|
|
|
ScopeDef::GenericParam(param) => CompletionItemKind::SymbolKind(match param {
|
2021-06-09 16:43:22 -05:00
|
|
|
hir::GenericParam::TypeParam(_) => SymbolKind::TypeParam,
|
|
|
|
hir::GenericParam::LifetimeParam(_) => SymbolKind::LifetimeParam,
|
|
|
|
hir::GenericParam::ConstParam(_) => SymbolKind::ConstParam,
|
|
|
|
}),
|
2021-12-21 07:07:48 -06:00
|
|
|
ScopeDef::Local(..) => CompletionItemKind::SymbolKind(SymbolKind::Local),
|
|
|
|
ScopeDef::Label(..) => CompletionItemKind::SymbolKind(SymbolKind::Label),
|
|
|
|
ScopeDef::AdtSelfType(..) | ScopeDef::ImplSelfType(..) => {
|
2021-06-09 16:43:22 -05:00
|
|
|
CompletionItemKind::SymbolKind(SymbolKind::SelfParam)
|
|
|
|
}
|
|
|
|
};
|
2020-11-01 04:36:30 -06:00
|
|
|
|
2021-11-05 06:30:39 -05:00
|
|
|
let local_name = local_name.to_smol_str();
|
2021-10-27 10:18:42 -05:00
|
|
|
let mut item = CompletionItem::new(kind, ctx.source_range(), local_name.clone());
|
2021-12-21 07:07:48 -06:00
|
|
|
if let ScopeDef::Local(local) = resolution {
|
2021-12-21 07:51:06 -06:00
|
|
|
let ty = local.ty(db);
|
2021-06-09 16:43:22 -05:00
|
|
|
if !ty.is_unknown() {
|
2021-12-21 07:51:06 -06:00
|
|
|
item.detail(ty.display(db).to_string());
|
2021-06-09 16:43:22 -05:00
|
|
|
}
|
2020-11-01 04:36:30 -06:00
|
|
|
|
2021-06-09 16:43:22 -05:00
|
|
|
item.set_relevance(CompletionRelevance {
|
|
|
|
type_match: compute_type_match(ctx.completion, &ty),
|
|
|
|
exact_name_match: compute_exact_name_match(ctx.completion, &local_name),
|
|
|
|
is_local: true,
|
|
|
|
..CompletionRelevance::default()
|
|
|
|
});
|
2021-03-13 09:25:41 -06:00
|
|
|
|
2021-06-09 16:43:22 -05:00
|
|
|
if let Some(ref_match) = compute_ref_match(ctx.completion, &ty) {
|
|
|
|
item.ref_match(ref_match);
|
|
|
|
}
|
|
|
|
};
|
2020-11-01 04:36:30 -06:00
|
|
|
|
2021-06-09 16:43:22 -05:00
|
|
|
// Add `<>` for generic types
|
2021-12-21 07:51:06 -06:00
|
|
|
let type_path_no_ty_args = matches!(
|
2021-06-09 16:43:22 -05:00
|
|
|
ctx.completion.path_context,
|
2022-02-02 09:01:46 -06:00
|
|
|
Some(PathCompletionCtx { kind: Some(PathKind::Type), has_type_args: false, .. })
|
2021-12-21 07:51:06 -06:00
|
|
|
) && ctx.completion.config.add_call_parenthesis;
|
|
|
|
if type_path_no_ty_args {
|
2021-06-09 16:43:22 -05:00
|
|
|
if let Some(cap) = ctx.snippet_cap() {
|
|
|
|
let has_non_default_type_params = match resolution {
|
2021-12-21 07:51:06 -06:00
|
|
|
ScopeDef::ModuleDef(Adt(it)) => it.has_non_default_type_params(db),
|
|
|
|
ScopeDef::ModuleDef(TypeAlias(it)) => it.has_non_default_type_params(db),
|
2021-06-09 16:43:22 -05:00
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
if has_non_default_type_params {
|
|
|
|
cov_mark::hit!(inserts_angle_brackets_for_generics);
|
|
|
|
item.lookup_by(local_name.clone())
|
2021-11-08 12:41:16 -06:00
|
|
|
.label(SmolStr::from_iter([&local_name, "<…>"]))
|
2021-06-09 16:43:22 -05:00
|
|
|
.insert_snippet(cap, format!("{}<$0>", local_name));
|
2020-11-01 04:36:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-21 07:51:06 -06:00
|
|
|
item.set_documentation(scope_def_docs(db, resolution))
|
2021-06-09 16:43:22 -05:00
|
|
|
.set_deprecated(scope_def_is_deprecated(&ctx, resolution));
|
2021-10-04 14:44:33 -05:00
|
|
|
|
|
|
|
if let Some(import_to_add) = import_to_add {
|
|
|
|
item.add_import(import_to_add);
|
|
|
|
}
|
2022-01-04 03:56:17 -06:00
|
|
|
item.build()
|
2021-06-09 16:43:22 -05:00
|
|
|
}
|
2020-11-01 04:36:30 -06:00
|
|
|
|
2021-12-21 07:07:48 -06:00
|
|
|
fn scope_def_docs(db: &RootDatabase, resolution: ScopeDef) -> Option<hir::Documentation> {
|
2021-06-09 16:43:22 -05:00
|
|
|
use hir::ModuleDef::*;
|
|
|
|
match resolution {
|
2021-12-21 07:07:48 -06:00
|
|
|
ScopeDef::ModuleDef(Module(it)) => it.docs(db),
|
|
|
|
ScopeDef::ModuleDef(Adt(it)) => it.docs(db),
|
|
|
|
ScopeDef::ModuleDef(Variant(it)) => it.docs(db),
|
|
|
|
ScopeDef::ModuleDef(Const(it)) => it.docs(db),
|
|
|
|
ScopeDef::ModuleDef(Static(it)) => it.docs(db),
|
|
|
|
ScopeDef::ModuleDef(Trait(it)) => it.docs(db),
|
|
|
|
ScopeDef::ModuleDef(TypeAlias(it)) => it.docs(db),
|
2021-06-09 16:43:22 -05:00
|
|
|
_ => None,
|
2021-01-18 17:08:59 -06:00
|
|
|
}
|
2021-06-09 16:43:22 -05:00
|
|
|
}
|
2021-01-18 17:08:59 -06:00
|
|
|
|
2021-12-21 07:07:48 -06:00
|
|
|
fn scope_def_is_deprecated(ctx: &RenderContext<'_>, resolution: ScopeDef) -> bool {
|
2021-06-09 16:43:22 -05:00
|
|
|
match resolution {
|
2021-12-21 07:07:48 -06:00
|
|
|
ScopeDef::ModuleDef(it) => ctx.is_deprecated_assoc_item(it),
|
|
|
|
ScopeDef::MacroDef(it) => ctx.is_deprecated(it),
|
|
|
|
ScopeDef::GenericParam(it) => ctx.is_deprecated(it),
|
|
|
|
ScopeDef::AdtSelfType(it) => ctx.is_deprecated(it),
|
2021-06-09 16:43:22 -05:00
|
|
|
_ => false,
|
2020-11-01 04:36:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 23:18:34 -05:00
|
|
|
fn compute_type_match(
|
|
|
|
ctx: &CompletionContext,
|
|
|
|
completion_ty: &hir::Type,
|
|
|
|
) -> Option<CompletionRelevanceTypeMatch> {
|
|
|
|
let expected_type = ctx.expected_type.as_ref()?;
|
|
|
|
|
|
|
|
// We don't ever consider unit type to be an exact type match, since
|
|
|
|
// nearly always this is not meaningful to the user.
|
|
|
|
if expected_type.is_unit() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
if completion_ty == expected_type {
|
|
|
|
Some(CompletionRelevanceTypeMatch::Exact)
|
2021-05-15 13:18:54 -05:00
|
|
|
} else if expected_type.could_unify_with(ctx.db, completion_ty) {
|
2021-03-22 23:18:34 -05:00
|
|
|
Some(CompletionRelevanceTypeMatch::CouldUnify)
|
|
|
|
} else {
|
|
|
|
None
|
2021-03-15 21:26:59 -05:00
|
|
|
}
|
|
|
|
}
|
2021-03-13 08:13:30 -06:00
|
|
|
|
2021-05-31 07:13:09 -05:00
|
|
|
fn compute_exact_name_match(ctx: &CompletionContext, completion_name: &str) -> bool {
|
2021-03-20 17:22:09 -05:00
|
|
|
ctx.expected_name.as_ref().map_or(false, |name| name.text() == completion_name)
|
2020-11-01 04:36:30 -06:00
|
|
|
}
|
|
|
|
|
2021-05-31 07:13:09 -05:00
|
|
|
fn compute_ref_match(
|
|
|
|
ctx: &CompletionContext,
|
|
|
|
completion_ty: &hir::Type,
|
|
|
|
) -> Option<hir::Mutability> {
|
2021-03-16 08:54:17 -05:00
|
|
|
let expected_type = ctx.expected_type.as_ref()?;
|
|
|
|
if completion_ty != expected_type {
|
|
|
|
let expected_type_without_ref = expected_type.remove_ref()?;
|
|
|
|
if completion_ty.autoderef(ctx.db).any(|deref_ty| deref_ty == expected_type_without_ref) {
|
|
|
|
cov_mark::hit!(suggest_ref);
|
|
|
|
let mutability = if expected_type.is_mutable_reference() {
|
2021-05-31 07:13:09 -05:00
|
|
|
hir::Mutability::Mut
|
2021-03-16 08:54:17 -05:00
|
|
|
} else {
|
2021-05-31 07:13:09 -05:00
|
|
|
hir::Mutability::Shared
|
2021-03-16 08:54:17 -05:00
|
|
|
};
|
|
|
|
return Some(mutability);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
None
|
2021-03-13 06:34:11 -06:00
|
|
|
}
|
|
|
|
|
2020-11-01 04:36:30 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-07-04 08:54:30 -05:00
|
|
|
use std::cmp;
|
|
|
|
|
2020-11-01 04:36:30 -06:00
|
|
|
use expect_test::{expect, Expect};
|
2021-10-27 10:18:42 -05:00
|
|
|
use ide_db::SymbolKind;
|
2021-03-13 08:13:30 -06:00
|
|
|
use itertools::Itertools;
|
2020-11-01 04:36:30 -06:00
|
|
|
|
|
|
|
use crate::{
|
2021-03-22 23:18:34 -05:00
|
|
|
item::CompletionRelevanceTypeMatch,
|
2021-06-16 14:45:02 -05:00
|
|
|
tests::{check_edit, do_completion, get_all_items, TEST_CONFIG},
|
2021-10-27 10:18:42 -05:00
|
|
|
CompletionItem, CompletionItemKind, CompletionRelevance,
|
2020-11-01 04:36:30 -06:00
|
|
|
};
|
|
|
|
|
2021-07-04 08:50:02 -05:00
|
|
|
#[track_caller]
|
2021-10-27 10:18:42 -05:00
|
|
|
fn check(ra_fixture: &str, kind: impl Into<CompletionItemKind>, expect: Expect) {
|
|
|
|
let actual = do_completion(ra_fixture, kind.into());
|
2020-11-01 04:36:30 -06:00
|
|
|
expect.assert_debug_eq(&actual);
|
|
|
|
}
|
|
|
|
|
2021-07-04 08:50:02 -05:00
|
|
|
#[track_caller]
|
2021-10-27 10:18:42 -05:00
|
|
|
fn check_kinds(ra_fixture: &str, kinds: &[CompletionItemKind], expect: Expect) {
|
|
|
|
let actual: Vec<_> =
|
|
|
|
kinds.iter().flat_map(|&kind| do_completion(ra_fixture, kind)).collect();
|
|
|
|
expect.assert_debug_eq(&actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[track_caller]
|
|
|
|
fn check_relevance_for_kinds(ra_fixture: &str, kinds: &[CompletionItemKind], expect: Expect) {
|
|
|
|
let mut actual = get_all_items(TEST_CONFIG, ra_fixture);
|
|
|
|
actual.retain(|it| kinds.contains(&it.kind()));
|
|
|
|
actual.sort_by_key(|it| cmp::Reverse(it.relevance().score()));
|
|
|
|
check_relevance_(actual, expect);
|
2021-07-04 08:50:02 -05:00
|
|
|
}
|
2020-11-01 04:36:30 -06:00
|
|
|
|
2021-07-04 08:50:02 -05:00
|
|
|
#[track_caller]
|
2021-10-27 10:18:42 -05:00
|
|
|
fn check_relevance(ra_fixture: &str, expect: Expect) {
|
2021-07-04 08:54:30 -05:00
|
|
|
let mut actual = get_all_items(TEST_CONFIG, ra_fixture);
|
2021-10-27 10:18:42 -05:00
|
|
|
actual.retain(|it| it.kind() != CompletionItemKind::Snippet);
|
|
|
|
actual.retain(|it| it.kind() != CompletionItemKind::Keyword);
|
|
|
|
actual.retain(|it| it.kind() != CompletionItemKind::BuiltinType);
|
2021-07-04 08:54:30 -05:00
|
|
|
actual.sort_by_key(|it| cmp::Reverse(it.relevance().score()));
|
2021-10-27 10:18:42 -05:00
|
|
|
check_relevance_(actual, expect);
|
|
|
|
}
|
2021-07-04 08:54:30 -05:00
|
|
|
|
2021-10-27 10:18:42 -05:00
|
|
|
#[track_caller]
|
|
|
|
fn check_relevance_(actual: Vec<CompletionItem>, expect: Expect) {
|
2021-07-04 08:54:30 -05:00
|
|
|
let actual = actual
|
2020-11-01 04:36:30 -06:00
|
|
|
.into_iter()
|
2021-03-13 10:31:52 -06:00
|
|
|
.flat_map(|it| {
|
|
|
|
let mut items = vec![];
|
|
|
|
|
2021-10-27 10:18:42 -05:00
|
|
|
let tag = it.kind().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);
|
2021-07-04 08:50:02 -05:00
|
|
|
|
|
|
|
fn display_relevance(relevance: CompletionRelevance) -> String {
|
|
|
|
let relevance_factors = vec![
|
|
|
|
(relevance.type_match == Some(CompletionRelevanceTypeMatch::Exact), "type"),
|
|
|
|
(
|
|
|
|
relevance.type_match == Some(CompletionRelevanceTypeMatch::CouldUnify),
|
|
|
|
"type_could_unify",
|
|
|
|
),
|
|
|
|
(relevance.exact_name_match, "name"),
|
|
|
|
(relevance.is_local, "local"),
|
|
|
|
(relevance.exact_postfix_snippet_match, "snippet"),
|
2022-01-11 03:07:16 -06:00
|
|
|
(relevance.is_op_method, "op_method"),
|
2021-07-04 08:50:02 -05:00
|
|
|
]
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|(cond, desc)| if cond { Some(desc) } else { None })
|
|
|
|
.join("+");
|
|
|
|
|
|
|
|
format!("[{}]", relevance_factors)
|
|
|
|
}
|
2020-11-01 04:36:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
|
|
"#,
|
2021-10-27 10:18:42 -05:00
|
|
|
SymbolKind::Variant,
|
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,
|
|
|
|
),
|
2021-11-24 09:01:33 -06:00
|
|
|
detail: "{x: i32, y: i32}",
|
2020-11-01 04:36:30 -06:00
|
|
|
},
|
|
|
|
]
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
|
|
"#,
|
2021-10-27 10:18:42 -05:00
|
|
|
SymbolKind::Variant,
|
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,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-06 18:56:07 -06:00
|
|
|
#[test]
|
|
|
|
fn fn_detail_includes_args_and_return_type() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn foo<T>(a: u32, b: u32, t: T) -> (u32, T) { (a, t) }
|
|
|
|
|
|
|
|
fn main() { fo$0 }
|
|
|
|
"#,
|
2021-10-27 10:18:42 -05:00
|
|
|
SymbolKind::Function,
|
2021-03-06 18:56:07 -06:00
|
|
|
expect![[r#"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "foo(…)",
|
|
|
|
source_range: 68..70,
|
|
|
|
delete: 68..70,
|
|
|
|
insert: "foo(${1:a}, ${2:b}, ${3:t})$0",
|
|
|
|
kind: SymbolKind(
|
|
|
|
Function,
|
|
|
|
),
|
|
|
|
lookup: "foo",
|
|
|
|
detail: "fn(u32, u32, T) -> (u32, T)",
|
|
|
|
trigger_call_info: true,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "main()",
|
|
|
|
source_range: 68..70,
|
|
|
|
delete: 68..70,
|
|
|
|
insert: "main()$0",
|
|
|
|
kind: SymbolKind(
|
|
|
|
Function,
|
|
|
|
),
|
|
|
|
lookup: "main",
|
|
|
|
detail: "fn()",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-11-01 04:36:30 -06:00
|
|
|
#[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
|
|
|
"#,
|
2021-10-27 10:18:42 -05:00
|
|
|
SymbolKind::Variant,
|
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() {
|
2021-10-27 10:18:42 -05:00
|
|
|
check_kinds(
|
2020-11-01 04:36:30 -06:00
|
|
|
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
|
|
|
"#,
|
2021-10-27 10:18:42 -05:00
|
|
|
&[
|
|
|
|
CompletionItemKind::SymbolKind(SymbolKind::Function),
|
|
|
|
CompletionItemKind::SymbolKind(SymbolKind::Module),
|
|
|
|
CompletionItemKind::SymbolKind(SymbolKind::Variant),
|
|
|
|
],
|
2020-11-01 04:36:30 -06:00
|
|
|
expect![[r#"
|
|
|
|
[
|
2021-10-27 10:18:42 -05:00
|
|
|
CompletionItem {
|
|
|
|
label: "main()",
|
|
|
|
source_range: 75..76,
|
|
|
|
delete: 75..76,
|
|
|
|
insert: "main()$0",
|
|
|
|
kind: SymbolKind(
|
|
|
|
Function,
|
|
|
|
),
|
|
|
|
lookup: "main",
|
|
|
|
detail: "fn()",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "m",
|
|
|
|
source_range: 75..76,
|
|
|
|
delete: 75..76,
|
|
|
|
insert: "m",
|
|
|
|
kind: SymbolKind(
|
|
|
|
Module,
|
|
|
|
),
|
|
|
|
},
|
2020-11-01 04:36:30 -06:00
|
|
|
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)",
|
2021-03-15 21:26:59 -05:00
|
|
|
relevance: CompletionRelevance {
|
|
|
|
exact_name_match: false,
|
2021-03-22 23:18:34 -05:00
|
|
|
type_match: Some(
|
|
|
|
Exact,
|
|
|
|
),
|
2021-03-15 21:26:59 -05:00
|
|
|
is_local: false,
|
2022-01-06 06:36:43 -06:00
|
|
|
is_op_method: false,
|
2022-02-01 19:05:49 -06:00
|
|
|
is_private_editable: false,
|
2021-07-04 08:50:02 -05:00
|
|
|
exact_postfix_snippet_match: false,
|
2021-03-15 21:26:59 -05:00
|
|
|
},
|
2020-11-01 04:36:30 -06:00
|
|
|
trigger_call_info: true,
|
|
|
|
},
|
|
|
|
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: "()",
|
2021-03-15 21:26:59 -05:00
|
|
|
relevance: CompletionRelevance {
|
|
|
|
exact_name_match: false,
|
2021-03-22 23:18:34 -05:00
|
|
|
type_match: Some(
|
|
|
|
Exact,
|
|
|
|
),
|
2021-03-15 21:26:59 -05:00
|
|
|
is_local: false,
|
2022-01-06 06:36:43 -06:00
|
|
|
is_op_method: false,
|
2022-02-01 19:05:49 -06:00
|
|
|
is_private_editable: false,
|
2021-07-04 08:50:02 -05:00
|
|
|
exact_postfix_snippet_match: false,
|
2021-03-15 21:26:59 -05:00
|
|
|
},
|
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
|
|
|
"#,
|
2021-10-27 10:18:42 -05:00
|
|
|
SymbolKind::Function,
|
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-03-06 18:56:07 -06:00
|
|
|
detail: "fn()",
|
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-03-06 18:56:07 -06:00
|
|
|
detail: "fn()",
|
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-03-06 18:56:07 -06:00
|
|
|
detail: "fn()",
|
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
|
|
|
"#,
|
2021-10-27 10:18:42 -05:00
|
|
|
SymbolKind::Field,
|
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,
|
2021-05-23 11:10:40 -05:00
|
|
|
relevance: CompletionRelevance {
|
|
|
|
exact_name_match: false,
|
|
|
|
type_match: Some(
|
|
|
|
CouldUnify,
|
|
|
|
),
|
|
|
|
is_local: false,
|
2022-01-06 06:36:43 -06:00
|
|
|
is_op_method: false,
|
2022-02-01 19:05:49 -06:00
|
|
|
is_private_editable: false,
|
2021-07-04 08:50:02 -05:00
|
|
|
exact_postfix_snippet_match: false,
|
2021-05-23 11:10:40 -05:00
|
|
|
},
|
2020-11-01 04:36:30 -06:00
|
|
|
},
|
|
|
|
]
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn renders_docs() {
|
2021-10-27 10:18:42 -05:00
|
|
|
check_kinds(
|
2020-11-01 04:36:30 -06:00
|
|
|
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
|
|
|
}"#,
|
2021-10-27 10:18:42 -05:00
|
|
|
&[CompletionItemKind::Method, CompletionItemKind::SymbolKind(SymbolKind::Field)],
|
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-03-06 18:56:07 -06:00
|
|
|
detail: "fn(self)",
|
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",
|
|
|
|
),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
|
2021-10-27 10:18:42 -05:00
|
|
|
check_kinds(
|
2020-11-01 04:36:30 -06:00
|
|
|
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::*;
|
|
|
|
"#,
|
2021-10-27 10:18:42 -05:00
|
|
|
&[
|
|
|
|
CompletionItemKind::SymbolKind(SymbolKind::Module),
|
|
|
|
CompletionItemKind::SymbolKind(SymbolKind::Variant),
|
|
|
|
CompletionItemKind::SymbolKind(SymbolKind::Enum),
|
|
|
|
],
|
2020-11-01 04:36:30 -06:00
|
|
|
expect![[r#"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
2021-10-27 10:18:42 -05:00
|
|
|
label: "my",
|
2020-11-01 04:36:30 -06:00
|
|
|
source_range: 10..12,
|
|
|
|
delete: 10..12,
|
2021-10-27 10:18:42 -05:00
|
|
|
insert: "my",
|
2021-01-20 11:46:14 -06:00
|
|
|
kind: SymbolKind(
|
2021-10-27 10:18:42 -05:00
|
|
|
Module,
|
2021-01-20 11:46:14 -06:00
|
|
|
),
|
2020-11-01 04:36:30 -06:00
|
|
|
documentation: Documentation(
|
2021-10-27 10:18:42 -05:00
|
|
|
"mod docs",
|
2020-11-01 04:36:30 -06:00
|
|
|
),
|
|
|
|
},
|
|
|
|
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 {
|
2021-10-27 10:18:42 -05:00
|
|
|
label: "E",
|
2020-11-01 04:36:30 -06:00
|
|
|
source_range: 10..12,
|
|
|
|
delete: 10..12,
|
2021-10-27 10:18:42 -05:00
|
|
|
insert: "E",
|
2021-01-20 11:46:14 -06:00
|
|
|
kind: SymbolKind(
|
2021-10-27 10:18:42 -05:00
|
|
|
Enum,
|
2021-01-20 11:46:14 -06:00
|
|
|
),
|
2020-11-01 04:36:30 -06:00
|
|
|
documentation: Documentation(
|
2021-10-27 10:18:42 -05:00
|
|
|
"enum docs",
|
2020-11-01 04:36:30 -06:00
|
|
|
),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"#]],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
|
|
"#,
|
2021-10-27 10:18:42 -05:00
|
|
|
CompletionItemKind::Method,
|
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-03-06 18:56:07 -06:00
|
|
|
detail: "fn(&self)",
|
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 }
|
|
|
|
}
|
2021-09-04 18:28:59 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
check_edit(
|
|
|
|
"type",
|
|
|
|
r#"
|
|
|
|
struct RawIdentTable { r#type: u32 }
|
|
|
|
|
|
|
|
fn main() -> RawIdentTable {
|
|
|
|
RawIdentTable { t$0: 42 }
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct RawIdentTable { r#type: u32 }
|
|
|
|
|
|
|
|
fn main() -> RawIdentTable {
|
|
|
|
RawIdentTable { r#type: 42 }
|
|
|
|
}
|
2020-11-01 04:36:30 -06:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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#"
|
|
|
|
fd bar [type+name]
|
|
|
|
fd baz [type]
|
2021-07-04 08:54:30 -05:00
|
|
|
fd foo []
|
2020-11-01 04:36:30 -06:00
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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#"
|
|
|
|
fd bar [type+name]
|
|
|
|
fd baz [type]
|
2021-07-04 08:54:30 -05:00
|
|
|
fd foo []
|
2020-11-01 04:36:30 -06:00
|
|
|
"#]],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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#"
|
|
|
|
fd bar [type+name]
|
|
|
|
fd baz [type]
|
2021-07-04 08:54:30 -05:00
|
|
|
fd foo []
|
2020-11-01 04:36:30 -06:00
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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-06-13 04:33:08 -05:00
|
|
|
cov_mark::check!(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
|
|
|
|
2021-03-12 17:06:17 -06:00
|
|
|
#[test]
|
|
|
|
fn score_fn_type_and_name_match() {
|
|
|
|
check_relevance(
|
|
|
|
r#"
|
|
|
|
struct A { bar: u8 }
|
|
|
|
fn baz() -> u8 { 0 }
|
|
|
|
fn bar() -> u8 { 0 }
|
|
|
|
fn f() { A { bar: b$0 }; }
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2021-07-04 08:54:30 -05:00
|
|
|
fn bar() [type+name]
|
2021-03-12 17:06:17 -06:00
|
|
|
fn baz() [type]
|
|
|
|
st A []
|
|
|
|
fn f() []
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn score_method_type_and_name_match() {
|
|
|
|
check_relevance(
|
|
|
|
r#"
|
|
|
|
fn baz(aaa: u32){}
|
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
fn aaa(&self) -> u32 { 0 }
|
|
|
|
fn bbb(&self) -> u32 { 0 }
|
|
|
|
fn ccc(&self) -> u64 { 0 }
|
|
|
|
}
|
|
|
|
fn f() {
|
|
|
|
baz(Foo.$0
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
me aaa() [type+name]
|
|
|
|
me bbb() [type]
|
|
|
|
me ccc() []
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn score_method_name_match_only() {
|
|
|
|
check_relevance(
|
|
|
|
r#"
|
|
|
|
fn baz(aaa: u32){}
|
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
fn aaa(&self) -> u64 { 0 }
|
|
|
|
}
|
|
|
|
fn f() {
|
|
|
|
baz(Foo.$0
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
me aaa() [name]
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-09 09:06:08 -06:00
|
|
|
#[test]
|
|
|
|
fn suggest_ref_mut() {
|
|
|
|
cov_mark::check!(suggest_ref);
|
2021-06-20 11:32:45 -05:00
|
|
|
check_relevance(
|
2021-03-09 09:06:08 -06:00
|
|
|
r#"
|
|
|
|
struct S;
|
|
|
|
fn foo(s: &mut S) {}
|
|
|
|
fn main() {
|
|
|
|
let mut s = S;
|
|
|
|
foo($0);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2021-06-20 11:32:45 -05:00
|
|
|
lc s [name+local]
|
|
|
|
lc &mut s [type+name+local]
|
|
|
|
st S []
|
|
|
|
fn main() []
|
|
|
|
fn foo(…) []
|
2021-03-09 09:06:08 -06:00
|
|
|
"#]],
|
2021-06-20 11:32:45 -05:00
|
|
|
);
|
|
|
|
check_relevance(
|
|
|
|
r#"
|
|
|
|
struct S;
|
|
|
|
fn foo(s: &mut S) {}
|
|
|
|
fn main() {
|
|
|
|
let mut s = S;
|
|
|
|
foo(&mut $0);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
lc s [type+name+local]
|
|
|
|
st S []
|
|
|
|
fn main() []
|
|
|
|
fn foo(…) []
|
|
|
|
"#]],
|
|
|
|
);
|
2022-01-06 13:45:09 -06:00
|
|
|
check_relevance(
|
|
|
|
r#"
|
|
|
|
struct S;
|
|
|
|
fn foo(s: &mut S) {}
|
|
|
|
fn main() {
|
|
|
|
let mut ssss = S;
|
|
|
|
foo(&mut s$0);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
lc ssss [type+local]
|
|
|
|
st S []
|
|
|
|
fn main() []
|
|
|
|
fn foo(…) []
|
|
|
|
"#]],
|
|
|
|
);
|
2021-03-09 09:06:08 -06:00
|
|
|
}
|
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#"
|
2021-06-15 15:11:53 -05:00
|
|
|
//- minicore: deref
|
2021-03-13 06:34:11 -06:00
|
|
|
struct S;
|
|
|
|
struct T(S);
|
|
|
|
|
2021-06-15 15:11:53 -05:00
|
|
|
impl core::ops::Deref for T {
|
2021-03-13 06:34:11 -06:00
|
|
|
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() []
|
|
|
|
fn foo(…) []
|
2021-06-15 15:11:53 -05:00
|
|
|
md core []
|
|
|
|
tt Sized []
|
2021-03-13 09:25:41 -06:00
|
|
|
"#]],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn suggest_deref_mut() {
|
|
|
|
check_relevance(
|
|
|
|
r#"
|
2021-06-16 02:30:29 -05:00
|
|
|
//- minicore: deref_mut
|
2021-03-13 09:25:41 -06:00
|
|
|
struct S;
|
|
|
|
struct T(S);
|
|
|
|
|
2021-06-16 02:30:29 -05:00
|
|
|
impl core::ops::Deref for T {
|
2021-03-13 09:25:41 -06:00
|
|
|
type Target = S;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-16 02:30:29 -05:00
|
|
|
impl core::ops::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-13 10:31:52 -06:00
|
|
|
st T []
|
|
|
|
st S []
|
2021-03-14 05:25:37 -05:00
|
|
|
fn main() []
|
2021-06-16 02:30:29 -05:00
|
|
|
fn foo(…) []
|
|
|
|
md core []
|
|
|
|
tt Sized []
|
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(…) []
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
2021-03-15 21:26:59 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn enum_owned() {
|
|
|
|
check_relevance(
|
|
|
|
r#"
|
|
|
|
enum Foo { A, B }
|
|
|
|
fn foo() {
|
|
|
|
bar($0);
|
|
|
|
}
|
|
|
|
fn bar(t: Foo) {}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
ev Foo::A [type]
|
|
|
|
ev Foo::B [type]
|
|
|
|
en Foo []
|
|
|
|
fn bar(…) []
|
|
|
|
fn foo() []
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn enum_ref() {
|
|
|
|
check_relevance(
|
|
|
|
r#"
|
|
|
|
enum Foo { A, B }
|
|
|
|
fn foo() {
|
|
|
|
bar($0);
|
|
|
|
}
|
|
|
|
fn bar(t: &Foo) {}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
ev Foo::A []
|
|
|
|
ev &Foo::A [type]
|
|
|
|
ev Foo::B []
|
|
|
|
ev &Foo::B [type]
|
|
|
|
en Foo []
|
|
|
|
fn bar(…) []
|
|
|
|
fn foo() []
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn suggest_deref_fn_ret() {
|
|
|
|
check_relevance(
|
|
|
|
r#"
|
2021-06-18 14:47:02 -05:00
|
|
|
//- minicore: deref
|
2021-03-15 21:26:59 -05:00
|
|
|
struct S;
|
|
|
|
struct T(S);
|
|
|
|
|
2021-06-18 14:47:02 -05:00
|
|
|
impl core::ops::Deref for T {
|
2021-03-15 21:26:59 -05:00
|
|
|
type Target = S;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo(s: &S) {}
|
|
|
|
fn bar() -> T {}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
foo($0);
|
|
|
|
}
|
2021-06-18 14:47:02 -05:00
|
|
|
"#,
|
2021-03-15 21:26:59 -05:00
|
|
|
expect![[r#"
|
|
|
|
st T []
|
|
|
|
st S []
|
|
|
|
fn main() []
|
2021-06-18 14:47:02 -05:00
|
|
|
fn bar() []
|
|
|
|
fn &bar() [type]
|
|
|
|
fn foo(…) []
|
|
|
|
md core []
|
|
|
|
tt Sized []
|
2021-03-15 21:26:59 -05:00
|
|
|
"#]],
|
|
|
|
)
|
|
|
|
}
|
2021-03-22 22:34:02 -05:00
|
|
|
|
2022-01-11 03:07:16 -06:00
|
|
|
#[test]
|
2022-02-01 05:33:55 -06:00
|
|
|
fn op_function_relevances() {
|
2022-01-11 03:07:16 -06:00
|
|
|
check_relevance(
|
|
|
|
r#"
|
|
|
|
#[lang = "sub"]
|
|
|
|
trait Sub {
|
|
|
|
fn sub(self, other: Self) -> Self { self }
|
|
|
|
}
|
|
|
|
impl Sub for u32 {}
|
|
|
|
fn foo(a: u32) { a.$0 }
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
me sub(…) (as Sub) [op_method]
|
|
|
|
"#]],
|
2022-02-01 05:33:55 -06:00
|
|
|
);
|
|
|
|
check_relevance(
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
fn new() -> Self {}
|
|
|
|
}
|
|
|
|
#[lang = "eq"]
|
|
|
|
pub trait PartialEq<Rhs: ?Sized = Self> {
|
|
|
|
fn eq(&self, other: &Rhs) -> bool;
|
|
|
|
fn ne(&self, other: &Rhs) -> bool;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq for Foo {}
|
|
|
|
fn main() {
|
|
|
|
Foo::$0
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
|
|
|
fn new() []
|
|
|
|
me eq(…) (as PartialEq) [op_method]
|
|
|
|
me ne(…) (as PartialEq) [op_method]
|
|
|
|
"#]],
|
|
|
|
);
|
2022-01-11 03:07:16 -06:00
|
|
|
}
|
|
|
|
|
2021-03-22 22:34:02 -05:00
|
|
|
#[test]
|
|
|
|
fn struct_field_method_ref() {
|
2021-10-27 10:18:42 -05:00
|
|
|
check_kinds(
|
2021-03-22 22:34:02 -05:00
|
|
|
r#"
|
|
|
|
struct Foo { bar: u32 }
|
|
|
|
impl Foo { fn baz(&self) -> u32 { 0 } }
|
|
|
|
|
|
|
|
fn foo(f: Foo) { let _: &u32 = f.b$0 }
|
|
|
|
"#,
|
2021-10-27 10:18:42 -05:00
|
|
|
&[CompletionItemKind::Method, CompletionItemKind::SymbolKind(SymbolKind::Field)],
|
2021-03-22 22:34:02 -05:00
|
|
|
// FIXME
|
|
|
|
// Ideally we'd also suggest &f.bar and &f.baz() as exact
|
|
|
|
// type matches. See #8058.
|
|
|
|
expect![[r#"
|
|
|
|
[
|
2021-10-27 10:18:42 -05:00
|
|
|
CompletionItem {
|
|
|
|
label: "baz()",
|
|
|
|
source_range: 98..99,
|
|
|
|
delete: 98..99,
|
|
|
|
insert: "baz()$0",
|
|
|
|
kind: Method,
|
|
|
|
lookup: "baz",
|
|
|
|
detail: "fn(&self) -> u32",
|
|
|
|
},
|
2021-03-22 22:34:02 -05:00
|
|
|
CompletionItem {
|
|
|
|
label: "bar",
|
|
|
|
source_range: 98..99,
|
|
|
|
delete: 98..99,
|
|
|
|
insert: "bar",
|
|
|
|
kind: SymbolKind(
|
|
|
|
Field,
|
|
|
|
),
|
|
|
|
detail: "u32",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
2021-03-16 10:45:46 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn generic_enum() {
|
|
|
|
check_relevance(
|
|
|
|
r#"
|
|
|
|
enum Foo<T> { A(T), B }
|
|
|
|
// bar() should not be an exact type match
|
|
|
|
// because the generic parameters are different
|
|
|
|
fn bar() -> Foo<u8> { Foo::B }
|
|
|
|
// FIXME baz() should be an exact type match
|
|
|
|
// because the types could unify, but it currently
|
|
|
|
// is not. This is due to the T here being
|
|
|
|
// TyKind::Placeholder rather than TyKind::Missing.
|
|
|
|
fn baz<T>() -> Foo<T> { Foo::B }
|
|
|
|
fn foo() {
|
|
|
|
let foo: Foo<u32> = Foo::B;
|
|
|
|
let _: Foo<u32> = f$0;
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2021-07-04 08:54:30 -05:00
|
|
|
lc foo [type+local]
|
2021-03-22 23:18:34 -05:00
|
|
|
ev Foo::A(…) [type_could_unify]
|
|
|
|
ev Foo::B [type_could_unify]
|
2021-08-03 09:36:06 -05:00
|
|
|
fn foo() []
|
2021-03-16 10:45:46 -05:00
|
|
|
en Foo []
|
|
|
|
fn baz() []
|
|
|
|
fn bar() []
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
2021-07-04 08:50:02 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn postfix_completion_relevance() {
|
|
|
|
check_relevance_for_kinds(
|
|
|
|
r#"
|
|
|
|
mod ops {
|
|
|
|
pub trait Not {
|
|
|
|
type Output;
|
|
|
|
fn not(self) -> Self::Output;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Not for bool {
|
|
|
|
type Output = bool;
|
|
|
|
fn not(self) -> bool { if self { false } else { true }}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let _: bool = (9 > 2).not$0;
|
|
|
|
}
|
2021-10-27 10:18:42 -05:00
|
|
|
"#,
|
|
|
|
&[CompletionItemKind::Snippet, CompletionItemKind::Method],
|
2021-07-04 08:50:02 -05:00
|
|
|
expect![[r#"
|
2021-07-04 08:54:30 -05:00
|
|
|
sn not [snippet]
|
2021-07-05 07:14:44 -05:00
|
|
|
me not() (use ops::Not) [type_could_unify]
|
2021-07-04 08:50:02 -05:00
|
|
|
sn if []
|
|
|
|
sn while []
|
|
|
|
sn ref []
|
|
|
|
sn refm []
|
|
|
|
sn match []
|
|
|
|
sn box []
|
|
|
|
sn dbg []
|
|
|
|
sn dbgr []
|
|
|
|
sn call []
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
2020-11-01 04:36:30 -06:00
|
|
|
}
|