Skip to content

Commit 833d530

Browse files
committed
Auto merge of #14758 - lumenian:hover-layout-config, r=HKalbasi
Add config for disabling hover memory layout data Requested in #14748 (comment)
2 parents aaf670b + 8e1ba7f commit 833d530

File tree

7 files changed

+90
-15
lines changed

7 files changed

+90
-15
lines changed

crates/ide/src/hover.rs

+2-1
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,
@@ -226,7 +227,7 @@ fn hover_simple(
226227
return None;
227228
}
228229
let c = token.parent().and_then(|x| x.parent()).and_then(ast::ClosureExpr::cast)?;
229-
render::closure_expr(sema, c)
230+
render::closure_expr(sema, config, c)
230231
})
231232
});
232233

crates/ide/src/hover/render.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,17 @@ pub(super) fn type_info_of(
4343

4444
pub(super) fn closure_expr(
4545
sema: &Semantics<'_, RootDatabase>,
46+
config: &HoverConfig,
4647
c: ast::ClosureExpr,
4748
) -> Option<HoverResult> {
4849
let ty = &sema.type_of_expr(&c.into())?.original;
49-
let layout = ty
50-
.layout(sema.db)
51-
.map(|x| format!(" // size = {}, align = {}", x.size.bytes(), x.align.abi.bytes()))
52-
.unwrap_or_default();
50+
let layout = if config.memory_layout {
51+
ty.layout(sema.db)
52+
.map(|x| format!(" // size = {}, align = {}", x.size.bytes(), x.align.abi.bytes()))
53+
.unwrap_or_default()
54+
} else {
55+
String::default()
56+
};
5357
let c = ty.as_closure()?;
5458
let mut captures = c
5559
.captured_items(sema.db)
@@ -415,7 +419,7 @@ pub(super) fn definition(
415419
let mod_path = definition_mod_path(db, &def);
416420
let (label, docs) = match def {
417421
Definition::Macro(it) => label_and_docs(db, it),
418-
Definition::Field(it) => label_and_layout_info_and_docs(db, it, |&it| {
422+
Definition::Field(it) => label_and_layout_info_and_docs(db, it, config, |&it| {
419423
let var_def = it.parent_def(db);
420424
let id = it.index();
421425
let layout = it.layout(db).ok()?;
@@ -435,7 +439,7 @@ pub(super) fn definition(
435439
}),
436440
Definition::Module(it) => label_and_docs(db, it),
437441
Definition::Function(it) => label_and_docs(db, it),
438-
Definition::Adt(it) => label_and_layout_info_and_docs(db, it, |&it| {
442+
Definition::Adt(it) => label_and_layout_info_and_docs(db, it, config, |&it| {
439443
let layout = it.layout(db).ok()?;
440444
Some(format!("size = {}, align = {}", layout.size.bytes(), layout.align.abi.bytes()))
441445
}),
@@ -473,7 +477,7 @@ pub(super) fn definition(
473477
}),
474478
Definition::Trait(it) => label_and_docs(db, it),
475479
Definition::TraitAlias(it) => label_and_docs(db, it),
476-
Definition::TypeAlias(it) => label_and_layout_info_and_docs(db, it, |&it| {
480+
Definition::TypeAlias(it) => label_and_layout_info_and_docs(db, it, config, |&it| {
477481
let layout = it.ty(db).layout(db).ok()?;
478482
Some(format!("size = {}, align = {}", layout.size.bytes(), layout.align.abi.bytes()))
479483
}),
@@ -577,17 +581,17 @@ where
577581
fn label_and_layout_info_and_docs<D, E, V>(
578582
db: &RootDatabase,
579583
def: D,
584+
config: &HoverConfig,
580585
value_extractor: E,
581586
) -> (String, Option<hir::Documentation>)
582587
where
583588
D: HasAttrs + HirDisplay,
584589
E: Fn(&D) -> Option<V>,
585590
V: Display,
586591
{
587-
let label = if let Some(value) = value_extractor(&def) {
588-
format!("{} // {value}", def.display(db))
589-
} else {
590-
def.display(db).to_string()
592+
let label = match value_extractor(&def) {
593+
Some(value) if config.memory_layout => format!("{} // {value}", def.display(db)),
594+
_ => def.display(db).to_string(),
591595
};
592596
let docs = def.attrs(db).docs();
593597
(label, docs)

crates/ide/src/hover/tests.rs

+56
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,44 @@ pub fn fo$0o() {}
17451763
);
17461764
}
17471765

1766+
#[test]
1767+
fn test_hover_no_memory_layout() {
1768+
check_hover_no_memory_layout(
1769+
r#"struct Foo { fiel$0d_a: u8, field_b: i32, field_c: i16 }"#,
1770+
expect![[r#"
1771+
*field_a*
1772+
1773+
```rust
1774+
test::Foo
1775+
```
1776+
1777+
```rust
1778+
field_a: u8
1779+
```
1780+
"#]],
1781+
);
1782+
1783+
check_hover_no_memory_layout(
1784+
r#"
1785+
//- minicore: copy
1786+
fn main() {
1787+
let x = 2;
1788+
let y = $0|z| x + z;
1789+
}
1790+
"#,
1791+
expect![[r#"
1792+
*|*
1793+
```rust
1794+
{closure#0}
1795+
impl Fn(i32) -> i32
1796+
```
1797+
1798+
## Captures
1799+
* `x` by immutable borrow
1800+
"#]],
1801+
);
1802+
}
1803+
17481804
#[test]
17491805
fn test_hover_macro_generated_struct_fn_doc_comment() {
17501806
cov_mark::check!(hover_macro_generated_struct_fn_doc_comment);

crates/ide/src/static_index.rs

+1
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

+4-1
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_memoryLayout_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_memoryLayout_enable,
14751478
documentation: self.data.hover_documentation_enable,
14761479
format: {
14771480
let is_markdown = try_or_def!(self

docs/user/generated_config.adoc

+6-1
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.memoryLayout.enable]]rust-analyzer.hover.memoryLayout.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

+6-1
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.memoryLayout.enable": {
963+
"markdownDescription": "Whether to show memory layout data on hover.",
959964
"default": true,
960965
"type": "boolean"
961966
},

0 commit comments

Comments
 (0)