Merge #9077
9077: internal: Use `Name`s instead of Strings in the completion rendering api r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
commit
d7cbb49057
crates/ide_completion/src
@ -18,7 +18,7 @@ pub(crate) mod unqualified_path;
|
||||
|
||||
use std::iter;
|
||||
|
||||
use hir::{known, ModPath, ScopeDef, Type};
|
||||
use hir::known;
|
||||
use ide_db::SymbolKind;
|
||||
|
||||
use crate::{
|
||||
@ -69,12 +69,17 @@ impl Completions {
|
||||
items.into_iter().for_each(|item| self.add(item.into()))
|
||||
}
|
||||
|
||||
pub(crate) fn add_field(&mut self, ctx: &CompletionContext, field: hir::Field, ty: &Type) {
|
||||
pub(crate) fn add_field(&mut self, ctx: &CompletionContext, field: hir::Field, ty: &hir::Type) {
|
||||
let item = render_field(RenderContext::new(ctx), field, ty);
|
||||
self.add(item);
|
||||
}
|
||||
|
||||
pub(crate) fn add_tuple_field(&mut self, ctx: &CompletionContext, field: usize, ty: &Type) {
|
||||
pub(crate) fn add_tuple_field(
|
||||
&mut self,
|
||||
ctx: &CompletionContext,
|
||||
field: usize,
|
||||
ty: &hir::Type,
|
||||
) {
|
||||
let item = render_tuple_field(RenderContext::new(ctx), field, ty);
|
||||
self.add(item);
|
||||
}
|
||||
@ -89,8 +94,8 @@ impl Completions {
|
||||
pub(crate) fn add_resolution(
|
||||
&mut self,
|
||||
ctx: &CompletionContext,
|
||||
local_name: String,
|
||||
resolution: &ScopeDef,
|
||||
local_name: hir::Name,
|
||||
resolution: &hir::ScopeDef,
|
||||
) {
|
||||
if let Some(item) = render_resolution(RenderContext::new(ctx), local_name, resolution) {
|
||||
self.add(item);
|
||||
@ -100,7 +105,7 @@ impl Completions {
|
||||
pub(crate) fn add_macro(
|
||||
&mut self,
|
||||
ctx: &CompletionContext,
|
||||
name: Option<String>,
|
||||
name: Option<hir::Name>,
|
||||
macro_: hir::MacroDef,
|
||||
) {
|
||||
let name = match name {
|
||||
@ -116,7 +121,7 @@ impl Completions {
|
||||
&mut self,
|
||||
ctx: &CompletionContext,
|
||||
func: hir::Function,
|
||||
local_name: Option<String>,
|
||||
local_name: Option<hir::Name>,
|
||||
) {
|
||||
if let Some(item) = render_fn(RenderContext::new(ctx), None, local_name, func) {
|
||||
self.add(item)
|
||||
@ -127,7 +132,7 @@ impl Completions {
|
||||
&mut self,
|
||||
ctx: &CompletionContext,
|
||||
func: hir::Function,
|
||||
local_name: Option<String>,
|
||||
local_name: Option<hir::Name>,
|
||||
) {
|
||||
if let Some(item) = render_method(RenderContext::new(ctx), None, local_name, func) {
|
||||
self.add(item)
|
||||
@ -149,7 +154,7 @@ impl Completions {
|
||||
&mut self,
|
||||
ctx: &CompletionContext,
|
||||
variant: hir::Variant,
|
||||
path: ModPath,
|
||||
path: hir::ModPath,
|
||||
) {
|
||||
if let Some(item) = render_variant_pat(RenderContext::new(ctx), variant, None, Some(path)) {
|
||||
self.add(item);
|
||||
@ -183,7 +188,7 @@ impl Completions {
|
||||
&mut self,
|
||||
ctx: &CompletionContext,
|
||||
variant: hir::Variant,
|
||||
path: ModPath,
|
||||
path: hir::ModPath,
|
||||
) {
|
||||
let item = render_variant(RenderContext::new(ctx), None, None, variant, Some(path));
|
||||
self.add(item);
|
||||
@ -193,7 +198,7 @@ impl Completions {
|
||||
&mut self,
|
||||
ctx: &CompletionContext,
|
||||
variant: hir::Variant,
|
||||
local_name: Option<String>,
|
||||
local_name: Option<hir::Name>,
|
||||
) {
|
||||
let item = render_variant(RenderContext::new(ctx), None, local_name, variant, None);
|
||||
self.add(item);
|
||||
|
@ -16,15 +16,14 @@ pub(crate) fn complete_lifetime(acc: &mut Completions, ctx: &CompletionContext)
|
||||
(Some(lt), Some(lp)) if lp == lt.clone() => return,
|
||||
(Some(_), Some(lp)) => {
|
||||
lp_string = lp.to_string();
|
||||
Some(&lp_string)
|
||||
Some(&*lp_string)
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
|
||||
ctx.scope.process_all_names(&mut |name, res| {
|
||||
if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) = res {
|
||||
let name = name.to_string();
|
||||
if param_lifetime != Some(&name) {
|
||||
if param_lifetime != Some(&*name.to_string()) {
|
||||
acc.add_resolution(ctx, name, &res);
|
||||
}
|
||||
}
|
||||
@ -41,7 +40,7 @@ pub(crate) fn complete_label(acc: &mut Completions, ctx: &CompletionContext) {
|
||||
}
|
||||
ctx.scope.process_all_names(&mut |name, res| {
|
||||
if let ScopeDef::Label(_) = res {
|
||||
acc.add_resolution(ctx, name.to_string(), &res);
|
||||
acc.add_resolution(ctx, name, &res);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -11,11 +11,11 @@ pub(crate) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &Compl
|
||||
|
||||
ctx.scope.process_all_names(&mut |name, res| {
|
||||
if let hir::ScopeDef::MacroDef(mac) = res {
|
||||
acc.add_macro(ctx, Some(name.to_string()), mac);
|
||||
acc.add_macro(ctx, Some(name.clone()), mac);
|
||||
}
|
||||
// FIXME: This should be done in qualified_path/unqualified_path instead?
|
||||
if let hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = res {
|
||||
acc.add_resolution(ctx, name.to_string(), &res);
|
||||
acc.add_resolution(ctx, name, &res);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -51,7 +51,7 @@ pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
|
||||
_ => false,
|
||||
};
|
||||
if add_resolution {
|
||||
acc.add_resolution(ctx, name.to_string(), &res);
|
||||
acc.add_resolution(ctx, name, &res);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
//! Completion of paths, i.e. `some::prefix::$0`.
|
||||
|
||||
use hir::{Adt, HasVisibility, PathResolution, ScopeDef};
|
||||
use hir::HasVisibility;
|
||||
use rustc_hash::FxHashSet;
|
||||
use syntax::AstNode;
|
||||
|
||||
@ -21,14 +21,14 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
|
||||
};
|
||||
let context_module = ctx.scope.module();
|
||||
if ctx.expects_assoc_item() {
|
||||
if let PathResolution::Def(hir::ModuleDef::Module(module)) = resolution {
|
||||
if let hir::PathResolution::Def(hir::ModuleDef::Module(module)) = resolution {
|
||||
let module_scope = module.scope(ctx.db, context_module);
|
||||
for (name, def) in module_scope {
|
||||
if let ScopeDef::MacroDef(macro_def) = def {
|
||||
acc.add_macro(ctx, Some(name.to_string()), macro_def);
|
||||
if let hir::ScopeDef::MacroDef(macro_def) = def {
|
||||
acc.add_macro(ctx, Some(name.clone()), macro_def);
|
||||
}
|
||||
if let ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = def {
|
||||
acc.add_resolution(ctx, name.to_string(), &def);
|
||||
if let hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = def {
|
||||
acc.add_resolution(ctx, name, &def);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -42,11 +42,11 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
|
||||
});
|
||||
|
||||
match resolution {
|
||||
PathResolution::Def(hir::ModuleDef::Module(module)) => {
|
||||
hir::PathResolution::Def(hir::ModuleDef::Module(module)) => {
|
||||
let module_scope = module.scope(ctx.db, context_module);
|
||||
for (name, def) in module_scope {
|
||||
if ctx.use_item_syntax.is_some() {
|
||||
if let ScopeDef::Unknown = def {
|
||||
if let hir::ScopeDef::Unknown = def {
|
||||
if let Some(name_ref) = ctx.name_ref_syntax.as_ref() {
|
||||
if name_ref.syntax().text() == name.to_string().as_str() {
|
||||
// for `use self::foo$0`, don't suggest `foo` as a completion
|
||||
@ -57,20 +57,20 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
|
||||
}
|
||||
}
|
||||
|
||||
acc.add_resolution(ctx, name.to_string(), &def);
|
||||
acc.add_resolution(ctx, name, &def);
|
||||
}
|
||||
}
|
||||
PathResolution::Def(def @ hir::ModuleDef::Adt(_))
|
||||
| PathResolution::Def(def @ hir::ModuleDef::TypeAlias(_))
|
||||
| PathResolution::Def(def @ hir::ModuleDef::BuiltinType(_)) => {
|
||||
if let hir::ModuleDef::Adt(Adt::Enum(e)) = def {
|
||||
hir::PathResolution::Def(def @ hir::ModuleDef::Adt(_))
|
||||
| hir::PathResolution::Def(def @ hir::ModuleDef::TypeAlias(_))
|
||||
| hir::PathResolution::Def(def @ hir::ModuleDef::BuiltinType(_)) => {
|
||||
if let hir::ModuleDef::Adt(hir::Adt::Enum(e)) = def {
|
||||
add_enum_variants(ctx, acc, e);
|
||||
}
|
||||
let ty = match def {
|
||||
hir::ModuleDef::Adt(adt) => adt.ty(ctx.db),
|
||||
hir::ModuleDef::TypeAlias(a) => {
|
||||
let ty = a.ty(ctx.db);
|
||||
if let Some(Adt::Enum(e)) = ty.as_adt() {
|
||||
if let Some(hir::Adt::Enum(e)) = ty.as_adt() {
|
||||
cov_mark::hit!(completes_variant_through_alias);
|
||||
add_enum_variants(ctx, acc, e);
|
||||
}
|
||||
@ -117,7 +117,7 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
|
||||
});
|
||||
}
|
||||
}
|
||||
PathResolution::Def(hir::ModuleDef::Trait(t)) => {
|
||||
hir::PathResolution::Def(hir::ModuleDef::Trait(t)) => {
|
||||
// Handles `Trait::assoc` as well as `<Ty as Trait>::assoc`.
|
||||
for item in t.items(ctx.db) {
|
||||
if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) {
|
||||
@ -130,15 +130,15 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
|
||||
}
|
||||
}
|
||||
}
|
||||
PathResolution::TypeParam(_) | PathResolution::SelfType(_) => {
|
||||
hir::PathResolution::TypeParam(_) | hir::PathResolution::SelfType(_) => {
|
||||
if let Some(krate) = ctx.krate {
|
||||
let ty = match resolution {
|
||||
PathResolution::TypeParam(param) => param.ty(ctx.db),
|
||||
PathResolution::SelfType(impl_def) => impl_def.self_ty(ctx.db),
|
||||
hir::PathResolution::TypeParam(param) => param.ty(ctx.db),
|
||||
hir::PathResolution::SelfType(impl_def) => impl_def.self_ty(ctx.db),
|
||||
_ => return,
|
||||
};
|
||||
|
||||
if let Some(Adt::Enum(e)) = ty.as_adt() {
|
||||
if let Some(hir::Adt::Enum(e)) = ty.as_adt() {
|
||||
add_enum_variants(ctx, acc, e);
|
||||
}
|
||||
|
||||
|
@ -14,10 +14,10 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
|
||||
if ctx.expects_assoc_item() {
|
||||
ctx.scope.process_all_names(&mut |name, def| {
|
||||
if let ScopeDef::MacroDef(macro_def) = def {
|
||||
acc.add_macro(ctx, Some(name.to_string()), macro_def);
|
||||
acc.add_macro(ctx, Some(name.clone()), macro_def);
|
||||
}
|
||||
if let ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = def {
|
||||
acc.add_resolution(ctx, name.to_string(), &def);
|
||||
acc.add_resolution(ctx, name, &def);
|
||||
}
|
||||
});
|
||||
return;
|
||||
@ -27,7 +27,7 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
|
||||
cov_mark::hit!(only_completes_modules_in_import);
|
||||
ctx.scope.process_all_names(&mut |name, res| {
|
||||
if let ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = res {
|
||||
acc.add_resolution(ctx, name.to_string(), &res);
|
||||
acc.add_resolution(ctx, name, &res);
|
||||
}
|
||||
});
|
||||
return;
|
||||
@ -45,7 +45,7 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
|
||||
cov_mark::hit!(skip_lifetime_completion);
|
||||
return;
|
||||
}
|
||||
acc.add_resolution(ctx, name.to_string(), &res);
|
||||
acc.add_resolution(ctx, name, &res);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -10,9 +10,7 @@ pub(crate) mod type_alias;
|
||||
|
||||
mod builder_ext;
|
||||
|
||||
use hir::{
|
||||
AsAssocItem, Documentation, HasAttrs, HirDisplay, ModuleDef, Mutability, ScopeDef, Type,
|
||||
};
|
||||
use hir::{AsAssocItem, HasAttrs, HirDisplay};
|
||||
use ide_db::{
|
||||
helpers::{item_name, SnippetCap},
|
||||
RootDatabase, SymbolKind,
|
||||
@ -21,31 +19,30 @@ use syntax::TextRange;
|
||||
|
||||
use crate::{
|
||||
item::{CompletionRelevanceTypeMatch, ImportEdit},
|
||||
render::{enum_variant::render_variant, function::render_fn, macro_::render_macro},
|
||||
CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, CompletionRelevance,
|
||||
};
|
||||
|
||||
use crate::render::{enum_variant::render_variant, function::render_fn, macro_::render_macro};
|
||||
|
||||
pub(crate) fn render_field<'a>(
|
||||
ctx: RenderContext<'a>,
|
||||
field: hir::Field,
|
||||
ty: &Type,
|
||||
ty: &hir::Type,
|
||||
) -> CompletionItem {
|
||||
Render::new(ctx).add_field(field, ty)
|
||||
Render::new(ctx).render_field(field, ty)
|
||||
}
|
||||
|
||||
pub(crate) fn render_tuple_field<'a>(
|
||||
ctx: RenderContext<'a>,
|
||||
field: usize,
|
||||
ty: &Type,
|
||||
ty: &hir::Type,
|
||||
) -> CompletionItem {
|
||||
Render::new(ctx).add_tuple_field(field, ty)
|
||||
Render::new(ctx).render_tuple_field(field, ty)
|
||||
}
|
||||
|
||||
pub(crate) fn render_resolution<'a>(
|
||||
ctx: RenderContext<'a>,
|
||||
local_name: String,
|
||||
resolution: &ScopeDef,
|
||||
local_name: hir::Name,
|
||||
resolution: &hir::ScopeDef,
|
||||
) -> Option<CompletionItem> {
|
||||
Render::new(ctx).render_resolution(local_name, None, resolution)
|
||||
}
|
||||
@ -54,12 +51,12 @@ pub(crate) fn render_resolution_with_import<'a>(
|
||||
ctx: RenderContext<'a>,
|
||||
import_edit: ImportEdit,
|
||||
) -> Option<CompletionItem> {
|
||||
let resolution = ScopeDef::from(import_edit.import.original_item);
|
||||
let resolution = hir::ScopeDef::from(import_edit.import.original_item);
|
||||
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(),
|
||||
_ => item_name(ctx.db(), import_edit.import.original_item)?.to_string(),
|
||||
hir::ScopeDef::ModuleDef(hir::ModuleDef::Function(f)) => f.name(ctx.completion.db),
|
||||
hir::ScopeDef::ModuleDef(hir::ModuleDef::Const(c)) => c.name(ctx.completion.db)?,
|
||||
hir::ScopeDef::ModuleDef(hir::ModuleDef::TypeAlias(t)) => t.name(ctx.completion.db),
|
||||
_ => item_name(ctx.db(), import_edit.import.original_item)?,
|
||||
};
|
||||
Render::new(ctx).render_resolution(local_name, Some(import_edit), &resolution).map(
|
||||
|mut item| {
|
||||
@ -113,7 +110,7 @@ impl<'a> RenderContext<'a> {
|
||||
|| assoc.containing_trait(db).map(|trait_| self.is_deprecated(trait_)).unwrap_or(false)
|
||||
}
|
||||
|
||||
fn docs(&self, node: impl HasAttrs) -> Option<Documentation> {
|
||||
fn docs(&self, node: impl HasAttrs) -> Option<hir::Documentation> {
|
||||
node.docs(self.db())
|
||||
}
|
||||
}
|
||||
@ -129,14 +126,11 @@ impl<'a> Render<'a> {
|
||||
Render { ctx }
|
||||
}
|
||||
|
||||
fn add_field(&mut self, field: hir::Field, ty: &Type) -> CompletionItem {
|
||||
fn render_field(&self, field: hir::Field, ty: &hir::Type) -> CompletionItem {
|
||||
let is_deprecated = self.ctx.is_deprecated(field);
|
||||
let name = field.name(self.ctx.db());
|
||||
let mut item = CompletionItem::new(
|
||||
CompletionKind::Reference,
|
||||
self.ctx.source_range(),
|
||||
name.to_string(),
|
||||
);
|
||||
let name = field.name(self.ctx.db()).to_string();
|
||||
let mut item =
|
||||
CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), name.clone());
|
||||
item.kind(SymbolKind::Field)
|
||||
.detail(ty.display(self.ctx.db()).to_string())
|
||||
.set_documentation(field.docs(self.ctx.db()))
|
||||
@ -144,7 +138,7 @@ impl<'a> Render<'a> {
|
||||
|
||||
item.set_relevance(CompletionRelevance {
|
||||
type_match: compute_type_match(self.ctx.completion, ty),
|
||||
exact_name_match: compute_exact_name_match(self.ctx.completion, name.to_string()),
|
||||
exact_name_match: compute_exact_name_match(self.ctx.completion, &name),
|
||||
..CompletionRelevance::default()
|
||||
});
|
||||
|
||||
@ -157,7 +151,7 @@ impl<'a> Render<'a> {
|
||||
item.build()
|
||||
}
|
||||
|
||||
fn add_tuple_field(&mut self, field: usize, ty: &Type) -> CompletionItem {
|
||||
fn render_tuple_field(&self, field: usize, ty: &hir::Type) -> CompletionItem {
|
||||
let mut item = CompletionItem::new(
|
||||
CompletionKind::Reference,
|
||||
self.ctx.source_range(),
|
||||
@ -171,71 +165,82 @@ impl<'a> Render<'a> {
|
||||
|
||||
fn render_resolution(
|
||||
self,
|
||||
local_name: String,
|
||||
local_name: hir::Name,
|
||||
import_to_add: Option<ImportEdit>,
|
||||
resolution: &ScopeDef,
|
||||
resolution: &hir::ScopeDef,
|
||||
) -> Option<CompletionItem> {
|
||||
let _p = profile::span("render_resolution");
|
||||
use hir::ModuleDef::*;
|
||||
|
||||
let completion_kind = match resolution {
|
||||
ScopeDef::ModuleDef(BuiltinType(..)) => CompletionKind::BuiltinType,
|
||||
hir::ScopeDef::ModuleDef(BuiltinType(..)) => CompletionKind::BuiltinType,
|
||||
_ => CompletionKind::Reference,
|
||||
};
|
||||
|
||||
let kind = match resolution {
|
||||
ScopeDef::ModuleDef(Function(func)) => {
|
||||
hir::ScopeDef::ModuleDef(Function(func)) => {
|
||||
return render_fn(self.ctx, import_to_add, Some(local_name), *func);
|
||||
}
|
||||
ScopeDef::ModuleDef(Variant(_)) if self.ctx.completion.is_pat_or_const.is_some() => {
|
||||
hir::ScopeDef::ModuleDef(Variant(_))
|
||||
if self.ctx.completion.is_pat_or_const.is_some() =>
|
||||
{
|
||||
CompletionItemKind::SymbolKind(SymbolKind::Variant)
|
||||
}
|
||||
ScopeDef::ModuleDef(Variant(var)) => {
|
||||
hir::ScopeDef::ModuleDef(Variant(var)) => {
|
||||
let item = render_variant(self.ctx, import_to_add, Some(local_name), *var, None);
|
||||
return Some(item);
|
||||
}
|
||||
ScopeDef::MacroDef(mac) => {
|
||||
hir::ScopeDef::MacroDef(mac) => {
|
||||
let item = render_macro(self.ctx, import_to_add, local_name, *mac);
|
||||
return item;
|
||||
}
|
||||
|
||||
ScopeDef::ModuleDef(Module(..)) => CompletionItemKind::SymbolKind(SymbolKind::Module),
|
||||
ScopeDef::ModuleDef(Adt(adt)) => CompletionItemKind::SymbolKind(match adt {
|
||||
hir::ScopeDef::ModuleDef(Module(..)) => {
|
||||
CompletionItemKind::SymbolKind(SymbolKind::Module)
|
||||
}
|
||||
hir::ScopeDef::ModuleDef(Adt(adt)) => CompletionItemKind::SymbolKind(match adt {
|
||||
hir::Adt::Struct(_) => SymbolKind::Struct,
|
||||
hir::Adt::Union(_) => SymbolKind::Union,
|
||||
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(..)) => {
|
||||
hir::ScopeDef::ModuleDef(Const(..)) => {
|
||||
CompletionItemKind::SymbolKind(SymbolKind::Const)
|
||||
}
|
||||
hir::ScopeDef::ModuleDef(Static(..)) => {
|
||||
CompletionItemKind::SymbolKind(SymbolKind::Static)
|
||||
}
|
||||
hir::ScopeDef::ModuleDef(Trait(..)) => {
|
||||
CompletionItemKind::SymbolKind(SymbolKind::Trait)
|
||||
}
|
||||
hir::ScopeDef::ModuleDef(TypeAlias(..)) => {
|
||||
CompletionItemKind::SymbolKind(SymbolKind::TypeAlias)
|
||||
}
|
||||
ScopeDef::ModuleDef(BuiltinType(..)) => CompletionItemKind::BuiltinType,
|
||||
ScopeDef::GenericParam(param) => CompletionItemKind::SymbolKind(match param {
|
||||
hir::ScopeDef::ModuleDef(BuiltinType(..)) => CompletionItemKind::BuiltinType,
|
||||
hir::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::Label(..) => CompletionItemKind::SymbolKind(SymbolKind::Label),
|
||||
ScopeDef::AdtSelfType(..) | ScopeDef::ImplSelfType(..) => {
|
||||
hir::ScopeDef::Local(..) => CompletionItemKind::SymbolKind(SymbolKind::Local),
|
||||
hir::ScopeDef::Label(..) => CompletionItemKind::SymbolKind(SymbolKind::Label),
|
||||
hir::ScopeDef::AdtSelfType(..) | hir::ScopeDef::ImplSelfType(..) => {
|
||||
CompletionItemKind::SymbolKind(SymbolKind::SelfParam)
|
||||
}
|
||||
ScopeDef::Unknown => {
|
||||
hir::ScopeDef::Unknown => {
|
||||
let mut item = CompletionItem::new(
|
||||
CompletionKind::Reference,
|
||||
self.ctx.source_range(),
|
||||
local_name,
|
||||
local_name.to_string(),
|
||||
);
|
||||
item.kind(CompletionItemKind::UnresolvedReference).add_import(import_to_add);
|
||||
return Some(item.build());
|
||||
}
|
||||
};
|
||||
|
||||
let local_name = local_name.to_string();
|
||||
let mut item =
|
||||
CompletionItem::new(completion_kind, self.ctx.source_range(), local_name.clone());
|
||||
if let ScopeDef::Local(local) = resolution {
|
||||
if let hir::ScopeDef::Local(local) = resolution {
|
||||
let ty = local.ty(self.ctx.db());
|
||||
if !ty.is_unknown() {
|
||||
item.detail(ty.display(self.ctx.db()).to_string());
|
||||
@ -260,8 +265,10 @@ impl<'a> Render<'a> {
|
||||
{
|
||||
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)) => {
|
||||
hir::ScopeDef::ModuleDef(Adt(it)) => {
|
||||
it.has_non_default_type_params(self.ctx.db())
|
||||
}
|
||||
hir::ScopeDef::ModuleDef(TypeAlias(it)) => {
|
||||
it.has_non_default_type_params(self.ctx.db())
|
||||
}
|
||||
_ => false,
|
||||
@ -281,26 +288,26 @@ impl<'a> Render<'a> {
|
||||
Some(item.build())
|
||||
}
|
||||
|
||||
fn docs(&self, resolution: &ScopeDef) -> Option<Documentation> {
|
||||
fn docs(&self, resolution: &hir::ScopeDef) -> Option<hir::Documentation> {
|
||||
use hir::ModuleDef::*;
|
||||
match resolution {
|
||||
ScopeDef::ModuleDef(Module(it)) => it.docs(self.ctx.db()),
|
||||
ScopeDef::ModuleDef(Adt(it)) => it.docs(self.ctx.db()),
|
||||
ScopeDef::ModuleDef(Variant(it)) => it.docs(self.ctx.db()),
|
||||
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()),
|
||||
hir::ScopeDef::ModuleDef(Module(it)) => it.docs(self.ctx.db()),
|
||||
hir::ScopeDef::ModuleDef(Adt(it)) => it.docs(self.ctx.db()),
|
||||
hir::ScopeDef::ModuleDef(Variant(it)) => it.docs(self.ctx.db()),
|
||||
hir::ScopeDef::ModuleDef(Const(it)) => it.docs(self.ctx.db()),
|
||||
hir::ScopeDef::ModuleDef(Static(it)) => it.docs(self.ctx.db()),
|
||||
hir::ScopeDef::ModuleDef(Trait(it)) => it.docs(self.ctx.db()),
|
||||
hir::ScopeDef::ModuleDef(TypeAlias(it)) => it.docs(self.ctx.db()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_deprecated(&self, resolution: &ScopeDef) -> bool {
|
||||
fn is_deprecated(&self, resolution: &hir::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),
|
||||
hir::ScopeDef::ModuleDef(it) => self.ctx.is_deprecated_assoc_item(*it),
|
||||
hir::ScopeDef::MacroDef(it) => self.ctx.is_deprecated(*it),
|
||||
hir::ScopeDef::GenericParam(it) => self.ctx.is_deprecated(*it),
|
||||
hir::ScopeDef::AdtSelfType(it) => self.ctx.is_deprecated(*it),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
@ -327,21 +334,23 @@ fn compute_type_match(
|
||||
}
|
||||
}
|
||||
|
||||
fn compute_exact_name_match(ctx: &CompletionContext, completion_name: impl Into<String>) -> bool {
|
||||
let completion_name = completion_name.into();
|
||||
fn compute_exact_name_match(ctx: &CompletionContext, completion_name: &str) -> bool {
|
||||
ctx.expected_name.as_ref().map_or(false, |name| name.text() == completion_name)
|
||||
}
|
||||
|
||||
fn compute_ref_match(ctx: &CompletionContext, completion_ty: &hir::Type) -> Option<Mutability> {
|
||||
fn compute_ref_match(
|
||||
ctx: &CompletionContext,
|
||||
completion_ty: &hir::Type,
|
||||
) -> Option<hir::Mutability> {
|
||||
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() {
|
||||
Mutability::Mut
|
||||
hir::Mutability::Mut
|
||||
} else {
|
||||
Mutability::Shared
|
||||
hir::Mutability::Shared
|
||||
};
|
||||
return Some(mutability);
|
||||
};
|
||||
|
@ -1,6 +1,8 @@
|
||||
//! Renderer for `enum` variants.
|
||||
|
||||
use hir::{HasAttrs, HirDisplay, ModPath, StructKind};
|
||||
use std::iter;
|
||||
|
||||
use hir::{HasAttrs, HirDisplay};
|
||||
use ide_db::SymbolKind;
|
||||
use itertools::Itertools;
|
||||
|
||||
@ -13,9 +15,9 @@ use crate::{
|
||||
pub(crate) fn render_variant<'a>(
|
||||
ctx: RenderContext<'a>,
|
||||
import_to_add: Option<ImportEdit>,
|
||||
local_name: Option<String>,
|
||||
local_name: Option<hir::Name>,
|
||||
variant: hir::Variant,
|
||||
path: Option<ModPath>,
|
||||
path: Option<hir::ModPath>,
|
||||
) -> CompletionItem {
|
||||
let _p = profile::span("render_enum_variant");
|
||||
EnumRender::new(ctx, local_name, variant, path).render(import_to_add)
|
||||
@ -24,42 +26,45 @@ pub(crate) fn render_variant<'a>(
|
||||
#[derive(Debug)]
|
||||
struct EnumRender<'a> {
|
||||
ctx: RenderContext<'a>,
|
||||
name: String,
|
||||
name: hir::Name,
|
||||
variant: hir::Variant,
|
||||
path: Option<ModPath>,
|
||||
qualified_name: String,
|
||||
short_qualified_name: String,
|
||||
variant_kind: StructKind,
|
||||
path: Option<hir::ModPath>,
|
||||
qualified_name: hir::ModPath,
|
||||
short_qualified_name: hir::ModPath,
|
||||
variant_kind: hir::StructKind,
|
||||
}
|
||||
|
||||
impl<'a> EnumRender<'a> {
|
||||
fn new(
|
||||
ctx: RenderContext<'a>,
|
||||
local_name: Option<String>,
|
||||
local_name: Option<hir::Name>,
|
||||
variant: hir::Variant,
|
||||
path: Option<ModPath>,
|
||||
path: Option<hir::ModPath>,
|
||||
) -> EnumRender<'a> {
|
||||
let name = local_name.unwrap_or_else(|| variant.name(ctx.db()).to_string());
|
||||
let name = local_name.unwrap_or_else(|| variant.name(ctx.db()));
|
||||
let variant_kind = variant.kind(ctx.db());
|
||||
|
||||
let (qualified_name, short_qualified_name) = match &path {
|
||||
Some(path) => {
|
||||
let full = path.to_string();
|
||||
let segments = path.segments();
|
||||
let short = segments[segments.len().saturating_sub(2)..].iter().join("::");
|
||||
(full, short)
|
||||
let short = hir::ModPath::from_segments(
|
||||
hir::PathKind::Plain,
|
||||
path.segments().iter().skip(path.segments().len().saturating_sub(2)).cloned(),
|
||||
);
|
||||
(path.clone(), short)
|
||||
}
|
||||
None => (name.to_string(), name.to_string()),
|
||||
None => (
|
||||
hir::ModPath::from_segments(hir::PathKind::Plain, iter::once(name.clone())),
|
||||
hir::ModPath::from_segments(hir::PathKind::Plain, iter::once(name.clone())),
|
||||
),
|
||||
};
|
||||
|
||||
EnumRender { ctx, name, variant, path, qualified_name, short_qualified_name, variant_kind }
|
||||
}
|
||||
|
||||
fn render(self, import_to_add: Option<ImportEdit>) -> CompletionItem {
|
||||
let mut item = CompletionItem::new(
|
||||
CompletionKind::Reference,
|
||||
self.ctx.source_range(),
|
||||
self.qualified_name.clone(),
|
||||
self.qualified_name.to_string(),
|
||||
);
|
||||
item.kind(SymbolKind::Variant)
|
||||
.set_documentation(self.variant.docs(self.ctx.db()))
|
||||
@ -67,12 +72,16 @@ impl<'a> EnumRender<'a> {
|
||||
.add_import(import_to_add)
|
||||
.detail(self.detail());
|
||||
|
||||
if self.variant_kind == StructKind::Tuple {
|
||||
if self.variant_kind == hir::StructKind::Tuple {
|
||||
cov_mark::hit!(inserts_parens_for_tuple_enums);
|
||||
let params = Params::Anonymous(self.variant.fields(self.ctx.db()).len());
|
||||
item.add_call_parens(self.ctx.completion, self.short_qualified_name, params);
|
||||
item.add_call_parens(
|
||||
self.ctx.completion,
|
||||
self.short_qualified_name.to_string(),
|
||||
params,
|
||||
);
|
||||
} else if self.path.is_some() {
|
||||
item.lookup_by(self.short_qualified_name);
|
||||
item.lookup_by(self.short_qualified_name.to_string());
|
||||
}
|
||||
|
||||
let ty = self.variant.parent_enum(self.ctx.completion.db).ty(self.ctx.completion.db);
|
||||
@ -96,11 +105,11 @@ impl<'a> EnumRender<'a> {
|
||||
.map(|field| (field.name(self.ctx.db()), field.ty(self.ctx.db())));
|
||||
|
||||
match self.variant_kind {
|
||||
StructKind::Tuple | StructKind::Unit => format!(
|
||||
hir::StructKind::Tuple | hir::StructKind::Unit => format!(
|
||||
"({})",
|
||||
detail_types.map(|(_, t)| t.display(self.ctx.db()).to_string()).format(", ")
|
||||
),
|
||||
StructKind::Record => format!(
|
||||
hir::StructKind::Record => format!(
|
||||
"{{ {} }}",
|
||||
detail_types
|
||||
.map(|(n, t)| format!("{}: {}", n, t.display(self.ctx.db()).to_string()))
|
||||
|
@ -1,6 +1,6 @@
|
||||
//! Renderer for function calls.
|
||||
|
||||
use hir::{HasSource, HirDisplay, Type};
|
||||
use hir::{HasSource, HirDisplay};
|
||||
use ide_db::SymbolKind;
|
||||
use itertools::Itertools;
|
||||
use syntax::ast::Fn;
|
||||
@ -16,7 +16,7 @@ use crate::{
|
||||
pub(crate) fn render_fn<'a>(
|
||||
ctx: RenderContext<'a>,
|
||||
import_to_add: Option<ImportEdit>,
|
||||
local_name: Option<String>,
|
||||
local_name: Option<hir::Name>,
|
||||
fn_: hir::Function,
|
||||
) -> Option<CompletionItem> {
|
||||
let _p = profile::span("render_fn");
|
||||
@ -26,7 +26,7 @@ pub(crate) fn render_fn<'a>(
|
||||
pub(crate) fn render_method<'a>(
|
||||
ctx: RenderContext<'a>,
|
||||
import_to_add: Option<ImportEdit>,
|
||||
local_name: Option<String>,
|
||||
local_name: Option<hir::Name>,
|
||||
fn_: hir::Function,
|
||||
) -> Option<CompletionItem> {
|
||||
let _p = profile::span("render_method");
|
||||
@ -45,11 +45,11 @@ struct FunctionRender<'a> {
|
||||
impl<'a> FunctionRender<'a> {
|
||||
fn new(
|
||||
ctx: RenderContext<'a>,
|
||||
local_name: Option<String>,
|
||||
local_name: Option<hir::Name>,
|
||||
fn_: hir::Function,
|
||||
is_method: bool,
|
||||
) -> Option<FunctionRender<'a>> {
|
||||
let name = local_name.unwrap_or_else(|| fn_.name(ctx.db()).to_string());
|
||||
let name = local_name.unwrap_or_else(|| fn_.name(ctx.db())).to_string();
|
||||
let ast_node = fn_.source(ctx.db())?.value;
|
||||
|
||||
Some(FunctionRender { ctx, name, func: fn_, ast_node, is_method })
|
||||
@ -74,7 +74,7 @@ impl<'a> FunctionRender<'a> {
|
||||
let ret_type = self.func.ret_type(self.ctx.db());
|
||||
item.set_relevance(CompletionRelevance {
|
||||
type_match: compute_type_match(self.ctx.completion, &ret_type),
|
||||
exact_name_match: compute_exact_name_match(self.ctx.completion, self.name.clone()),
|
||||
exact_name_match: compute_exact_name_match(self.ctx.completion, &self.name),
|
||||
..CompletionRelevance::default()
|
||||
});
|
||||
|
||||
@ -129,7 +129,7 @@ impl<'a> FunctionRender<'a> {
|
||||
format!("-> {}", ret_ty.display(self.ctx.db()))
|
||||
}
|
||||
|
||||
fn add_arg(&self, arg: &str, ty: &Type) -> String {
|
||||
fn add_arg(&self, arg: &str, ty: &hir::Type) -> String {
|
||||
if let Some(derefed_ty) = ty.remove_ref() {
|
||||
for (name, local) in self.ctx.completion.locals.iter() {
|
||||
if name == arg && local.ty(self.ctx.db()) == derefed_ty {
|
||||
|
@ -1,6 +1,6 @@
|
||||
//! Renderer for macro invocations.
|
||||
|
||||
use hir::{Documentation, HasSource};
|
||||
use hir::HasSource;
|
||||
use ide_db::SymbolKind;
|
||||
use syntax::display::macro_label;
|
||||
|
||||
@ -12,7 +12,7 @@ use crate::{
|
||||
pub(crate) fn render_macro<'a>(
|
||||
ctx: RenderContext<'a>,
|
||||
import_to_add: Option<ImportEdit>,
|
||||
name: String,
|
||||
name: hir::Name,
|
||||
macro_: hir::MacroDef,
|
||||
) -> Option<CompletionItem> {
|
||||
let _p = profile::span("render_macro");
|
||||
@ -24,13 +24,14 @@ struct MacroRender<'a> {
|
||||
ctx: RenderContext<'a>,
|
||||
name: String,
|
||||
macro_: hir::MacroDef,
|
||||
docs: Option<Documentation>,
|
||||
docs: Option<hir::Documentation>,
|
||||
bra: &'static str,
|
||||
ket: &'static str,
|
||||
}
|
||||
|
||||
impl<'a> MacroRender<'a> {
|
||||
fn new(ctx: RenderContext<'a>, name: String, macro_: hir::MacroDef) -> MacroRender<'a> {
|
||||
fn new(ctx: RenderContext<'a>, name: hir::Name, macro_: hir::MacroDef) -> MacroRender<'a> {
|
||||
let name = name.to_string();
|
||||
let docs = ctx.docs(macro_);
|
||||
let docs_str = docs.as_ref().map_or("", |s| s.as_str());
|
||||
let (bra, ket) = guess_macro_braces(&name, docs_str);
|
||||
|
Loading…
x
Reference in New Issue
Block a user