From f2cb7dbcb71d81336c95dc7ae1301ba2a79ef707 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 20 Jan 2021 18:38:12 +0100 Subject: Partially unify SymbolKind and CompletionItemKind --- crates/completion/src/completions/fn_param.rs | 4 +- crates/completion/src/completions/mod_.rs | 10 ++-- crates/completion/src/completions/record.rs | 8 ++- crates/completion/src/completions/trait_impl.rs | 8 +-- crates/completion/src/item.rs | 70 ++++++++++++------------- crates/completion/src/render.rs | 47 +++++++++-------- crates/completion/src/render/const_.rs | 5 +- crates/completion/src/render/enum_variant.rs | 5 +- crates/completion/src/render/function.rs | 3 +- crates/completion/src/render/macro_.rs | 5 +- crates/completion/src/render/type_alias.rs | 5 +- crates/ide_db/src/lib.rs | 28 +++++----- crates/rust-analyzer/src/to_proto.rs | 36 ++++++++----- 13 files changed, 126 insertions(+), 108 deletions(-) diff --git a/crates/completion/src/completions/fn_param.rs b/crates/completion/src/completions/fn_param.rs index 5505c3559..38e33a93e 100644 --- a/crates/completion/src/completions/fn_param.rs +++ b/crates/completion/src/completions/fn_param.rs @@ -6,7 +6,7 @@ use syntax::{ 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) }); diff --git a/crates/completion/src/completions/mod_.rs b/crates/completion/src/completions/mod_.rs index 00e951ca9..352fc7c77 100644 --- a/crates/completion/src/completions/mod_.rs +++ b/crates/completion/src/completions/mod_.rs @@ -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) }); diff --git a/crates/completion/src/completions/record.rs b/crates/completion/src/completions/record.rs index bb6354ded..0a7927eb8 100644 --- a/crates/completion/src/completions/record.rs +++ b/crates/completion/src/completions/record.rs @@ -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(), ); } diff --git a/crates/completion/src/completions/trait_impl.rs b/crates/completion/src/completions/trait_impl.rs index 135ae49dc..f258ad9c3 100644 --- a/crates/completion/src/completions/trait_impl.rs +++ b/crates/completion/src/completions/trait_impl.rs @@ -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); } diff --git a/crates/completion/src/item.rs b/crates/completion/src/item.rs index ecf581be2..178e05c46 100644 --- a/crates/completion/src/item.rs +++ b/crates/completion/src/item.rs @@ -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 @@ impl Builder { 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) -> Builder { + self.kind = Some(kind.into()); self } pub(crate) fn text_edit(mut self, edit: TextEdit) -> Builder { diff --git a/crates/completion/src/render.rs b/crates/completion/src/render.rs index 68556c8ce..8f66f6e59 100644 --- a/crates/completion/src/render.rs +++ b/crates/completion/src/render.rs @@ -13,7 +13,7 @@ mod builder_ext; 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 @@ impl<'a> Render<'a> { 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 @@ impl<'a> Render<'a> { 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 @@ impl<'a> Render<'a> { 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 @@ impl<'a> Render<'a> { 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, diff --git a/crates/completion/src/render/const_.rs b/crates/completion/src/render/const_.rs index e46452d4e..5010b642a 100644 --- a/crates/completion/src/render/const_.rs +++ b/crates/completion/src/render/const_.rs @@ -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 @@ impl<'a> ConstRender<'a> { 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_) diff --git a/crates/completion/src/render/enum_variant.rs b/crates/completion/src/render/enum_variant.rs index 89fb49773..adcddebd1 100644 --- a/crates/completion/src/render/enum_variant.rs +++ b/crates/completion/src/render/enum_variant.rs @@ -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 @@ impl<'a> EnumRender<'a> { 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) diff --git a/crates/completion/src/render/function.rs b/crates/completion/src/render/function.rs index 8f4c66211..2d616b1fb 100644 --- a/crates/completion/src/render/function.rs +++ b/crates/completion/src/render/function.rs @@ -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 @@ impl<'a> FunctionRender<'a> { if self.func.self_param(self.ctx.db()).is_some() { CompletionItemKind::Method } else { - CompletionItemKind::Function + SymbolKind::Function.into() } } } diff --git a/crates/completion/src/render/macro_.rs b/crates/completion/src/render/macro_.rs index f893e420a..a4535786f 100644 --- a/crates/completion/src/render/macro_.rs +++ b/crates/completion/src/render/macro_.rs @@ -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 @@ impl<'a> MacroRender<'a> { fn render(&self, import_to_add: Option) -> Option { 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) diff --git a/crates/completion/src/render/type_alias.rs b/crates/completion/src/render/type_alias.rs index 29287143a..bd97c3692 100644 --- a/crates/completion/src/render/type_alias.rs +++ b/crates/completion/src/render/type_alias.rs @@ -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 @@ impl<'a> TypeAliasRender<'a> { 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) diff --git a/crates/ide_db/src/lib.rs b/crates/ide_db/src/lib.rs index ee35d02f5..6eb34b06b 100644 --- a/crates/ide_db/src/lib.rs +++ b/crates/ide_db/src/lib.rs @@ -137,24 +137,24 @@ fn line_index(db: &dyn LineIndexDatabase, file_id: FileId) -> Arc { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] pub enum SymbolKind { - Module, - Impl, - Field, - TypeParam, + Const, ConstParam, + Enum, + Field, + Function, + Impl, + Label, LifetimeParam, - ValueParam, - SelfParam, Local, - Label, - Function, - Const, + Macro, + Module, + SelfParam, Static, Struct, - Enum, - Variant, - Union, - TypeAlias, Trait, - Macro, + TypeAlias, + TypeParam, + Union, + ValueParam, + Variant, } diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 18e8a6d22..96f915f1c 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -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, + }, } } -- cgit v1.2.3