mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-31 23:38:45 +00:00
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.
This commit is contained in:
parent
989b09d20c
commit
e295f0c29c
2 changed files with 77 additions and 3 deletions
|
@ -2,12 +2,13 @@
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
|
||||||
use either::Either;
|
use either::Either;
|
||||||
use hir::{AsAssocItem, AttributeTemplate, HasAttrs, HirDisplay, Semantics, TypeInfo};
|
use hir::{AsAssocItem, AttributeTemplate, HasAttrs, HasSource, HirDisplay, Semantics, TypeInfo};
|
||||||
use ide_db::{
|
use ide_db::{
|
||||||
base_db::SourceDatabase,
|
base_db::SourceDatabase,
|
||||||
defs::Definition,
|
defs::Definition,
|
||||||
famous_defs::FamousDefs,
|
famous_defs::FamousDefs,
|
||||||
generated::lints::{CLIPPY_LINTS, DEFAULT_LINTS, FEATURES},
|
generated::lints::{CLIPPY_LINTS, DEFAULT_LINTS, FEATURES},
|
||||||
|
syntax_helpers::insert_whitespace_into_node,
|
||||||
RootDatabase,
|
RootDatabase,
|
||||||
};
|
};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
@ -350,10 +351,24 @@ pub(super) fn definition(
|
||||||
let body = it.eval(db);
|
let body = it.eval(db);
|
||||||
match body {
|
match body {
|
||||||
Ok(x) => Some(format!("{}", x)),
|
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| it.value(db)),
|
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::Trait(it) => label_and_docs(db, it),
|
Definition::Trait(it) => label_and_docs(db, it),
|
||||||
Definition::TypeAlias(it) => label_and_docs(db, it),
|
Definition::TypeAlias(it) => label_and_docs(db, it),
|
||||||
Definition::BuiltinType(it) => {
|
Definition::BuiltinType(it) => {
|
||||||
|
|
|
@ -5113,3 +5113,62 @@ 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;
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
"#]],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue