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

Commit e295f0c

Browse files
Insert whitespaces into static & const bodies if they are expanded from macro on hover
Macro expansion erases whitespace information, and so we end with invalid Rust code.
1 parent 989b09d commit e295f0c

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

crates/ide/src/hover/render.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
use std::fmt::Display;
33

44
use either::Either;
5-
use hir::{AsAssocItem, AttributeTemplate, HasAttrs, HirDisplay, Semantics, TypeInfo};
5+
use hir::{AsAssocItem, AttributeTemplate, HasAttrs, HasSource, HirDisplay, Semantics, TypeInfo};
66
use ide_db::{
77
base_db::SourceDatabase,
88
defs::Definition,
99
famous_defs::FamousDefs,
1010
generated::lints::{CLIPPY_LINTS, DEFAULT_LINTS, FEATURES},
11+
syntax_helpers::insert_whitespace_into_node,
1112
RootDatabase,
1213
};
1314
use itertools::Itertools;
@@ -350,10 +351,24 @@ pub(super) fn definition(
350351
let body = it.eval(db);
351352
match body {
352353
Ok(x) => Some(format!("{}", x)),
353-
Err(_) => it.value(db).map(|x| format!("{}", x)),
354+
Err(_) => {
355+
let source = it.source(db)?;
356+
let mut body = source.value.body()?.syntax().clone();
357+
if source.file_id.is_macro() {
358+
body = insert_whitespace_into_node::insert_ws_into(body);
359+
}
360+
Some(body.to_string())
361+
}
362+
}
363+
}),
364+
Definition::Static(it) => label_value_and_docs(db, it, |it| {
365+
let source = it.source(db)?;
366+
let mut body = source.value.body()?.syntax().clone();
367+
if source.file_id.is_macro() {
368+
body = insert_whitespace_into_node::insert_ws_into(body);
354369
}
370+
Some(body.to_string())
355371
}),
356-
Definition::Static(it) => label_value_and_docs(db, it, |it| it.value(db)),
357372
Definition::Trait(it) => label_and_docs(db, it),
358373
Definition::TypeAlias(it) => label_and_docs(db, it),
359374
Definition::BuiltinType(it) => {

crates/ide/src/hover/tests.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5113,3 +5113,62 @@ fn f() {
51135113
"#]],
51145114
);
51155115
}
5116+
5117+
#[test]
5118+
fn static_const_macro_expanded_body() {
5119+
check(
5120+
r#"
5121+
macro_rules! m {
5122+
() => {
5123+
pub const V: i8 = {
5124+
let e = 123;
5125+
f(e) // Prevent const eval from evaluating this constant, we want to print the body's code.
5126+
};
5127+
};
5128+
}
5129+
m!();
5130+
fn main() { $0V; }
5131+
"#,
5132+
expect![[r#"
5133+
*V*
5134+
5135+
```rust
5136+
test
5137+
```
5138+
5139+
```rust
5140+
pub const V: i8 = {
5141+
let e = 123;
5142+
f(e)
5143+
}
5144+
```
5145+
"#]],
5146+
);
5147+
check(
5148+
r#"
5149+
macro_rules! m {
5150+
() => {
5151+
pub static V: i8 = {
5152+
let e = 123;
5153+
};
5154+
};
5155+
}
5156+
m!();
5157+
fn main() { $0V; }
5158+
"#,
5159+
expect![[r#"
5160+
*V*
5161+
5162+
```rust
5163+
test
5164+
```
5165+
5166+
```rust
5167+
pub static V: i8 = {
5168+
let e = 123;
5169+
5170+
}
5171+
```
5172+
"#]],
5173+
);
5174+
}

0 commit comments

Comments
 (0)