From 15b16917fcd55068d9aba3d4b5d87763ec5deb69 Mon Sep 17 00:00:00 2001 From: Igor Aleksanov Date: Sun, 1 Nov 2020 13:10:02 +0300 Subject: Add TypeAliasRender --- crates/completion/src/completions.rs | 21 ++++---------- crates/completion/src/render.rs | 3 +- crates/completion/src/render/const_.rs | 3 +- crates/completion/src/render/type_alias.rs | 46 ++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 19 deletions(-) create mode 100644 crates/completion/src/render/type_alias.rs (limited to 'crates') diff --git a/crates/completion/src/completions.rs b/crates/completion/src/completions.rs index d8dc1b1c3..434366b12 100644 --- a/crates/completion/src/completions.rs +++ b/crates/completion/src/completions.rs @@ -14,13 +14,12 @@ pub(crate) mod macro_in_item_position; pub(crate) mod trait_impl; pub(crate) mod mod_; -use hir::{HasAttrs, HasSource, HirDisplay, ModPath, Mutability, ScopeDef, Type}; -use syntax::{ast::NameOwner, display::*}; +use hir::{HasAttrs, HirDisplay, ModPath, Mutability, ScopeDef, Type}; use test_utils::mark; use crate::{ item::Builder, - render::{ConstRender, EnumVariantRender, FunctionRender, MacroRender}, + render::{ConstRender, EnumVariantRender, FunctionRender, MacroRender, TypeAliasRender}, CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, CompletionScore, RootDatabase, }; @@ -216,19 +215,9 @@ impl Completions { } pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) { - let type_def = type_alias.source(ctx.db).value; - let name = match type_def.name() { - Some(name) => name, - _ => return, - }; - let detail = type_label(&type_def); - - CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string()) - .kind(CompletionItemKind::TypeAlias) - .set_documentation(type_alias.docs(ctx.db)) - .set_deprecated(is_deprecated(type_alias, ctx.db)) - .detail(detail) - .add_to(self); + if let Some(item) = TypeAliasRender::new(ctx.into(), type_alias).render() { + self.add(item) + } } pub(crate) fn add_qualified_enum_variant( diff --git a/crates/completion/src/render.rs b/crates/completion/src/render.rs index c614a7172..3a14357f3 100644 --- a/crates/completion/src/render.rs +++ b/crates/completion/src/render.rs @@ -6,6 +6,7 @@ mod function; mod builder_ext; mod enum_variant; mod const_; +mod type_alias; use hir::{Documentation, HasAttrs}; use ide_db::RootDatabase; @@ -15,7 +16,7 @@ use crate::{config::SnippetCap, CompletionContext}; pub(crate) use crate::render::{ const_::ConstRender, enum_variant::EnumVariantRender, function::FunctionRender, - macro_::MacroRender, + macro_::MacroRender, type_alias::TypeAliasRender, }; #[derive(Debug)] diff --git a/crates/completion/src/render/const_.rs b/crates/completion/src/render/const_.rs index d88bfa07f..829eb574d 100644 --- a/crates/completion/src/render/const_.rs +++ b/crates/completion/src/render/const_.rs @@ -37,8 +37,7 @@ impl<'a> ConstRender<'a> { } fn name(&self) -> Option { - let ast_node = self.const_.source(self.ctx.db()).value; - ast_node.name().map(|name| name.text().to_string()) + self.ast_node.name().map(|name| name.text().to_string()) } fn detail(&self) -> String { diff --git a/crates/completion/src/render/type_alias.rs b/crates/completion/src/render/type_alias.rs new file mode 100644 index 000000000..378aa8c67 --- /dev/null +++ b/crates/completion/src/render/type_alias.rs @@ -0,0 +1,46 @@ +use hir::HasSource; +use syntax::{ + ast::{NameOwner, TypeAlias}, + display::type_label, +}; + +use crate::{ + item::{CompletionItem, CompletionItemKind, CompletionKind}, + render::RenderContext, +}; + +#[derive(Debug)] +pub(crate) struct TypeAliasRender<'a> { + ctx: RenderContext<'a>, + type_alias: hir::TypeAlias, + ast_node: TypeAlias, +} + +impl<'a> TypeAliasRender<'a> { + pub(crate) fn new(ctx: RenderContext<'a>, type_alias: hir::TypeAlias) -> TypeAliasRender<'a> { + let ast_node = type_alias.source(ctx.db()).value; + TypeAliasRender { ctx, type_alias, ast_node } + } + + pub(crate) fn render(self) -> Option { + let name = self.name()?; + let detail = self.detail(); + + let item = CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), name) + .kind(CompletionItemKind::TypeAlias) + .set_documentation(self.ctx.docs(self.type_alias)) + .set_deprecated(self.ctx.is_deprecated(self.type_alias)) + .detail(detail) + .build(); + + Some(item) + } + + fn name(&self) -> Option { + self.ast_node.name().map(|name| name.text().to_string()) + } + + fn detail(&self) -> String { + type_label(&self.ast_node) + } +} -- cgit v1.2.3