hir: show const value in hover

This commit is contained in:
Jake Heinz 2021-11-17 05:49:27 +00:00
parent add6cccd4c
commit 4fbc4b9356
3 changed files with 42 additions and 28 deletions

View File

@ -401,7 +401,8 @@ fn write_where_clause(def: GenericDefId, f: &mut HirFormatter) -> Result<(), Hir
impl HirDisplay for Const {
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
write_visibility(self.module(f.db).id, self.visibility(f.db), f)?;
let module_id = self.module(f.db).id;
write_visibility(module_id, self.visibility(f.db), f)?;
let data = f.db.const_data(self.id);
write!(f, "const ")?;
match &data.name {
@ -409,6 +410,15 @@ impl HirDisplay for Const {
None => write!(f, "_: ")?,
}
data.type_ref.hir_fmt(f)?;
let ast_id_map = f.db.ast_id_map(data.file_id);
let ast_node = ast_id_map.get(data.ast_id);
if let Some(syntax_node) = f.db.parse_or_expand(data.file_id) {
let ast_node = ast_node.to_node(&syntax_node);
if let Some(body) = ast_node.body() {
write!(f, " = {}", body)?;
}
}
Ok(())
}
}

View File

@ -2,7 +2,7 @@
use std::sync::Arc;
use hir_expand::{name::Name, InFile};
use hir_expand::{ast_id_map::FileAstId, name::Name, HirFileId, InFile};
use syntax::ast;
use crate::{
@ -255,6 +255,8 @@ pub struct ConstData {
pub name: Option<Name>,
pub type_ref: Interned<TypeRef>,
pub visibility: RawVisibility,
pub ast_id: FileAstId<ast::Const>,
pub file_id: HirFileId,
}
impl ConstData {
@ -267,6 +269,8 @@ impl ConstData {
name: konst.name.clone(),
type_ref: konst.type_ref.clone(),
visibility: item_tree[konst.visibility].clone(),
ast_id: konst.ast_id.clone(),
file_id: loc.id.file_id(),
})
}
}

View File

@ -503,16 +503,16 @@ fn hover_const_static() {
check(
r#"const foo$0: u32 = 123;"#,
expect![[r#"
*foo*
*foo*
```rust
test
```
```rust
test
```
```rust
const foo: u32
```
"#]],
```rust
const foo: u32 = 123
```
"#]],
);
check(
r#"static foo$0: u32 = 456;"#,
@ -788,16 +788,16 @@ fn main() {
}
"#,
expect![[r#"
*C*
*C*
```rust
test
```
```rust
test
```
```rust
const C: u32
```
"#]],
```rust
const C: u32 = 1
```
"#]],
)
}
@ -3176,20 +3176,20 @@ fn foo() {
}
"#,
expect![[r#"
*FOO*
*FOO*
```rust
test
```
```rust
test
```
```rust
const FOO: usize
```
```rust
const FOO: usize = 3
```
---
---
This is a doc
"#]],
This is a doc
"#]],
);
}