8055: Implement HirDisplay for some more types r=Veykril a=Veykril

bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2021-03-16 15:48:35 +00:00 committed by GitHub
commit 00c80b208b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 16 deletions

View File

@ -12,8 +12,8 @@
use syntax::ast::{self, NameOwner}; use syntax::ast::{self, NameOwner};
use crate::{ use crate::{
Const, ConstParam, Enum, Field, Function, HasVisibility, Module, Static, Struct, Substs, Trait, Adt, Const, ConstParam, Enum, Field, Function, GenericParam, HasVisibility, LifetimeParam,
Type, TypeAlias, TypeParam, Union, Variant, Module, Static, Struct, Substs, Trait, Type, TypeAlias, TypeParam, Union, Variant,
}; };
impl HirDisplay for Function { impl HirDisplay for Function {
@ -120,6 +120,16 @@ fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
} }
} }
impl HirDisplay for Adt {
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
match self {
Adt::Struct(it) => it.hir_fmt(f),
Adt::Union(it) => it.hir_fmt(f),
Adt::Enum(it) => it.hir_fmt(f),
}
}
}
impl HirDisplay for Struct { impl HirDisplay for Struct {
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
write_visibility(self.module(f.db).id, self.visibility(f.db), f)?; write_visibility(self.module(f.db).id, self.visibility(f.db), f)?;
@ -211,6 +221,16 @@ fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
} }
} }
impl HirDisplay for GenericParam {
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
match self {
GenericParam::TypeParam(it) => it.hir_fmt(f),
GenericParam::LifetimeParam(it) => it.hir_fmt(f),
GenericParam::ConstParam(it) => it.hir_fmt(f),
}
}
}
impl HirDisplay for TypeParam { impl HirDisplay for TypeParam {
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
write!(f, "{}", self.name(f.db))?; write!(f, "{}", self.name(f.db))?;
@ -224,6 +244,12 @@ fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
} }
} }
impl HirDisplay for LifetimeParam {
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
write!(f, "{}", self.name(f.db))
}
}
impl HirDisplay for ConstParam { impl HirDisplay for ConstParam {
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
write!(f, "const {}: ", self.name(f.db))?; write!(f, "const {}: ", self.name(f.db))?;

View File

@ -1,6 +1,6 @@
use either::Either; use either::Either;
use hir::{ use hir::{
Adt, AsAssocItem, AssocItemContainer, GenericParam, HasAttrs, HasSource, HirDisplay, Module, AsAssocItem, AssocItemContainer, GenericParam, HasAttrs, HasSource, HirDisplay, Module,
ModuleDef, Semantics, ModuleDef, Semantics,
}; };
use ide_db::{ use ide_db::{
@ -339,9 +339,7 @@ fn hover_for_definition(
Definition::ModuleDef(it) => match it { Definition::ModuleDef(it) => match it {
ModuleDef::Module(it) => from_hir_fmt(db, it, mod_path), ModuleDef::Module(it) => from_hir_fmt(db, it, mod_path),
ModuleDef::Function(it) => from_hir_fmt(db, it, mod_path), ModuleDef::Function(it) => from_hir_fmt(db, it, mod_path),
ModuleDef::Adt(Adt::Struct(it)) => from_hir_fmt(db, it, mod_path), ModuleDef::Adt(it) => from_hir_fmt(db, it, mod_path),
ModuleDef::Adt(Adt::Union(it)) => from_hir_fmt(db, it, mod_path),
ModuleDef::Adt(Adt::Enum(it)) => from_hir_fmt(db, it, mod_path),
ModuleDef::Variant(it) => from_hir_fmt(db, it, mod_path), ModuleDef::Variant(it) => from_hir_fmt(db, it, mod_path),
ModuleDef::Const(it) => from_hir_fmt(db, it, mod_path), ModuleDef::Const(it) => from_hir_fmt(db, it, mod_path),
ModuleDef::Static(it) => from_hir_fmt(db, it, mod_path), ModuleDef::Static(it) => from_hir_fmt(db, it, mod_path),
@ -353,18 +351,10 @@ fn hover_for_definition(
}, },
Definition::Local(it) => hover_for_local(it, db), Definition::Local(it) => hover_for_local(it, db),
Definition::SelfType(impl_def) => { Definition::SelfType(impl_def) => {
impl_def.target_ty(db).as_adt().and_then(|adt| match adt { impl_def.target_ty(db).as_adt().and_then(|adt| from_hir_fmt(db, adt, mod_path))
Adt::Struct(it) => from_hir_fmt(db, it, mod_path),
Adt::Union(it) => from_hir_fmt(db, it, mod_path),
Adt::Enum(it) => from_hir_fmt(db, it, mod_path),
})
} }
Definition::GenericParam(it) => from_hir_fmt(db, it, None),
Definition::Label(it) => Some(Markup::fenced_block(&it.name(db))), Definition::Label(it) => Some(Markup::fenced_block(&it.name(db))),
Definition::GenericParam(it) => match it {
GenericParam::TypeParam(it) => Some(Markup::fenced_block(&it.display(db))),
GenericParam::LifetimeParam(it) => Some(Markup::fenced_block(&it.name(db))),
GenericParam::ConstParam(it) => Some(Markup::fenced_block(&it.display(db))),
},
}; };
fn from_hir_fmt<D>(db: &RootDatabase, def: D, mod_path: Option<String>) -> Option<Markup> fn from_hir_fmt<D>(db: &RootDatabase, def: D, mod_path: Option<String>) -> Option<Markup>