Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide/src/hover/render.rs')
-rw-r--r--crates/ide/src/hover/render.rs47
1 files changed, 44 insertions, 3 deletions
diff --git a/crates/ide/src/hover/render.rs b/crates/ide/src/hover/render.rs
index fb00a40f96..f37c9f4a6d 100644
--- a/crates/ide/src/hover/render.rs
+++ b/crates/ide/src/hover/render.rs
@@ -2,7 +2,9 @@
use std::fmt::Display;
use either::Either;
-use hir::{AsAssocItem, AttributeTemplate, HasAttrs, HasSource, HirDisplay, Semantics, TypeInfo};
+use hir::{
+ Adt, AsAssocItem, AttributeTemplate, HasAttrs, HasSource, HirDisplay, Semantics, TypeInfo,
+};
use ide_db::{
base_db::SourceDatabase,
defs::Definition,
@@ -388,10 +390,30 @@ pub(super) fn definition(
let mod_path = definition_mod_path(db, &def);
let (label, docs) = match def {
Definition::Macro(it) => label_and_docs(db, it),
- Definition::Field(it) => label_and_docs(db, it),
+ Definition::Field(it) => label_and_layout_info_and_docs(db, it, |&it| {
+ let var_def = it.parent_def(db);
+ let id = it.index();
+ let layout = it.layout(db).ok()?;
+ let offset = match var_def {
+ hir::VariantDef::Struct(s) => {
+ let layout = Adt::from(s).layout(db).ok()?;
+ layout.fields.offset(id)
+ }
+ _ => return None,
+ };
+ Some(format!(
+ "size = {}, align = {}, offset = {}",
+ layout.size.bytes(),
+ layout.align.abi.bytes(),
+ offset.bytes()
+ ))
+ }),
Definition::Module(it) => label_and_docs(db, it),
Definition::Function(it) => label_and_docs(db, it),
- Definition::Adt(it) => label_and_docs(db, it),
+ Definition::Adt(it) => label_and_layout_info_and_docs(db, it, |&it| {
+ let layout = it.layout(db).ok()?;
+ Some(format!("size = {}, align = {}", layout.size.bytes(), layout.align.abi.bytes()))
+ }),
Definition::Variant(it) => label_value_and_docs(db, it, |&it| {
if !it.parent_enum(db).is_data_carrying(db) {
match it.eval(db) {
@@ -489,6 +511,25 @@ where
(label, docs)
}
+fn label_and_layout_info_and_docs<D, E, V>(
+ db: &RootDatabase,
+ def: D,
+ value_extractor: E,
+) -> (String, Option<hir::Documentation>)
+where
+ D: HasAttrs + HirDisplay,
+ E: Fn(&D) -> Option<V>,
+ V: Display,
+{
+ let label = if let Some(value) = value_extractor(&def) {
+ format!("{} // {}", def.display(db), value)
+ } else {
+ def.display(db).to_string()
+ };
+ let docs = def.attrs(db).docs();
+ (label, docs)
+}
+
fn label_value_and_docs<D, E, V>(
db: &RootDatabase,
def: D,