aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoroxalica <[email protected]>2021-03-15 16:05:03 +0000
committeroxalica <[email protected]>2021-03-15 17:04:33 +0000
commit87171238c6c528c421f06de8cd7e41ed3b6ff57a (patch)
treeb7abccc6adf5df6baae0d97146b9e9b6c5ef7c32
parentef416e0154767619fcbfa0d1682b28bd338a8ce9 (diff)
Use hir formatter more
-rw-r--r--crates/hir/src/display.rs180
-rw-r--r--crates/hir/src/lib.rs36
-rw-r--r--crates/hir_def/src/adt.rs10
-rw-r--r--crates/hir_def/src/data.rs12
-rw-r--r--crates/hir_def/src/item_tree.rs4
-rw-r--r--crates/hir_def/src/item_tree/lower.rs8
-rw-r--r--crates/hir_ty/src/traits/chalk.rs2
-rw-r--r--crates/ide/src/hover.rs59
8 files changed, 258 insertions, 53 deletions
diff --git a/crates/hir/src/display.rs b/crates/hir/src/display.rs
index ae08e2584..0640712de 100644
--- a/crates/hir/src/display.rs
+++ b/crates/hir/src/display.rs
@@ -1,8 +1,9 @@
1//! HirDisplay implementations for various hir types. 1//! HirDisplay implementations for various hir types.
2use hir_def::{ 2use hir_def::{
3 adt::VariantData,
3 generics::{TypeParamProvenance, WherePredicate, WherePredicateTypeTarget}, 4 generics::{TypeParamProvenance, WherePredicate, WherePredicateTypeTarget},
4 type_ref::{TypeBound, TypeRef}, 5 type_ref::{TypeBound, TypeRef},
5 GenericDefId, 6 AdtId, GenericDefId,
6}; 7};
7use hir_ty::display::{ 8use hir_ty::display::{
8 write_bounds_like_dyn_trait_with_prefix, write_visibility, HirDisplay, HirDisplayError, 9 write_bounds_like_dyn_trait_with_prefix, write_visibility, HirDisplay, HirDisplayError,
@@ -10,7 +11,10 @@ use hir_ty::display::{
10}; 11};
11use syntax::ast::{self, NameOwner}; 12use syntax::ast::{self, NameOwner};
12 13
13use crate::{Function, HasVisibility, Substs, Type, TypeParam}; 14use crate::{
15 Const, ConstParam, Enum, Field, Function, HasVisibility, Module, Static, Struct, Substs, Trait,
16 Type, TypeAlias, TypeParam, Union, Variant,
17};
14 18
15impl HirDisplay for Function { 19impl HirDisplay for Function {
16 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { 20 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
@@ -116,6 +120,91 @@ impl HirDisplay for Function {
116 } 120 }
117} 121}
118 122
123impl HirDisplay for Struct {
124 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
125 write_visibility(self.module(f.db).id, self.visibility(f.db), f)?;
126 write!(f, "struct ")?;
127 write!(f, "{}", self.name(f.db))?;
128 let def_id = GenericDefId::AdtId(AdtId::StructId(self.id));
129 write_generic_params(def_id, f)?;
130 write_where_clause(def_id, f)?;
131 Ok(())
132 }
133}
134
135impl HirDisplay for Enum {
136 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
137 write_visibility(self.module(f.db).id, self.visibility(f.db), f)?;
138 write!(f, "enum ")?;
139 write!(f, "{}", self.name(f.db))?;
140 let def_id = GenericDefId::AdtId(AdtId::EnumId(self.id));
141 write_generic_params(def_id, f)?;
142 write_where_clause(def_id, f)?;
143 Ok(())
144 }
145}
146
147impl HirDisplay for Union {
148 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
149 write_visibility(self.module(f.db).id, self.visibility(f.db), f)?;
150 write!(f, "union ")?;
151 write!(f, "{}", self.name(f.db))?;
152 let def_id = GenericDefId::AdtId(AdtId::UnionId(self.id));
153 write_generic_params(def_id, f)?;
154 write_where_clause(def_id, f)?;
155 Ok(())
156 }
157}
158
159impl HirDisplay for Field {
160 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
161 write_visibility(self.parent.module(f.db).id, self.visibility(f.db), f)?;
162 write!(f, "{}: ", self.name(f.db))?;
163 self.signature_ty(f.db).hir_fmt(f)
164 }
165}
166
167impl HirDisplay for Variant {
168 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
169 write!(f, "{}", self.name(f.db))?;
170 let data = self.variant_data(f.db);
171 match &*data {
172 VariantData::Unit => {}
173 VariantData::Tuple(fields) => {
174 write!(f, "(")?;
175 let mut first = true;
176 for (_, field) in fields.iter() {
177 if first {
178 first = false;
179 } else {
180 write!(f, ", ")?;
181 }
182 // Enum variant fields must be pub.
183 field.type_ref.hir_fmt(f)?;
184 }
185 write!(f, ")")?;
186 }
187 VariantData::Record(fields) => {
188 write!(f, " {{")?;
189 let mut first = true;
190 for (_, field) in fields.iter() {
191 if first {
192 first = false;
193 write!(f, " ")?;
194 } else {
195 write!(f, ", ")?;
196 }
197 // Enum variant fields must be pub.
198 write!(f, "{}: ", field.name)?;
199 field.type_ref.hir_fmt(f)?;
200 }
201 write!(f, " }}")?;
202 }
203 }
204 Ok(())
205 }
206}
207
119impl HirDisplay for Type { 208impl HirDisplay for Type {
120 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { 209 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
121 self.ty.value.hir_fmt(f) 210 self.ty.value.hir_fmt(f)
@@ -135,6 +224,13 @@ impl HirDisplay for TypeParam {
135 } 224 }
136} 225}
137 226
227impl HirDisplay for ConstParam {
228 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
229 write!(f, "const {}: ", self.name(f.db))?;
230 self.ty(f.db).hir_fmt(f)
231 }
232}
233
138fn write_generic_params(def: GenericDefId, f: &mut HirFormatter) -> Result<(), HirDisplayError> { 234fn write_generic_params(def: GenericDefId, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
139 let params = f.db.generic_params(def); 235 let params = f.db.generic_params(def);
140 if params.lifetimes.is_empty() && params.types.is_empty() && params.consts.is_empty() { 236 if params.lifetimes.is_empty() && params.types.is_empty() && params.consts.is_empty() {
@@ -253,3 +349,83 @@ fn write_where_clause(def: GenericDefId, f: &mut HirFormatter) -> Result<(), Hir
253 349
254 Ok(()) 350 Ok(())
255} 351}
352
353impl HirDisplay for Const {
354 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
355 write_visibility(self.module(f.db).id, self.visibility(f.db), f)?;
356 let data = f.db.const_data(self.id);
357 write!(f, "const ")?;
358 match &data.name {
359 Some(name) => write!(f, "{}: ", name)?,
360 None => write!(f, "_: ")?,
361 }
362 data.type_ref.hir_fmt(f)?;
363 Ok(())
364 }
365}
366
367impl HirDisplay for Static {
368 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
369 write_visibility(self.module(f.db).id, self.visibility(f.db), f)?;
370 let data = f.db.static_data(self.id);
371 write!(f, "static ")?;
372 if data.mutable {
373 write!(f, "mut ")?;
374 }
375 match &data.name {
376 Some(name) => write!(f, "{}: ", name)?,
377 None => write!(f, "_: ")?,
378 }
379 data.type_ref.hir_fmt(f)?;
380 Ok(())
381 }
382}
383
384impl HirDisplay for Trait {
385 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
386 write_visibility(self.module(f.db).id, self.visibility(f.db), f)?;
387 let data = f.db.trait_data(self.id);
388 if data.is_unsafe {
389 write!(f, "unsafe ")?;
390 }
391 if data.is_auto {
392 write!(f, "auto ")?;
393 }
394 write!(f, "trait {}", data.name)?;
395 let def_id = GenericDefId::TraitId(self.id);
396 write_generic_params(def_id, f)?;
397 if !data.bounds.is_empty() {
398 write!(f, ": ")?;
399 f.write_joined(&*data.bounds, " + ")?;
400 }
401 write_where_clause(def_id, f)?;
402 Ok(())
403 }
404}
405
406impl HirDisplay for TypeAlias {
407 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
408 write_visibility(self.module(f.db).id, self.visibility(f.db), f)?;
409 let data = f.db.type_alias_data(self.id);
410 write!(f, "type {}", data.name)?;
411 if !data.bounds.is_empty() {
412 write!(f, ": ")?;
413 f.write_joined(&data.bounds, " + ")?;
414 }
415 if let Some(ty) = &data.type_ref {
416 write!(f, " = ")?;
417 ty.hir_fmt(f)?;
418 }
419 Ok(())
420 }
421}
422
423impl HirDisplay for Module {
424 fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
425 // FIXME: Module doesn't have visibility saved in data.
426 match self.name(f.db) {
427 Some(name) => write!(f, "mod {}", name),
428 None => write!(f, "mod {{unnamed}}"),
429 }
430 }
431}
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index 476fdb132..98135602a 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -572,6 +572,12 @@ impl Struct {
572 } 572 }
573} 573}
574 574
575impl HasVisibility for Struct {
576 fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
577 db.struct_data(self.id).visibility.resolve(db.upcast(), &self.id.resolver(db.upcast()))
578 }
579}
580
575#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 581#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
576pub struct Union { 582pub struct Union {
577 pub(crate) id: UnionId, 583 pub(crate) id: UnionId,
@@ -604,6 +610,12 @@ impl Union {
604 } 610 }
605} 611}
606 612
613impl HasVisibility for Union {
614 fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
615 db.union_data(self.id).visibility.resolve(db.upcast(), &self.id.resolver(db.upcast()))
616 }
617}
618
607#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 619#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
608pub struct Enum { 620pub struct Enum {
609 pub(crate) id: EnumId, 621 pub(crate) id: EnumId,
@@ -631,6 +643,12 @@ impl Enum {
631 } 643 }
632} 644}
633 645
646impl HasVisibility for Enum {
647 fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
648 db.enum_data(self.id).visibility.resolve(db.upcast(), &self.id.resolver(db.upcast()))
649 }
650}
651
634#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 652#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
635pub struct Variant { 653pub struct Variant {
636 pub(crate) parent: Enum, 654 pub(crate) parent: Enum,
@@ -962,6 +980,10 @@ impl Const {
962 pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { 980 pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
963 db.const_data(self.id).name.clone() 981 db.const_data(self.id).name.clone()
964 } 982 }
983
984 pub fn type_ref(self, db: &dyn HirDatabase) -> TypeRef {
985 db.const_data(self.id).type_ref.clone()
986 }
965} 987}
966 988
967impl HasVisibility for Const { 989impl HasVisibility for Const {
@@ -995,6 +1017,12 @@ impl Static {
995 } 1017 }
996} 1018}
997 1019
1020impl HasVisibility for Static {
1021 fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
1022 db.static_data(self.id).visibility.resolve(db.upcast(), &self.id.resolver(db.upcast()))
1023 }
1024}
1025
998#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 1026#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
999pub struct Trait { 1027pub struct Trait {
1000 pub(crate) id: TraitId, 1028 pub(crate) id: TraitId,
@@ -1014,7 +1042,13 @@ impl Trait {
1014 } 1042 }
1015 1043
1016 pub fn is_auto(self, db: &dyn HirDatabase) -> bool { 1044 pub fn is_auto(self, db: &dyn HirDatabase) -> bool {
1017 db.trait_data(self.id).auto 1045 db.trait_data(self.id).is_auto
1046 }
1047}
1048
1049impl HasVisibility for Trait {
1050 fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
1051 db.trait_data(self.id).visibility.resolve(db.upcast(), &self.id.resolver(db.upcast()))
1018 } 1052 }
1019} 1053}
1020 1054
diff --git a/crates/hir_def/src/adt.rs b/crates/hir_def/src/adt.rs
index efbde17d8..1b9bb8235 100644
--- a/crates/hir_def/src/adt.rs
+++ b/crates/hir_def/src/adt.rs
@@ -31,12 +31,14 @@ pub struct StructData {
31 pub name: Name, 31 pub name: Name,
32 pub variant_data: Arc<VariantData>, 32 pub variant_data: Arc<VariantData>,
33 pub repr: Option<ReprKind>, 33 pub repr: Option<ReprKind>,
34 pub visibility: RawVisibility,
34} 35}
35 36
36#[derive(Debug, Clone, PartialEq, Eq)] 37#[derive(Debug, Clone, PartialEq, Eq)]
37pub struct EnumData { 38pub struct EnumData {
38 pub name: Name, 39 pub name: Name,
39 pub variants: Arena<EnumVariantData>, 40 pub variants: Arena<EnumVariantData>,
41 pub visibility: RawVisibility,
40} 42}
41 43
42#[derive(Debug, Clone, PartialEq, Eq)] 44#[derive(Debug, Clone, PartialEq, Eq)]
@@ -102,6 +104,7 @@ impl StructData {
102 name: strukt.name.clone(), 104 name: strukt.name.clone(),
103 variant_data: Arc::new(variant_data), 105 variant_data: Arc::new(variant_data),
104 repr, 106 repr,
107 visibility: item_tree[strukt.visibility].clone(),
105 }) 108 })
106 } 109 }
107 pub(crate) fn union_data_query(db: &dyn DefDatabase, id: UnionId) -> Arc<StructData> { 110 pub(crate) fn union_data_query(db: &dyn DefDatabase, id: UnionId) -> Arc<StructData> {
@@ -118,6 +121,7 @@ impl StructData {
118 name: union.name.clone(), 121 name: union.name.clone(),
119 variant_data: Arc::new(variant_data), 122 variant_data: Arc::new(variant_data),
120 repr, 123 repr,
124 visibility: item_tree[union.visibility].clone(),
121 }) 125 })
122 } 126 }
123} 127}
@@ -150,7 +154,11 @@ impl EnumData {
150 } 154 }
151 } 155 }
152 156
153 Arc::new(EnumData { name: enum_.name.clone(), variants }) 157 Arc::new(EnumData {
158 name: enum_.name.clone(),
159 variants,
160 visibility: item_tree[enum_.visibility].clone(),
161 })
154 } 162 }
155 163
156 pub fn variant(&self, name: &Name) -> Option<LocalEnumVariantId> { 164 pub fn variant(&self, name: &Name) -> Option<LocalEnumVariantId> {
diff --git a/crates/hir_def/src/data.rs b/crates/hir_def/src/data.rs
index b09da0697..74a2194e5 100644
--- a/crates/hir_def/src/data.rs
+++ b/crates/hir_def/src/data.rs
@@ -87,7 +87,10 @@ impl TypeAliasData {
87pub struct TraitData { 87pub struct TraitData {
88 pub name: Name, 88 pub name: Name,
89 pub items: Vec<(Name, AssocItemId)>, 89 pub items: Vec<(Name, AssocItemId)>,
90 pub auto: bool, 90 pub is_auto: bool,
91 pub is_unsafe: bool,
92 pub visibility: RawVisibility,
93 pub bounds: Box<[TypeBound]>,
91} 94}
92 95
93impl TraitData { 96impl TraitData {
@@ -96,10 +99,13 @@ impl TraitData {
96 let item_tree = db.item_tree(tr_loc.id.file_id); 99 let item_tree = db.item_tree(tr_loc.id.file_id);
97 let tr_def = &item_tree[tr_loc.id.value]; 100 let tr_def = &item_tree[tr_loc.id.value];
98 let name = tr_def.name.clone(); 101 let name = tr_def.name.clone();
99 let auto = tr_def.auto; 102 let is_auto = tr_def.is_auto;
103 let is_unsafe = tr_def.is_unsafe;
100 let module_id = tr_loc.container; 104 let module_id = tr_loc.container;
101 let container = AssocContainerId::TraitId(tr); 105 let container = AssocContainerId::TraitId(tr);
102 let mut expander = Expander::new(db, tr_loc.id.file_id, module_id); 106 let mut expander = Expander::new(db, tr_loc.id.file_id, module_id);
107 let visibility = item_tree[tr_def.visibility].clone();
108 let bounds = tr_def.bounds.clone();
103 109
104 let items = collect_items( 110 let items = collect_items(
105 db, 111 db,
@@ -111,7 +117,7 @@ impl TraitData {
111 100, 117 100,
112 ); 118 );
113 119
114 Arc::new(TraitData { name, items, auto }) 120 Arc::new(TraitData { name, items, is_auto, is_unsafe, visibility, bounds })
115 } 121 }
116 122
117 pub fn associated_types(&self) -> impl Iterator<Item = TypeAliasId> + '_ { 123 pub fn associated_types(&self) -> impl Iterator<Item = TypeAliasId> + '_ {
diff --git a/crates/hir_def/src/item_tree.rs b/crates/hir_def/src/item_tree.rs
index d8f22d442..d63ac3df3 100644
--- a/crates/hir_def/src/item_tree.rs
+++ b/crates/hir_def/src/item_tree.rs
@@ -633,7 +633,9 @@ pub struct Trait {
633 pub name: Name, 633 pub name: Name,
634 pub visibility: RawVisibilityId, 634 pub visibility: RawVisibilityId,
635 pub generic_params: GenericParamsId, 635 pub generic_params: GenericParamsId,
636 pub auto: bool, 636 pub is_auto: bool,
637 pub is_unsafe: bool,
638 pub bounds: Box<[TypeBound]>,
637 pub items: Box<[AssocItem]>, 639 pub items: Box<[AssocItem]>,
638 pub ast_id: FileAstId<ast::Trait>, 640 pub ast_id: FileAstId<ast::Trait>,
639} 641}
diff --git a/crates/hir_def/src/item_tree/lower.rs b/crates/hir_def/src/item_tree/lower.rs
index 6b5438dc9..7e91b991d 100644
--- a/crates/hir_def/src/item_tree/lower.rs
+++ b/crates/hir_def/src/item_tree/lower.rs
@@ -500,7 +500,9 @@ impl Ctx {
500 let visibility = self.lower_visibility(trait_def); 500 let visibility = self.lower_visibility(trait_def);
501 let generic_params = 501 let generic_params =
502 self.lower_generic_params_and_inner_items(GenericsOwner::Trait(trait_def), trait_def); 502 self.lower_generic_params_and_inner_items(GenericsOwner::Trait(trait_def), trait_def);
503 let auto = trait_def.auto_token().is_some(); 503 let is_auto = trait_def.auto_token().is_some();
504 let is_unsafe = trait_def.unsafe_token().is_some();
505 let bounds = self.lower_type_bounds(trait_def);
504 let items = trait_def.assoc_item_list().map(|list| { 506 let items = trait_def.assoc_item_list().map(|list| {
505 self.with_inherited_visibility(visibility, |this| { 507 self.with_inherited_visibility(visibility, |this| {
506 list.assoc_items() 508 list.assoc_items()
@@ -520,7 +522,9 @@ impl Ctx {
520 name, 522 name,
521 visibility, 523 visibility,
522 generic_params, 524 generic_params,
523 auto, 525 is_auto,
526 is_unsafe,
527 bounds: bounds.into(),
524 items: items.unwrap_or_default(), 528 items: items.unwrap_or_default(),
525 ast_id, 529 ast_id,
526 }; 530 };
diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/traits/chalk.rs
index 232cf9cd0..4bd8ba303 100644
--- a/crates/hir_ty/src/traits/chalk.rs
+++ b/crates/hir_ty/src/traits/chalk.rs
@@ -429,7 +429,7 @@ pub(crate) fn trait_datum_query(
429 let generic_params = generics(db.upcast(), trait_.into()); 429 let generic_params = generics(db.upcast(), trait_.into());
430 let bound_vars = Substs::bound_vars(&generic_params, DebruijnIndex::INNERMOST); 430 let bound_vars = Substs::bound_vars(&generic_params, DebruijnIndex::INNERMOST);
431 let flags = rust_ir::TraitFlags { 431 let flags = rust_ir::TraitFlags {
432 auto: trait_data.auto, 432 auto: trait_data.is_auto,
433 upstream: trait_.lookup(db.upcast()).container.krate() != krate, 433 upstream: trait_.lookup(db.upcast()).container.krate() != krate,
434 non_enumerable: true, 434 non_enumerable: true,
435 coinductive: false, // only relevant for Chalk testing 435 coinductive: false, // only relevant for Chalk testing
diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs
index 1e4c247c0..7328f410a 100644
--- a/crates/ide/src/hover.rs
+++ b/crates/ide/src/hover.rs
@@ -1,7 +1,7 @@
1use either::Either; 1use either::Either;
2use hir::{ 2use hir::{
3 Adt, AsAssocItem, AssocItemContainer, FieldSource, GenericParam, HasAttrs, HasSource, 3 Adt, AsAssocItem, AssocItemContainer, GenericParam, HasAttrs, HasSource, HirDisplay, Module,
4 HirDisplay, Module, ModuleDef, ModuleSource, Semantics, 4 ModuleDef, Semantics,
5}; 5};
6use ide_db::{ 6use ide_db::{
7 base_db::SourceDatabase, 7 base_db::SourceDatabase,
@@ -14,7 +14,7 @@ use stdx::format_to;
14use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset, T}; 14use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset, T};
15 15
16use crate::{ 16use crate::{
17 display::{macro_label, ShortLabel, TryToNav}, 17 display::{macro_label, TryToNav},
18 doc_links::{remove_links, rewrite_links}, 18 doc_links::{remove_links, rewrite_links},
19 markdown_remove::remove_markdown, 19 markdown_remove::remove_markdown,
20 markup::Markup, 20 markup::Markup,
@@ -335,34 +335,18 @@ fn hover_for_definition(
335 let label = macro_label(&it.source(db)?.value); 335 let label = macro_label(&it.source(db)?.value);
336 from_def_source_labeled(db, it, Some(label), mod_path) 336 from_def_source_labeled(db, it, Some(label), mod_path)
337 } 337 }
338 Definition::Field(def) => { 338 Definition::Field(def) => from_hir_fmt(db, def, mod_path),
339 let src = def.source(db)?.value;
340 if let FieldSource::Named(it) = src {
341 from_def_source_labeled(db, def, it.short_label(), mod_path)
342 } else {
343 None
344 }
345 }
346 Definition::ModuleDef(it) => match it { 339 Definition::ModuleDef(it) => match it {
347 ModuleDef::Module(it) => from_def_source_labeled( 340 ModuleDef::Module(it) => from_hir_fmt(db, it, mod_path),
348 db,
349 it,
350 match it.definition_source(db).value {
351 ModuleSource::Module(it) => it.short_label(),
352 ModuleSource::SourceFile(it) => it.short_label(),
353 ModuleSource::BlockExpr(it) => it.short_label(),
354 },
355 mod_path,
356 ),
357 ModuleDef::Function(it) => from_hir_fmt(db, it, mod_path), 341 ModuleDef::Function(it) => from_hir_fmt(db, it, mod_path),
358 ModuleDef::Adt(Adt::Struct(it)) => from_def_source(db, it, mod_path), 342 ModuleDef::Adt(Adt::Struct(it)) => from_hir_fmt(db, it, mod_path),
359 ModuleDef::Adt(Adt::Union(it)) => from_def_source(db, it, mod_path), 343 ModuleDef::Adt(Adt::Union(it)) => from_hir_fmt(db, it, mod_path),
360 ModuleDef::Adt(Adt::Enum(it)) => from_def_source(db, it, mod_path), 344 ModuleDef::Adt(Adt::Enum(it)) => from_hir_fmt(db, it, mod_path),
361 ModuleDef::Variant(it) => from_def_source(db, it, mod_path), 345 ModuleDef::Variant(it) => from_hir_fmt(db, it, mod_path),
362 ModuleDef::Const(it) => from_def_source(db, it, mod_path), 346 ModuleDef::Const(it) => from_hir_fmt(db, it, mod_path),
363 ModuleDef::Static(it) => from_def_source(db, it, mod_path), 347 ModuleDef::Static(it) => from_hir_fmt(db, it, mod_path),
364 ModuleDef::Trait(it) => from_def_source(db, it, mod_path), 348 ModuleDef::Trait(it) => from_hir_fmt(db, it, mod_path),
365 ModuleDef::TypeAlias(it) => from_def_source(db, it, mod_path), 349 ModuleDef::TypeAlias(it) => from_hir_fmt(db, it, mod_path),
366 ModuleDef::BuiltinType(it) => famous_defs 350 ModuleDef::BuiltinType(it) => famous_defs
367 .and_then(|fd| hover_for_builtin(fd, it)) 351 .and_then(|fd| hover_for_builtin(fd, it))
368 .or_else(|| Some(Markup::fenced_block(&it.name()))), 352 .or_else(|| Some(Markup::fenced_block(&it.name()))),
@@ -370,16 +354,16 @@ fn hover_for_definition(
370 Definition::Local(it) => hover_for_local(it, db), 354 Definition::Local(it) => hover_for_local(it, db),
371 Definition::SelfType(impl_def) => { 355 Definition::SelfType(impl_def) => {
372 impl_def.target_ty(db).as_adt().and_then(|adt| match adt { 356 impl_def.target_ty(db).as_adt().and_then(|adt| match adt {
373 Adt::Struct(it) => from_def_source(db, it, mod_path), 357 Adt::Struct(it) => from_hir_fmt(db, it, mod_path),
374 Adt::Union(it) => from_def_source(db, it, mod_path), 358 Adt::Union(it) => from_hir_fmt(db, it, mod_path),
375 Adt::Enum(it) => from_def_source(db, it, mod_path), 359 Adt::Enum(it) => from_hir_fmt(db, it, mod_path),
376 }) 360 })
377 } 361 }
378 Definition::Label(it) => Some(Markup::fenced_block(&it.name(db))), 362 Definition::Label(it) => Some(Markup::fenced_block(&it.name(db))),
379 Definition::GenericParam(it) => match it { 363 Definition::GenericParam(it) => match it {
380 GenericParam::TypeParam(it) => Some(Markup::fenced_block(&it.display(db))), 364 GenericParam::TypeParam(it) => Some(Markup::fenced_block(&it.display(db))),
381 GenericParam::LifetimeParam(it) => Some(Markup::fenced_block(&it.name(db))), 365 GenericParam::LifetimeParam(it) => Some(Markup::fenced_block(&it.name(db))),
382 GenericParam::ConstParam(it) => from_def_source(db, it, None), 366 GenericParam::ConstParam(it) => Some(Markup::fenced_block(&it.display(db))),
383 }, 367 },
384 }; 368 };
385 369
@@ -391,15 +375,6 @@ fn hover_for_definition(
391 from_def_source_labeled(db, def, Some(label), mod_path) 375 from_def_source_labeled(db, def, Some(label), mod_path)
392 } 376 }
393 377
394 fn from_def_source<A, D>(db: &RootDatabase, def: D, mod_path: Option<String>) -> Option<Markup>
395 where
396 D: HasSource<Ast = A> + HasAttrs + Copy,
397 A: ShortLabel,
398 {
399 let short_label = def.source(db)?.value.short_label();
400 from_def_source_labeled(db, def, short_label, mod_path)
401 }
402
403 fn from_def_source_labeled<D>( 378 fn from_def_source_labeled<D>(
404 db: &RootDatabase, 379 db: &RootDatabase,
405 def: D, 380 def: D,