Partially unify SymbolKind and CompletionItemKind

This commit is contained in:
Lukas Wirth 2021-01-20 18:38:12 +01:00
parent 563a175fdb
commit f2cb7dbcb7
13 changed files with 128 additions and 110 deletions

View File

@ -6,7 +6,7 @@
match_ast, AstNode,
};
use crate::{CompletionContext, CompletionItem, CompletionKind, Completions};
use crate::{CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions};
/// Complete repeated parameters, both name and type. For example, if all
/// functions in a file have a `spam: &mut Spam` parameter, a completion with
@ -58,7 +58,7 @@ pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
})
.for_each(|(label, lookup)| {
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label)
.kind(crate::CompletionItemKind::Binding)
.kind(CompletionItemKind::Binding)
.lookup_by(lookup)
.add_to(acc)
});

View File

@ -3,11 +3,13 @@
use std::iter;
use hir::{Module, ModuleSource};
use ide_db::base_db::{SourceDatabaseExt, VfsPath};
use ide_db::RootDatabase;
use ide_db::{
base_db::{SourceDatabaseExt, VfsPath},
RootDatabase, SymbolKind,
};
use rustc_hash::FxHashSet;
use crate::{CompletionItem, CompletionItemKind};
use crate::CompletionItem;
use crate::{context::CompletionContext, item::CompletionKind, Completions};
@ -79,7 +81,7 @@ pub(crate) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Op
label.push(';');
}
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), &label)
.kind(CompletionItemKind::Module)
.kind(SymbolKind::Module)
.add_to(acc)
});

View File

@ -1,10 +1,8 @@
//! Complete fields in record literals and patterns.
use ide_db::helpers::FamousDefs;
use ide_db::{helpers::FamousDefs, SymbolKind};
use syntax::ast::Expr;
use crate::{
item::CompletionKind, CompletionContext, CompletionItem, CompletionItemKind, Completions,
};
use crate::{item::CompletionKind, CompletionContext, CompletionItem, Completions};
pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
let missing_fields = match (ctx.record_pat_syntax.as_ref(), ctx.record_lit_syntax.as_ref()) {
@ -31,7 +29,7 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) ->
"..Default::default()",
)
.insert_text(completion_text)
.kind(CompletionItemKind::Field)
.kind(SymbolKind::Field)
.build(),
);
}

View File

@ -32,7 +32,7 @@
//! ```
use hir::{self, HasAttrs, HasSource};
use ide_db::traits::get_missing_assoc_items;
use ide_db::{traits::get_missing_assoc_items, SymbolKind};
use syntax::{
ast::{self, edit, Impl},
display::function_declaration,
@ -152,7 +152,7 @@ fn add_function_impl(
let completion_kind = if func.self_param(ctx.db).is_some() {
CompletionItemKind::Method
} else {
CompletionItemKind::Function
CompletionItemKind::SymbolKind(SymbolKind::Function)
};
let range = TextRange::new(fn_def_node.text_range().start(), ctx.source_range().end());
@ -188,7 +188,7 @@ fn add_type_alias_impl(
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone())
.text_edit(TextEdit::replace(range, snippet))
.lookup_by(alias_name)
.kind(CompletionItemKind::TypeAlias)
.kind(SymbolKind::TypeAlias)
.set_documentation(type_alias.docs(ctx.db))
.add_to(acc);
}
@ -211,7 +211,7 @@ fn add_const_impl(
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone())
.text_edit(TextEdit::replace(range, snippet))
.lookup_by(const_name)
.kind(CompletionItemKind::Const)
.kind(SymbolKind::Const)
.set_documentation(const_.docs(ctx.db))
.add_to(acc);
}

View File

@ -3,11 +3,14 @@
use std::fmt;
use hir::{Documentation, ModPath, Mutability};
use ide_db::helpers::{
insert_use::{self, ImportScope, MergeBehavior},
mod_path_to_ast, SnippetCap,
use ide_db::{
helpers::{
insert_use::{self, ImportScope, MergeBehavior},
mod_path_to_ast, SnippetCap,
},
SymbolKind,
};
use stdx::assert_never;
use stdx::{assert_never, impl_from};
use syntax::{algo, TextRange};
use text_edit::TextEdit;
@ -117,53 +120,50 @@ pub enum CompletionScore {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompletionItemKind {
SymbolKind(SymbolKind),
BuiltinType,
Attribute,
Binding,
BuiltinType,
Const,
ConstParam,
Enum,
EnumVariant,
Field,
Function,
Keyword,
LifetimeParam,
Macro,
Method,
Module,
Snippet,
Static,
Struct,
Trait,
TypeAlias,
TypeParam,
UnresolvedReference,
}
impl_from!(SymbolKind for CompletionItemKind);
impl CompletionItemKind {
#[cfg(test)]
pub(crate) fn tag(&self) -> &'static str {
match self {
CompletionItemKind::SymbolKind(kind) => match kind {
SymbolKind::Const => "ct",
SymbolKind::ConstParam => "cp",
SymbolKind::Enum => "en",
SymbolKind::Field => "fd",
SymbolKind::Function => "fn",
SymbolKind::Impl => "im",
SymbolKind::Label => "lb",
SymbolKind::LifetimeParam => "lt",
SymbolKind::Local => "lc",
SymbolKind::Macro => "ma",
SymbolKind::Module => "md",
SymbolKind::SelfParam => "sp",
SymbolKind::Static => "sc",
SymbolKind::Struct => "st",
SymbolKind::Trait => "tt",
SymbolKind::TypeAlias => "ta",
SymbolKind::TypeParam => "tp",
SymbolKind::Union => "un",
SymbolKind::ValueParam => "vp",
SymbolKind::Variant => "ev",
},
CompletionItemKind::Attribute => "at",
CompletionItemKind::Binding => "bn",
CompletionItemKind::BuiltinType => "bt",
CompletionItemKind::Const => "ct",
CompletionItemKind::ConstParam => "cp",
CompletionItemKind::Enum => "en",
CompletionItemKind::EnumVariant => "ev",
CompletionItemKind::Field => "fd",
CompletionItemKind::Function => "fn",
CompletionItemKind::Keyword => "kw",
CompletionItemKind::LifetimeParam => "lt",
CompletionItemKind::Macro => "ma",
CompletionItemKind::Method => "me",
CompletionItemKind::Module => "md",
CompletionItemKind::Snippet => "sn",
CompletionItemKind::Static => "sc",
CompletionItemKind::Struct => "st",
CompletionItemKind::Trait => "tt",
CompletionItemKind::TypeAlias => "ta",
CompletionItemKind::TypeParam => "tp",
CompletionItemKind::UnresolvedReference => "??",
}
}
@ -386,8 +386,8 @@ pub(crate) fn insert_snippet(
self.insert_text_format = InsertTextFormat::Snippet;
self.insert_text(snippet)
}
pub(crate) fn kind(mut self, kind: CompletionItemKind) -> Builder {
self.kind = Some(kind);
pub(crate) fn kind(mut self, kind: impl Into<CompletionItemKind>) -> Builder {
self.kind = Some(kind.into());
self
}
pub(crate) fn text_edit(mut self, edit: TextEdit) -> Builder {

View File

@ -13,7 +13,7 @@
use hir::{
AsAssocItem, Documentation, HasAttrs, HirDisplay, ModuleDef, Mutability, ScopeDef, Type,
};
use ide_db::{helpers::SnippetCap, RootDatabase};
use ide_db::{helpers::SnippetCap, RootDatabase, SymbolKind};
use syntax::TextRange;
use test_utils::mark;
@ -146,7 +146,7 @@ fn add_field(&mut self, field: hir::Field, ty: &Type) -> CompletionItem {
self.ctx.source_range(),
name.to_string(),
)
.kind(CompletionItemKind::Field)
.kind(SymbolKind::Field)
.detail(ty.display(self.ctx.db()).to_string())
.set_documentation(field.docs(self.ctx.db()))
.set_deprecated(is_deprecated);
@ -160,7 +160,7 @@ fn add_field(&mut self, field: hir::Field, ty: &Type) -> CompletionItem {
fn add_tuple_field(&mut self, field: usize, ty: &Type) -> CompletionItem {
CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), field.to_string())
.kind(CompletionItemKind::Field)
.kind(SymbolKind::Field)
.detail(ty.display(self.ctx.db()).to_string())
.build()
}
@ -187,7 +187,7 @@ fn render_resolution(
if self.ctx.completion.is_pat_binding_or_const
| self.ctx.completion.is_irrefutable_pat_binding =>
{
CompletionItemKind::EnumVariant
CompletionItemKind::SymbolKind(SymbolKind::Variant)
}
ScopeDef::ModuleDef(Variant(var)) => {
let item = render_variant(self.ctx, import_to_add, Some(local_name), *var, None);
@ -198,24 +198,29 @@ fn render_resolution(
return item;
}
ScopeDef::ModuleDef(Module(..)) => CompletionItemKind::Module,
ScopeDef::ModuleDef(Adt(hir::Adt::Struct(_))) => CompletionItemKind::Struct,
// FIXME: add CompletionItemKind::Union
ScopeDef::ModuleDef(Adt(hir::Adt::Union(_))) => CompletionItemKind::Struct,
ScopeDef::ModuleDef(Adt(hir::Adt::Enum(_))) => CompletionItemKind::Enum,
ScopeDef::ModuleDef(Const(..)) => CompletionItemKind::Const,
ScopeDef::ModuleDef(Static(..)) => CompletionItemKind::Static,
ScopeDef::ModuleDef(Trait(..)) => CompletionItemKind::Trait,
ScopeDef::ModuleDef(TypeAlias(..)) => CompletionItemKind::TypeAlias,
ScopeDef::ModuleDef(Module(..)) => CompletionItemKind::SymbolKind(SymbolKind::Module),
ScopeDef::ModuleDef(Adt(adt)) => CompletionItemKind::SymbolKind(match adt {
hir::Adt::Struct(_) => SymbolKind::Struct,
// FIXME: add CompletionItemKind::Union
hir::Adt::Union(_) => SymbolKind::Struct,
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)
}
ScopeDef::ModuleDef(BuiltinType(..)) => CompletionItemKind::BuiltinType,
ScopeDef::GenericParam(param) => match param {
hir::GenericParam::TypeParam(_) => CompletionItemKind::TypeParam,
hir::GenericParam::LifetimeParam(_) => CompletionItemKind::LifetimeParam,
hir::GenericParam::ConstParam(_) => CompletionItemKind::ConstParam,
},
ScopeDef::Local(..) => CompletionItemKind::Binding,
// (does this need its own kind?)
ScopeDef::AdtSelfType(..) | ScopeDef::ImplSelfType(..) => CompletionItemKind::TypeParam,
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)
}
ScopeDef::Unknown => {
let item = CompletionItem::new(
CompletionKind::Reference,

View File

@ -1,13 +1,14 @@
//! Renderer for `const` fields.
use hir::HasSource;
use ide_db::SymbolKind;
use syntax::{
ast::{Const, NameOwner},
display::const_label,
};
use crate::{
item::{CompletionItem, CompletionItemKind, CompletionKind},
item::{CompletionItem, CompletionKind},
render::RenderContext,
};
@ -36,7 +37,7 @@ fn render(self) -> Option<CompletionItem> {
let detail = self.detail();
let item = CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), name)
.kind(CompletionItemKind::Const)
.kind(SymbolKind::Const)
.set_documentation(self.ctx.docs(self.const_))
.set_deprecated(
self.ctx.is_deprecated(self.const_)

View File

@ -1,11 +1,12 @@
//! Renderer for `enum` variants.
use hir::{HasAttrs, HirDisplay, ModPath, StructKind};
use ide_db::SymbolKind;
use itertools::Itertools;
use test_utils::mark;
use crate::{
item::{CompletionItem, CompletionItemKind, CompletionKind, ImportEdit},
item::{CompletionItem, CompletionKind, ImportEdit},
render::{builder_ext::Params, RenderContext},
};
@ -60,7 +61,7 @@ fn render(self, import_to_add: Option<ImportEdit>) -> CompletionItem {
self.ctx.source_range(),
self.qualified_name.clone(),
)
.kind(CompletionItemKind::EnumVariant)
.kind(SymbolKind::Variant)
.set_documentation(self.variant.docs(self.ctx.db()))
.set_deprecated(self.ctx.is_deprecated(self.variant))
.add_import(import_to_add)

View File

@ -1,6 +1,7 @@
//! Renderer for function calls.
use hir::{HasSource, Type};
use ide_db::SymbolKind;
use syntax::{ast::Fn, display::function_declaration};
use test_utils::mark;
@ -105,7 +106,7 @@ fn kind(&self) -> CompletionItemKind {
if self.func.self_param(self.ctx.db()).is_some() {
CompletionItemKind::Method
} else {
CompletionItemKind::Function
SymbolKind::Function.into()
}
}
}

View File

@ -1,11 +1,12 @@
//! Renderer for macro invocations.
use hir::{Documentation, HasSource};
use ide_db::SymbolKind;
use syntax::display::macro_label;
use test_utils::mark;
use crate::{
item::{CompletionItem, CompletionItemKind, CompletionKind, ImportEdit},
item::{CompletionItem, CompletionKind, ImportEdit},
render::RenderContext,
};
@ -41,7 +42,7 @@ fn new(ctx: RenderContext<'a>, name: String, macro_: hir::MacroDef) -> MacroRend
fn render(&self, import_to_add: Option<ImportEdit>) -> Option<CompletionItem> {
let mut builder =
CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), &self.label())
.kind(CompletionItemKind::Macro)
.kind(SymbolKind::Macro)
.set_documentation(self.docs.clone())
.set_deprecated(self.ctx.is_deprecated(self.macro_))
.add_import(import_to_add)

View File

@ -1,13 +1,14 @@
//! Renderer for type aliases.
use hir::HasSource;
use ide_db::SymbolKind;
use syntax::{
ast::{NameOwner, TypeAlias},
display::type_label,
};
use crate::{
item::{CompletionItem, CompletionItemKind, CompletionKind},
item::{CompletionItem, CompletionKind},
render::RenderContext,
};
@ -36,7 +37,7 @@ fn render(self) -> Option<CompletionItem> {
let detail = self.detail();
let item = CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), name)
.kind(CompletionItemKind::TypeAlias)
.kind(SymbolKind::TypeAlias)
.set_documentation(self.ctx.docs(self.type_alias))
.set_deprecated(
self.ctx.is_deprecated(self.type_alias)

View File

@ -137,24 +137,24 @@ fn line_index(db: &dyn LineIndexDatabase, file_id: FileId) -> Arc<LineIndex> {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum SymbolKind {
Module,
Impl,
Field,
TypeParam,
ConstParam,
LifetimeParam,
ValueParam,
SelfParam,
Local,
Label,
Function,
Const,
ConstParam,
Enum,
Field,
Function,
Impl,
Label,
LifetimeParam,
Local,
Macro,
Module,
SelfParam,
Static,
Struct,
Enum,
Variant,
Union,
TypeAlias,
Trait,
Macro,
TypeAlias,
TypeParam,
Union,
ValueParam,
Variant,
}

View File

@ -91,24 +91,32 @@ pub(crate) fn completion_item_kind(
CompletionItemKind::Attribute => lsp_types::CompletionItemKind::EnumMember,
CompletionItemKind::Binding => lsp_types::CompletionItemKind::Variable,
CompletionItemKind::BuiltinType => lsp_types::CompletionItemKind::Struct,
CompletionItemKind::Const => lsp_types::CompletionItemKind::Constant,
CompletionItemKind::ConstParam => lsp_types::CompletionItemKind::TypeParameter,
CompletionItemKind::Enum => lsp_types::CompletionItemKind::Enum,
CompletionItemKind::EnumVariant => lsp_types::CompletionItemKind::EnumMember,
CompletionItemKind::Field => lsp_types::CompletionItemKind::Field,
CompletionItemKind::Function => lsp_types::CompletionItemKind::Function,
CompletionItemKind::Keyword => lsp_types::CompletionItemKind::Keyword,
CompletionItemKind::LifetimeParam => lsp_types::CompletionItemKind::TypeParameter,
CompletionItemKind::Macro => lsp_types::CompletionItemKind::Method,
CompletionItemKind::Method => lsp_types::CompletionItemKind::Method,
CompletionItemKind::Module => lsp_types::CompletionItemKind::Module,
CompletionItemKind::Snippet => lsp_types::CompletionItemKind::Snippet,
CompletionItemKind::Static => lsp_types::CompletionItemKind::Value,
CompletionItemKind::Struct => lsp_types::CompletionItemKind::Struct,
CompletionItemKind::Trait => lsp_types::CompletionItemKind::Interface,
CompletionItemKind::TypeAlias => lsp_types::CompletionItemKind::Struct,
CompletionItemKind::TypeParam => lsp_types::CompletionItemKind::TypeParameter,
CompletionItemKind::UnresolvedReference => lsp_types::CompletionItemKind::Reference,
CompletionItemKind::SymbolKind(symbol) => match symbol {
SymbolKind::Const => lsp_types::CompletionItemKind::Constant,
SymbolKind::ConstParam => lsp_types::CompletionItemKind::TypeParameter,
SymbolKind::Enum => lsp_types::CompletionItemKind::Enum,
SymbolKind::Field => lsp_types::CompletionItemKind::Field,
SymbolKind::Function => lsp_types::CompletionItemKind::Function,
SymbolKind::Impl => lsp_types::CompletionItemKind::Text,
SymbolKind::Label => lsp_types::CompletionItemKind::Variable,
SymbolKind::LifetimeParam => lsp_types::CompletionItemKind::TypeParameter,
SymbolKind::Local => lsp_types::CompletionItemKind::Variable,
SymbolKind::Macro => lsp_types::CompletionItemKind::Method,
SymbolKind::Module => lsp_types::CompletionItemKind::Module,
SymbolKind::SelfParam => lsp_types::CompletionItemKind::Value,
SymbolKind::Static => lsp_types::CompletionItemKind::Value,
SymbolKind::Struct => lsp_types::CompletionItemKind::Struct,
SymbolKind::Trait => lsp_types::CompletionItemKind::Interface,
SymbolKind::TypeAlias => lsp_types::CompletionItemKind::Struct,
SymbolKind::TypeParam => lsp_types::CompletionItemKind::TypeParameter,
SymbolKind::Union => lsp_types::CompletionItemKind::Struct,
SymbolKind::ValueParam => lsp_types::CompletionItemKind::Value,
SymbolKind::Variant => lsp_types::CompletionItemKind::EnumMember,
},
}
}