From 4d1e1daa31239cb71a81c6958c1b9137274fd189 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 16 Mar 2021 16:36:34 +0100 Subject: Implement HirDisplay for some more types --- crates/hir/src/display.rs | 30 ++++++++++++++++++++++++++++-- crates/ide/src/hover.rs | 18 ++++-------------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/crates/hir/src/display.rs b/crates/hir/src/display.rs index 44cdcc296..81f0e04e5 100644 --- a/crates/hir/src/display.rs +++ b/crates/hir/src/display.rs @@ -12,8 +12,8 @@ use hir_ty::display::{ use syntax::ast::{self, NameOwner}; use crate::{ - Const, ConstParam, Enum, Field, Function, HasVisibility, Module, Static, Struct, Substs, Trait, - Type, TypeAlias, TypeParam, Union, Variant, + Adt, Const, ConstParam, Enum, Field, Function, GenericParam, HasVisibility, LifetimeParam, + Module, Static, Struct, Substs, Trait, Type, TypeAlias, TypeParam, Union, Variant, }; impl HirDisplay for Function { @@ -120,6 +120,16 @@ impl HirDisplay for Function { } } +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 { fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { write_visibility(self.module(f.db).id, self.visibility(f.db), f)?; @@ -211,6 +221,16 @@ impl HirDisplay for Type { } } +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 { fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { write!(f, "{}", self.name(f.db))?; @@ -224,6 +244,12 @@ impl HirDisplay for TypeParam { } } +impl HirDisplay for LifetimeParam { + fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { + write!(f, "{}", self.name(f.db)) + } +} + impl HirDisplay for ConstParam { fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { write!(f, "const {}: ", self.name(f.db))?; diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 8d45b4875..325014622 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -1,6 +1,6 @@ use either::Either; use hir::{ - Adt, AsAssocItem, AssocItemContainer, GenericParam, HasAttrs, HasSource, HirDisplay, Module, + AsAssocItem, AssocItemContainer, GenericParam, HasAttrs, HasSource, HirDisplay, Module, ModuleDef, Semantics, }; use ide_db::{ @@ -339,9 +339,7 @@ fn hover_for_definition( Definition::ModuleDef(it) => match it { ModuleDef::Module(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(Adt::Union(it)) => from_hir_fmt(db, it, mod_path), - ModuleDef::Adt(Adt::Enum(it)) => from_hir_fmt(db, it, mod_path), + ModuleDef::Adt(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::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::SelfType(impl_def) => { - impl_def.target_ty(db).as_adt().and_then(|adt| match adt { - 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), - }) + impl_def.target_ty(db).as_adt().and_then(|adt| from_hir_fmt(db, adt, mod_path)) } + Definition::GenericParam(it) => from_hir_fmt(db, it, None), 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(db: &RootDatabase, def: D, mod_path: Option) -> Option -- cgit v1.2.3