Merge #10785
10785: ide: show const value in hover r=jhgg a=jhgg
fixes #10783
I think my original attempt was incorrect, because it looks like `HirDisplay` is used in more places than just the hover.
So, I've attempted it again in 312eafe
, this time specifically just rendering the value in `hover::render`
pictoral:
![image](https://user-images.githubusercontent.com/5489149/142163890-b6aa2ab4-7bd0-4dd3-b35d-5eaa83fffb7f.png)
Co-authored-by: Jake Heinz <jh@discordapp.com>
Co-authored-by: Jake <jh@discordapp.com>
This commit is contained in:
commit
64a73dcfba
@ -1457,6 +1457,10 @@ pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
|
||||
db.const_data(self.id).name.clone()
|
||||
}
|
||||
|
||||
pub fn value(self, db: &dyn HirDatabase) -> Option<ast::Expr> {
|
||||
self.source(db)?.value.body()
|
||||
}
|
||||
|
||||
pub fn ty(self, db: &dyn HirDatabase) -> Type {
|
||||
let data = db.const_data(self.id);
|
||||
let resolver = self.id.resolver(db.upcast());
|
||||
|
@ -1,6 +1,6 @@
|
||||
//! Logic for rendering the different hover messages
|
||||
use either::Either;
|
||||
use hir::{AsAssocItem, HasAttrs, HasSource, HirDisplay, Semantics, TypeInfo};
|
||||
use hir::{AsAssocItem, Const, HasAttrs, HasSource, HirDisplay, Semantics, TypeInfo};
|
||||
use ide_db::{
|
||||
base_db::SourceDatabase,
|
||||
defs::Definition,
|
||||
@ -352,7 +352,7 @@ pub(super) fn definition(
|
||||
Definition::Function(it) => label_and_docs(db, it),
|
||||
Definition::Adt(it) => label_and_docs(db, it),
|
||||
Definition::Variant(it) => label_and_docs(db, it),
|
||||
Definition::Const(it) => label_and_docs(db, it),
|
||||
Definition::Const(it) => const_label_value_and_docs(db, it),
|
||||
Definition::Static(it) => label_and_docs(db, it),
|
||||
Definition::Trait(it) => label_and_docs(db, it),
|
||||
Definition::TypeAlias(it) => label_and_docs(db, it),
|
||||
@ -381,6 +381,21 @@ fn label_and_docs<D>(db: &RootDatabase, def: D) -> (String, Option<hir::Document
|
||||
(label, docs)
|
||||
}
|
||||
|
||||
fn const_label_value_and_docs(
|
||||
db: &RootDatabase,
|
||||
konst: Const,
|
||||
) -> (String, Option<hir::Documentation>) {
|
||||
let label = if let Some(expr) = konst.value(db) {
|
||||
format!("{} = {}", konst.display(db), expr)
|
||||
} else {
|
||||
konst.display(db).to_string()
|
||||
};
|
||||
|
||||
let docs = konst.attrs(db).docs();
|
||||
|
||||
(label, docs)
|
||||
}
|
||||
|
||||
fn definition_mod_path(db: &RootDatabase, def: &Definition) -> Option<String> {
|
||||
if let Definition::GenericParam(_) = def {
|
||||
return None;
|
||||
|
@ -510,10 +510,32 @@ fn hover_const_static() {
|
||||
```
|
||||
|
||||
```rust
|
||||
const foo: u32
|
||||
const foo: u32 = 123
|
||||
```
|
||||
"#]],
|
||||
);
|
||||
check(
|
||||
r#"
|
||||
const foo$0: u32 = {
|
||||
let x = foo();
|
||||
x + 100
|
||||
};"#,
|
||||
expect![[r#"
|
||||
*foo*
|
||||
|
||||
```rust
|
||||
test
|
||||
```
|
||||
|
||||
```rust
|
||||
const foo: u32 = {
|
||||
let x = foo();
|
||||
x + 100
|
||||
}
|
||||
```
|
||||
"#]],
|
||||
);
|
||||
|
||||
check(
|
||||
r#"static foo$0: u32 = 456;"#,
|
||||
expect![[r#"
|
||||
@ -795,7 +817,7 @@ fn main() {
|
||||
```
|
||||
|
||||
```rust
|
||||
const C: u32
|
||||
const C: u32 = 1
|
||||
```
|
||||
"#]],
|
||||
)
|
||||
@ -3183,7 +3205,7 @@ fn foo() {
|
||||
```
|
||||
|
||||
```rust
|
||||
const FOO: usize
|
||||
const FOO: usize = 3
|
||||
```
|
||||
|
||||
---
|
||||
|
Loading…
Reference in New Issue
Block a user