Skip to content

Commit 5be2e65

Browse files
committed
Auto merge of rust-lang#13185 - ChayimFriedman2:insert-ws-in-static-const-macro, r=Veykril
fix: Insert whitespaces into static & const bodies if they are expanded from macro on hover Partially fixes rust-lang#13143. To resolve the other part we need to expand macros in unevaluated static & const bodies, and I'm not sure we want to. If for example it includes a call to `assert!()`, expanding it will lead to worse hover.
2 parents 4790916 + 26b5f1f commit 5be2e65

File tree

3 files changed

+77
-4
lines changed

3 files changed

+77
-4
lines changed

crates/ide-db/src/syntax_helpers/insert_whitespace_into_node.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub fn insert_ws_into(syn: SyntaxNode) -> SyntaxNode {
9595
AS_KW | DYN_KW | IMPL_KW | CONST_KW => {
9696
mods.push(do_ws(after, tok));
9797
}
98-
T![;] => {
98+
T![;] if is_next(|it| it != R_CURLY, true) => {
9999
if indent > 0 {
100100
mods.push(do_indent(after, tok, indent));
101101
}

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: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5113,3 +5113,61 @@ 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+
}

0 commit comments

Comments
 (0)