mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 05:15:04 +00:00
Merge #10796
10796: ide: display static values in hover r=Veykril a=jhgg Continuation from #10785 - does the same thing, but for `static`'s as well. Co-authored-by: Jake Heinz <jh@discordapp.com>
This commit is contained in:
commit
bf8cf09967
3 changed files with 30 additions and 20 deletions
|
@ -1497,6 +1497,10 @@ impl Static {
|
||||||
db.static_data(self.id).mutable
|
db.static_data(self.id).mutable
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn value(self, db: &dyn HirDatabase) -> Option<ast::Expr> {
|
||||||
|
self.source(db)?.value.body()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn ty(self, db: &dyn HirDatabase) -> Type {
|
pub fn ty(self, db: &dyn HirDatabase) -> Type {
|
||||||
let data = db.static_data(self.id);
|
let data = db.static_data(self.id);
|
||||||
let resolver = self.id.resolver(db.upcast());
|
let resolver = self.id.resolver(db.upcast());
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
//! Logic for rendering the different hover messages
|
//! Logic for rendering the different hover messages
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
use either::Either;
|
use either::Either;
|
||||||
use hir::{AsAssocItem, Const, HasAttrs, HasSource, HirDisplay, Semantics, TypeInfo};
|
use hir::{AsAssocItem, HasAttrs, HasSource, HirDisplay, Semantics, TypeInfo};
|
||||||
use ide_db::{
|
use ide_db::{
|
||||||
base_db::SourceDatabase,
|
base_db::SourceDatabase,
|
||||||
defs::Definition,
|
defs::Definition,
|
||||||
|
@ -352,8 +354,8 @@ pub(super) fn definition(
|
||||||
Definition::Function(it) => label_and_docs(db, it),
|
Definition::Function(it) => label_and_docs(db, it),
|
||||||
Definition::Adt(it) => label_and_docs(db, it),
|
Definition::Adt(it) => label_and_docs(db, it),
|
||||||
Definition::Variant(it) => label_and_docs(db, it),
|
Definition::Variant(it) => label_and_docs(db, it),
|
||||||
Definition::Const(it) => const_label_value_and_docs(db, it),
|
Definition::Const(it) => label_value_and_docs(db, it, |it| it.value(db)),
|
||||||
Definition::Static(it) => label_and_docs(db, it),
|
Definition::Static(it) => label_value_and_docs(db, it, |it| it.value(db)),
|
||||||
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) => {
|
||||||
|
@ -381,18 +383,22 @@ where
|
||||||
(label, docs)
|
(label, docs)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn const_label_value_and_docs(
|
fn label_value_and_docs<D, E, V>(
|
||||||
db: &RootDatabase,
|
db: &RootDatabase,
|
||||||
konst: Const,
|
def: D,
|
||||||
) -> (String, Option<hir::Documentation>) {
|
value_extractor: E,
|
||||||
let label = if let Some(expr) = konst.value(db) {
|
) -> (String, Option<hir::Documentation>)
|
||||||
format!("{} = {}", konst.display(db), expr)
|
where
|
||||||
|
D: HasAttrs + HirDisplay,
|
||||||
|
E: Fn(&D) -> Option<V>,
|
||||||
|
V: Display,
|
||||||
|
{
|
||||||
|
let label = if let Some(value) = (value_extractor)(&def) {
|
||||||
|
format!("{} = {}", def.display(db), value)
|
||||||
} else {
|
} else {
|
||||||
konst.display(db).to_string()
|
def.display(db).to_string()
|
||||||
};
|
};
|
||||||
|
let docs = def.attrs(db).docs();
|
||||||
let docs = konst.attrs(db).docs();
|
|
||||||
|
|
||||||
(label, docs)
|
(label, docs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -539,16 +539,16 @@ const foo$0: u32 = {
|
||||||
check(
|
check(
|
||||||
r#"static foo$0: u32 = 456;"#,
|
r#"static foo$0: u32 = 456;"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
*foo*
|
*foo*
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
test
|
test
|
||||||
```
|
```
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
static foo: u32
|
static foo: u32 = 456
|
||||||
```
|
```
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue