use Source for TypeAlias

This commit is contained in:
Aleksey Kladov 2019-06-11 17:25:55 +03:00
parent 647c8f3df8
commit f2ccc54468
7 changed files with 22 additions and 38 deletions

View File

@ -932,12 +932,19 @@ pub struct TypeAlias {
pub(crate) id: TypeAliasId,
}
impl HasSource for TypeAlias {
type Ast = TreeArc<ast::TypeAliasDef>;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::TypeAliasDef>> {
self.id.source(db).into()
}
}
impl TypeAlias {
pub fn source(
self,
db: &(impl DefDatabase + AstDatabase),
) -> (HirFileId, TreeArc<ast::TypeAliasDef>) {
self.id.source(db)
) -> Source<TreeArc<ast::TypeAliasDef>> {
self.id.source(db).into()
}
pub fn module(self, db: &impl DefDatabase) -> Module {

View File

@ -84,7 +84,7 @@ pub(crate) fn documentation_query(
DocDef::Function(it) => docs_from_ast(&*it.source(db).ast),
DocDef::Union(it) => docs_from_ast(&*it.source(db).1),
DocDef::Trait(it) => docs_from_ast(&*it.source(db).1),
DocDef::TypeAlias(it) => docs_from_ast(&*it.source(db).1),
DocDef::TypeAlias(it) => docs_from_ast(&*it.source(db).ast),
DocDef::MacroDef(it) => docs_from_ast(&*it.source(db).1),
}
}

View File

@ -81,7 +81,7 @@ pub(crate) fn generic_params_query(
});
generics.fill(&*it.source(db).1, start + 1);
}
GenericDef::TypeAlias(it) => generics.fill(&*it.source(db).1, start),
GenericDef::TypeAlias(it) => generics.fill(&*it.source(db).ast, start),
GenericDef::ImplBlock(it) => generics.fill(&*it.source(db).1, start),
}

View File

@ -8,6 +8,6 @@ pub(crate) fn type_alias_ref_query(
db: &(impl DefDatabase + AstDatabase),
typ: TypeAlias,
) -> Arc<TypeRef> {
let (_, node) = typ.source(db);
let node = typ.source(db).ast;
Arc::new(TypeRef::from_ast_opt(node.type_ref()))
}

View File

@ -141,13 +141,12 @@ pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const
}
pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) {
let (_file_id, type_def) = type_alias.source(ctx.db);
let type_def = type_alias.source(ctx.db).ast;
let name = match type_def.name() {
Some(name) => name,
_ => return,
};
let (_, ast_node) = type_alias.source(ctx.db);
let detail = type_label(&ast_node);
let detail = type_label(&type_def);
CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string())
.kind(CompletionItemKind::TypeAlias)

View File

@ -232,15 +232,7 @@ pub(crate) fn from_def(
node.short_label(),
)
}
hir::ModuleDef::TypeAlias(e) => {
let (file_id, node) = e.source(db);
NavigationTarget::from_named(
file_id.original_file(db),
&*node,
node.doc_comment_text(),
node.short_label(),
)
}
hir::ModuleDef::TypeAlias(it) => NavigationTarget::from_def_source(db, it),
hir::ModuleDef::BuiltinType(..) => {
return None;
}
@ -267,15 +259,7 @@ pub(crate) fn from_impl_item(db: &RootDatabase, impl_item: hir::ImplItem) -> Nav
match impl_item {
ImplItem::Method(it) => NavigationTarget::from_function(db, it),
ImplItem::Const(it) => NavigationTarget::from_def_source(db, it),
ImplItem::TypeAlias(a) => {
let (file_id, node) = a.source(db);
NavigationTarget::from_named(
file_id.original_file(db),
&*node,
node.doc_comment_text(),
node.short_label(),
)
}
ImplItem::TypeAlias(it) => NavigationTarget::from_def_source(db, it),
}
}

View File

@ -107,14 +107,11 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
res.extend(hover_text(it.doc_comment_text(), it.short_label()));
}
}
Some(AssocItem(it)) => match it {
hir::ImplItem::Method(it) => res.extend(from_def_source(db, it)),
hir::ImplItem::Const(it) => res.extend(from_def_source(db, it)),
hir::ImplItem::TypeAlias(it) => {
let it = it.source(db).1;
res.extend(hover_text(it.doc_comment_text(), it.short_label()))
}
},
Some(AssocItem(it)) => res.extend(match it {
hir::ImplItem::Method(it) => from_def_source(db, it),
hir::ImplItem::Const(it) => from_def_source(db, it),
hir::ImplItem::TypeAlias(it) => from_def_source(db, it),
}),
Some(Def(it)) => {
match it {
hir::ModuleDef::Module(it) => {
@ -137,10 +134,7 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
let it = it.source(db).1;
res.extend(hover_text(it.doc_comment_text(), it.short_label()))
}
hir::ModuleDef::TypeAlias(it) => {
let it = it.source(db).1;
res.extend(hover_text(it.doc_comment_text(), it.short_label()))
}
hir::ModuleDef::TypeAlias(it) => res.extend(from_def_source(db, it)),
hir::ModuleDef::BuiltinType(_) => {
// FIXME: hover for builtin Type ?
}