Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 4ed0fa8

Browse files
committed
Add config for disabling hover memory layout data
1 parent ecc081d commit 4ed0fa8

File tree

7 files changed

+63
-10
lines changed

7 files changed

+63
-10
lines changed

crates/ide/src/hover.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use crate::{
2727
#[derive(Clone, Debug, PartialEq, Eq)]
2828
pub struct HoverConfig {
2929
pub links_in_hover: bool,
30+
pub memory_layout: bool,
3031
pub documentation: bool,
3132
pub keywords: bool,
3233
pub format: HoverDocFormat,

crates/ide/src/hover/render.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ pub(super) fn definition(
415415
let mod_path = definition_mod_path(db, &def);
416416
let (label, docs) = match def {
417417
Definition::Macro(it) => label_and_docs(db, it),
418-
Definition::Field(it) => label_and_layout_info_and_docs(db, it, |&it| {
418+
Definition::Field(it) => label_and_layout_info_and_docs(db, it, config, |&it| {
419419
let var_def = it.parent_def(db);
420420
let id = it.index();
421421
let layout = it.layout(db).ok()?;
@@ -435,7 +435,7 @@ pub(super) fn definition(
435435
}),
436436
Definition::Module(it) => label_and_docs(db, it),
437437
Definition::Function(it) => label_and_docs(db, it),
438-
Definition::Adt(it) => label_and_layout_info_and_docs(db, it, |&it| {
438+
Definition::Adt(it) => label_and_layout_info_and_docs(db, it, config, |&it| {
439439
let layout = it.layout(db).ok()?;
440440
Some(format!("size = {}, align = {}", layout.size.bytes(), layout.align.abi.bytes()))
441441
}),
@@ -473,7 +473,7 @@ pub(super) fn definition(
473473
}),
474474
Definition::Trait(it) => label_and_docs(db, it),
475475
Definition::TraitAlias(it) => label_and_docs(db, it),
476-
Definition::TypeAlias(it) => label_and_layout_info_and_docs(db, it, |&it| {
476+
Definition::TypeAlias(it) => label_and_layout_info_and_docs(db, it, config, |&it| {
477477
let layout = it.ty(db).layout(db).ok()?;
478478
Some(format!("size = {}, align = {}", layout.size.bytes(), layout.align.abi.bytes()))
479479
}),
@@ -577,17 +577,17 @@ where
577577
fn label_and_layout_info_and_docs<D, E, V>(
578578
db: &RootDatabase,
579579
def: D,
580+
config: &HoverConfig,
580581
value_extractor: E,
581582
) -> (String, Option<hir::Documentation>)
582583
where
583584
D: HasAttrs + HirDisplay,
584585
E: Fn(&D) -> Option<V>,
585586
V: Display,
586587
{
587-
let label = if let Some(value) = value_extractor(&def) {
588-
format!("{} // {value}", def.display(db))
589-
} else {
590-
def.display(db).to_string()
588+
let label = match value_extractor(&def) {
589+
Some(value) if config.memory_layout => format!("{} // {value}", def.display(db)),
590+
_ => def.display(db).to_string(),
591591
};
592592
let docs = def.attrs(db).docs();
593593
(label, docs)

crates/ide/src/hover/tests.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::{fixture, HoverConfig, HoverDocFormat};
66

77
const HOVER_BASE_CONFIG: HoverConfig = HoverConfig {
88
links_in_hover: false,
9+
memory_layout: true,
910
documentation: true,
1011
format: HoverDocFormat::Markdown,
1112
keywords: true,
@@ -57,6 +58,23 @@ fn check_hover_no_links(ra_fixture: &str, expect: Expect) {
5758
expect.assert_eq(&actual)
5859
}
5960

61+
fn check_hover_no_memory_layout(ra_fixture: &str, expect: Expect) {
62+
let (analysis, position) = fixture::position(ra_fixture);
63+
let hover = analysis
64+
.hover(
65+
&HoverConfig { memory_layout: false, ..HOVER_BASE_CONFIG },
66+
FileRange { file_id: position.file_id, range: TextRange::empty(position.offset) },
67+
)
68+
.unwrap()
69+
.unwrap();
70+
71+
let content = analysis.db.file_text(position.file_id);
72+
let hovered_element = &content[hover.range];
73+
74+
let actual = format!("*{hovered_element}*\n{}\n", hover.info.markup);
75+
expect.assert_eq(&actual)
76+
}
77+
6078
fn check_hover_no_markdown(ra_fixture: &str, expect: Expect) {
6179
let (analysis, position) = fixture::position(ra_fixture);
6280
let hover = analysis
@@ -1745,6 +1763,26 @@ pub fn fo$0o() {}
17451763
);
17461764
}
17471765

1766+
#[test]
1767+
fn test_hover_no_memory_layout() {
1768+
check_hover_no_memory_layout(
1769+
r#"
1770+
struct Foo { fiel$0d_a: u8, field_b: i32, field_c: i16 }
1771+
"#,
1772+
expect![[r#"
1773+
*field_a*
1774+
1775+
```rust
1776+
test::Foo
1777+
```
1778+
1779+
```rust
1780+
field_a: u8
1781+
```
1782+
"#]],
1783+
);
1784+
}
1785+
17481786
#[test]
17491787
fn test_hover_macro_generated_struct_fn_doc_comment() {
17501788
cov_mark::check!(hover_macro_generated_struct_fn_doc_comment);

crates/ide/src/static_index.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ impl StaticIndex<'_> {
137137
});
138138
let hover_config = HoverConfig {
139139
links_in_hover: true,
140+
memory_layout: true,
140141
documentation: true,
141142
keywords: true,
142143
format: crate::HoverDocFormat::Markdown,

crates/rust-analyzer/src/config.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,10 @@ config_data! {
313313
/// Whether to show keyword hover popups. Only applies when
314314
/// `#rust-analyzer.hover.documentation.enable#` is set.
315315
hover_documentation_keywords_enable: bool = "true",
316-
/// Use markdown syntax for links in hover.
316+
/// Use markdown syntax for links on hover.
317317
hover_links_enable: bool = "true",
318+
/// Whether to show memory layout data on hover.
319+
hover_memory_layout_enable: bool = "true",
318320

319321
/// Whether to enforce the import granularity setting for all files. If set to false rust-analyzer will try to keep import styles consistent per file.
320322
imports_granularity_enforce: bool = "false",
@@ -1472,6 +1474,7 @@ impl Config {
14721474
pub fn hover(&self) -> HoverConfig {
14731475
HoverConfig {
14741476
links_in_hover: self.data.hover_links_enable,
1477+
memory_layout: self.data.hover_memory_layout_enable,
14751478
documentation: self.data.hover_documentation_enable,
14761479
format: {
14771480
let is_markdown = try_or_def!(self

docs/user/generated_config.adoc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,12 @@ Whether to show keyword hover popups. Only applies when
421421
[[rust-analyzer.hover.links.enable]]rust-analyzer.hover.links.enable (default: `true`)::
422422
+
423423
--
424-
Use markdown syntax for links in hover.
424+
Use markdown syntax for links on hover.
425+
--
426+
[[rust-analyzer.hover.memory.layout.enable]]rust-analyzer.hover.memory.layout.enable (default: `true`)::
427+
+
428+
--
429+
Whether to show memory layout data on hover.
425430
--
426431
[[rust-analyzer.imports.granularity.enforce]]rust-analyzer.imports.granularity.enforce (default: `false`)::
427432
+

editors/code/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,12 @@
955955
"type": "boolean"
956956
},
957957
"rust-analyzer.hover.links.enable": {
958-
"markdownDescription": "Use markdown syntax for links in hover.",
958+
"markdownDescription": "Use markdown syntax for links on hover.",
959+
"default": true,
960+
"type": "boolean"
961+
},
962+
"rust-analyzer.hover.memory.layout.enable": {
963+
"markdownDescription": "Whether to show memory layout data on hover.",
959964
"default": true,
960965
"type": "boolean"
961966
},

0 commit comments

Comments
 (0)