mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 05:15:04 +00:00
hir: show const value in hover
This commit is contained in:
parent
add6cccd4c
commit
4fbc4b9356
3 changed files with 42 additions and 28 deletions
|
@ -401,7 +401,8 @@ fn write_where_clause(def: GenericDefId, f: &mut HirFormatter) -> Result<(), Hir
|
||||||
|
|
||||||
impl HirDisplay for Const {
|
impl HirDisplay for Const {
|
||||||
fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
|
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);
|
let data = f.db.const_data(self.id);
|
||||||
write!(f, "const ")?;
|
write!(f, "const ")?;
|
||||||
match &data.name {
|
match &data.name {
|
||||||
|
@ -409,6 +410,15 @@ impl HirDisplay for Const {
|
||||||
None => write!(f, "_: ")?,
|
None => write!(f, "_: ")?,
|
||||||
}
|
}
|
||||||
data.type_ref.hir_fmt(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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
use std::sync::Arc;
|
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 syntax::ast;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -255,6 +255,8 @@ pub struct ConstData {
|
||||||
pub name: Option<Name>,
|
pub name: Option<Name>,
|
||||||
pub type_ref: Interned<TypeRef>,
|
pub type_ref: Interned<TypeRef>,
|
||||||
pub visibility: RawVisibility,
|
pub visibility: RawVisibility,
|
||||||
|
pub ast_id: FileAstId<ast::Const>,
|
||||||
|
pub file_id: HirFileId,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConstData {
|
impl ConstData {
|
||||||
|
@ -267,6 +269,8 @@ impl ConstData {
|
||||||
name: konst.name.clone(),
|
name: konst.name.clone(),
|
||||||
type_ref: konst.type_ref.clone(),
|
type_ref: konst.type_ref.clone(),
|
||||||
visibility: item_tree[konst.visibility].clone(),
|
visibility: item_tree[konst.visibility].clone(),
|
||||||
|
ast_id: konst.ast_id.clone(),
|
||||||
|
file_id: loc.id.file_id(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -510,7 +510,7 @@ fn hover_const_static() {
|
||||||
```
|
```
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
const foo: u32
|
const foo: u32 = 123
|
||||||
```
|
```
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
|
@ -795,7 +795,7 @@ fn main() {
|
||||||
```
|
```
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
const C: u32
|
const C: u32 = 1
|
||||||
```
|
```
|
||||||
"#]],
|
"#]],
|
||||||
)
|
)
|
||||||
|
@ -3183,7 +3183,7 @@ fn foo() {
|
||||||
```
|
```
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
const FOO: usize
|
const FOO: usize = 3
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue