feat: preview adt field when hover

This commit is contained in:
YangzeLuo 2023-11-08 21:45:32 +08:00
parent 69432287cb
commit 41bcd542e2
2 changed files with 94 additions and 26 deletions

View File

@ -1,6 +1,6 @@
//! HirDisplay implementations for various hir types. //! HirDisplay implementations for various hir types.
use hir_def::{ use hir_def::{
data::adt::VariantData, data::adt::{StructKind, VariantData},
generics::{ generics::{
TypeOrConstParamData, TypeParamProvenance, WherePredicate, WherePredicateTypeTarget, TypeOrConstParamData, TypeParamProvenance, WherePredicate, WherePredicateTypeTarget,
}, },
@ -163,7 +163,40 @@ fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
write!(f, "{}", self.name(f.db).display(f.db.upcast()))?; write!(f, "{}", self.name(f.db).display(f.db.upcast()))?;
let def_id = GenericDefId::AdtId(AdtId::StructId(self.id)); let def_id = GenericDefId::AdtId(AdtId::StructId(self.id));
write_generic_params(def_id, f)?; write_generic_params(def_id, f)?;
let variant_data = self.variant_data(f.db);
if let StructKind::Tuple = variant_data.kind() {
f.write_char('(')?;
let mut it = variant_data.fields().iter().peekable();
while let Some((id, _)) = it.next() {
let field = Field { parent: (*self).into(), id };
field.ty(f.db).hir_fmt(f)?;
if it.peek().is_some() {
f.write_str(", ")?;
}
}
f.write_str(");")?;
}
write_where_clause(def_id, f)?; write_where_clause(def_id, f)?;
if let StructKind::Record = variant_data.kind() {
let fields = self.fields(f.db);
if fields.is_empty() {
f.write_str(" {}")?;
} else {
f.write_str(" {\n")?;
for field in self.fields(f.db) {
f.write_str(" ")?;
field.hir_fmt(f)?;
f.write_str(",\n")?;
}
f.write_str("}")?;
}
}
Ok(()) Ok(())
} }
} }
@ -176,6 +209,18 @@ fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
let def_id = GenericDefId::AdtId(AdtId::EnumId(self.id)); let def_id = GenericDefId::AdtId(AdtId::EnumId(self.id));
write_generic_params(def_id, f)?; write_generic_params(def_id, f)?;
write_where_clause(def_id, f)?; write_where_clause(def_id, f)?;
let variants = self.variants(f.db);
if !variants.is_empty() {
f.write_str(" {\n")?;
for variant in variants {
f.write_str(" ")?;
variant.hir_fmt(f)?;
f.write_str(",\n")?;
}
f.write_str("}")?;
}
Ok(()) Ok(())
} }
} }
@ -188,6 +233,18 @@ fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
let def_id = GenericDefId::AdtId(AdtId::UnionId(self.id)); let def_id = GenericDefId::AdtId(AdtId::UnionId(self.id));
write_generic_params(def_id, f)?; write_generic_params(def_id, f)?;
write_where_clause(def_id, f)?; write_where_clause(def_id, f)?;
let fields = self.fields(f.db);
if !fields.is_empty() {
f.write_str(" {\n")?;
for field in self.fields(f.db) {
f.write_str(" ")?;
field.hir_fmt(f)?;
f.write_str(",\n")?;
}
f.write_str("}")?;
}
Ok(()) Ok(())
} }
} }

View File

@ -1136,7 +1136,9 @@ fn new() -> Self { Self$0 { x: 0 } }
``` ```
```rust ```rust
struct Thing struct Thing {
x: u32,
}
``` ```
"#]], "#]],
); );
@ -1155,7 +1157,9 @@ fn new() -> Self$0 { Self { x: 0 } }
``` ```
```rust ```rust
struct Thing struct Thing {
x: u32,
}
``` ```
"#]], "#]],
); );
@ -1174,7 +1178,9 @@ pub fn new() -> Self$0 { Thing::A }
``` ```
```rust ```rust
enum Thing enum Thing {
A,
}
``` ```
"#]], "#]],
); );
@ -1193,7 +1199,9 @@ pub fn thing(a: Self$0) {}
``` ```
```rust ```rust
enum Thing enum Thing {
A,
}
``` ```
"#]], "#]],
); );
@ -2005,7 +2013,10 @@ fn test_hover_layout_of_enum() {
``` ```
```rust ```rust
enum Foo // size = 16 (0x10), align = 8, niches = 254 enum Foo {
Variant1(u8, u16),
Variant2(i32, u8, i64),
} // size = 16 (0x10), align = 8, niches = 254
``` ```
"#]], "#]],
); );
@ -2346,7 +2357,7 @@ struct S{ f1: u32 }
focus_range: 7..8, focus_range: 7..8,
name: "S", name: "S",
kind: Struct, kind: Struct,
description: "struct S", description: "struct S {\n f1: u32,\n}",
}, },
}, },
], ],
@ -2379,7 +2390,7 @@ struct S<T>{ f1: T }
focus_range: 24..25, focus_range: 24..25,
name: "S", name: "S",
kind: Struct, kind: Struct,
description: "struct S<T>", description: "struct S<T> {\n f1: T,\n}",
}, },
}, },
HoverGotoTypeData { HoverGotoTypeData {
@ -2392,7 +2403,7 @@ struct S<T>{ f1: T }
focus_range: 7..10, focus_range: 7..10,
name: "Arg", name: "Arg",
kind: Struct, kind: Struct,
description: "struct Arg", description: "struct Arg(u32);",
}, },
}, },
], ],
@ -2438,7 +2449,7 @@ struct S<T>{ f1: T }
focus_range: 24..25, focus_range: 24..25,
name: "S", name: "S",
kind: Struct, kind: Struct,
description: "struct S<T>", description: "struct S<T> {\n f1: T,\n}",
}, },
}, },
HoverGotoTypeData { HoverGotoTypeData {
@ -2451,7 +2462,7 @@ struct S<T>{ f1: T }
focus_range: 7..10, focus_range: 7..10,
name: "Arg", name: "Arg",
kind: Struct, kind: Struct,
description: "struct Arg", description: "struct Arg(u32);",
}, },
}, },
], ],
@ -2487,7 +2498,7 @@ mod M {
focus_range: 7..8, focus_range: 7..8,
name: "A", name: "A",
kind: Struct, kind: Struct,
description: "struct A", description: "struct A(u32);",
}, },
}, },
HoverGotoTypeData { HoverGotoTypeData {
@ -2500,7 +2511,7 @@ mod M {
focus_range: 22..23, focus_range: 22..23,
name: "B", name: "B",
kind: Struct, kind: Struct,
description: "struct B", description: "struct B(u32);",
}, },
}, },
HoverGotoTypeData { HoverGotoTypeData {
@ -2514,7 +2525,7 @@ mod M {
name: "C", name: "C",
kind: Struct, kind: Struct,
container_name: "M", container_name: "M",
description: "pub struct C", description: "pub struct C(u32);",
}, },
}, },
], ],
@ -2704,7 +2715,7 @@ fn foo() -> impl Foo<S1> + Bar<S2> {}
focus_range: 39..41, focus_range: 39..41,
name: "S1", name: "S1",
kind: Struct, kind: Struct,
description: "struct S1", description: "struct S1 {}",
}, },
}, },
HoverGotoTypeData { HoverGotoTypeData {
@ -2717,7 +2728,7 @@ fn foo() -> impl Foo<S1> + Bar<S2> {}
focus_range: 52..54, focus_range: 52..54,
name: "S2", name: "S2",
kind: Struct, kind: Struct,
description: "struct S2", description: "struct S2 {}",
}, },
}, },
], ],
@ -2808,7 +2819,7 @@ fn foo(ar$0g: &impl Foo + Bar<S>) {}
focus_range: 36..37, focus_range: 36..37,
name: "S", name: "S",
kind: Struct, kind: Struct,
description: "struct S", description: "struct S {}",
}, },
}, },
], ],
@ -2908,7 +2919,7 @@ fn foo(ar$0g: &impl Foo<S>) {}
focus_range: 23..24, focus_range: 23..24,
name: "S", name: "S",
kind: Struct, kind: Struct,
description: "struct S", description: "struct S {}",
}, },
}, },
], ],
@ -2945,7 +2956,7 @@ fn foo() -> B<dyn Foo> {}
focus_range: 49..50, focus_range: 49..50,
name: "B", name: "B",
kind: Struct, kind: Struct,
description: "struct B<T>", description: "struct B<T> {}",
}, },
}, },
HoverGotoTypeData { HoverGotoTypeData {
@ -3034,7 +3045,7 @@ fn foo(ar$0g: &dyn Foo<S>) {}
focus_range: 23..24, focus_range: 23..24,
name: "S", name: "S",
kind: Struct, kind: Struct,
description: "struct S", description: "struct S {}",
}, },
}, },
], ],
@ -3082,7 +3093,7 @@ fn foo(a$0rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {}
focus_range: 50..51, focus_range: 50..51,
name: "B", name: "B",
kind: Struct, kind: Struct,
description: "struct B<T>", description: "struct B<T> {}",
}, },
}, },
HoverGotoTypeData { HoverGotoTypeData {
@ -3108,7 +3119,7 @@ fn foo(a$0rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {}
focus_range: 65..66, focus_range: 65..66,
name: "S", name: "S",
kind: Struct, kind: Struct,
description: "struct S", description: "struct S {}",
}, },
}, },
], ],
@ -3335,7 +3346,7 @@ fn const_generic_order() {
``` ```
```rust ```rust
struct ST<const C: usize = 1, T = Foo> struct ST<const C: usize = 1, T = Foo>(T);
``` ```
"#]], "#]],
); );
@ -3356,7 +3367,7 @@ fn const_generic_default_value() {
``` ```
```rust ```rust
struct ST<const C: usize = {const}, T = Foo> struct ST<const C: usize = {const}, T = Foo>(T);
``` ```
"#]], "#]],
); );
@ -3378,7 +3389,7 @@ fn const_generic_default_value_2() {
``` ```
```rust ```rust
struct ST<const C: usize = VAL, T = Foo> struct ST<const C: usize = VAL, T = Foo>(T);
``` ```
"#]], "#]],
); );
@ -5935,7 +5946,7 @@ fn hover_intra_in_attr() {
``` ```
```rust ```rust
pub struct Foo // size = 4, align = 4 pub struct Foo(i32); // size = 4, align = 4
``` ```
--- ---