Skip to content

Commit

Permalink
Auto merge of rust-lang#13185 - ChayimFriedman2:insert-ws-in-static-c…
Browse files Browse the repository at this point in the history
…onst-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.
  • Loading branch information
bors committed Sep 5, 2022
2 parents 4790916 + 26b5f1f commit 5be2e65
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub fn insert_ws_into(syn: SyntaxNode) -> SyntaxNode {
AS_KW | DYN_KW | IMPL_KW | CONST_KW => {
mods.push(do_ws(after, tok));
}
T![;] => {
T![;] if is_next(|it| it != R_CURLY, true) => {
if indent > 0 {
mods.push(do_indent(after, tok, indent));
}
Expand Down
21 changes: 18 additions & 3 deletions crates/ide/src/hover/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
use std::fmt::Display;

use either::Either;
use hir::{AsAssocItem, AttributeTemplate, HasAttrs, HirDisplay, Semantics, TypeInfo};
use hir::{AsAssocItem, AttributeTemplate, HasAttrs, HasSource, HirDisplay, Semantics, TypeInfo};
use ide_db::{
base_db::SourceDatabase,
defs::Definition,
famous_defs::FamousDefs,
generated::lints::{CLIPPY_LINTS, DEFAULT_LINTS, FEATURES},
syntax_helpers::insert_whitespace_into_node,
RootDatabase,
};
use itertools::Itertools;
Expand Down Expand Up @@ -350,10 +351,24 @@ pub(super) fn definition(
let body = it.eval(db);
match body {
Ok(x) => Some(format!("{}", x)),
Err(_) => it.value(db).map(|x| format!("{}", x)),
Err(_) => {
let source = it.source(db)?;
let mut body = source.value.body()?.syntax().clone();
if source.file_id.is_macro() {
body = insert_whitespace_into_node::insert_ws_into(body);
}
Some(body.to_string())
}
}
}),
Definition::Static(it) => label_value_and_docs(db, it, |it| {
let source = it.source(db)?;
let mut body = source.value.body()?.syntax().clone();
if source.file_id.is_macro() {
body = insert_whitespace_into_node::insert_ws_into(body);
}
Some(body.to_string())
}),
Definition::Static(it) => label_value_and_docs(db, it, |it| it.value(db)),
Definition::Trait(it) => label_and_docs(db, it),
Definition::TypeAlias(it) => label_and_docs(db, it),
Definition::BuiltinType(it) => {
Expand Down
58 changes: 58 additions & 0 deletions crates/ide/src/hover/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5113,3 +5113,61 @@ fn f() {
"#]],
);
}

#[test]
fn static_const_macro_expanded_body() {
check(
r#"
macro_rules! m {
() => {
pub const V: i8 = {
let e = 123;
f(e) // Prevent const eval from evaluating this constant, we want to print the body's code.
};
};
}
m!();
fn main() { $0V; }
"#,
expect![[r#"
*V*
```rust
test
```
```rust
pub const V: i8 = {
let e = 123;
f(e)
}
```
"#]],
);
check(
r#"
macro_rules! m {
() => {
pub static V: i8 = {
let e = 123;
};
};
}
m!();
fn main() { $0V; }
"#,
expect![[r#"
*V*
```rust
test
```
```rust
pub static V: i8 = {
let e = 123;
}
```
"#]],
);
}

0 comments on commit 5be2e65

Please sign in to comment.