Add TypeAliasRender

This commit is contained in:
Igor Aleksanov 2020-11-01 13:10:02 +03:00
parent 944ccf6075
commit 15b16917fc
4 changed files with 54 additions and 19 deletions

View file

@ -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(

View file

@ -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)]

View file

@ -37,8 +37,7 @@ impl<'a> ConstRender<'a> {
}
fn name(&self) -> Option<String> {
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 {

View file

@ -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<CompletionItem> {
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<String> {
self.ast_node.name().map(|name| name.text().to_string())
}
fn detail(&self) -> String {
type_label(&self.ast_node)
}
}