From cd49553c309d627edbcd8b5ef8866e514fffb3d6 Mon Sep 17 00:00:00 2001 From: YangzeLuo Date: Wed, 8 Nov 2023 01:35:03 +0800 Subject: [PATCH 1/3] feat: preview adt field when hover --- crates/hir/src/display.rs | 57 +++++++++++++++++++++++++++++++++++ crates/ide/src/hover/tests.rs | 42 ++++++++++++++++++-------- 2 files changed, 86 insertions(+), 13 deletions(-) diff --git a/crates/hir/src/display.rs b/crates/hir/src/display.rs index ac171026d5..4717ed24f8 100644 --- a/crates/hir/src/display.rs +++ b/crates/hir/src/display.rs @@ -163,7 +163,40 @@ impl HirDisplay for Struct { write!(f, "{}", self.name(f.db).display(f.db.upcast()))?; let def_id = GenericDefId::AdtId(AdtId::StructId(self.id)); write_generic_params(def_id, f)?; + + let field_is_tuple_index = + self.variant_data(f.db).fields().iter().any(|(_, f)| f.name.as_tuple_index().is_some()); + + if field_is_tuple_index { + f.write_char('(')?; + let variant_data = self.variant_data(f.db); + 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(");")?; + return Ok(()); + } + write_where_clause(def_id, f)?; + + let fields = self.fields(f.db); + if !fields.is_empty() { + f.write_str("\n{\n")?; + for field in self.fields(f.db) { + f.write_str(" ")?; + field.hir_fmt(f)?; + f.write_str(",\n")?; + } + f.write_str("}")?; + } + Ok(()) } } @@ -176,6 +209,18 @@ impl HirDisplay for Enum { let def_id = GenericDefId::AdtId(AdtId::EnumId(self.id)); write_generic_params(def_id, f)?; write_where_clause(def_id, f)?; + + let variants = self.variants(f.db); + if !variants.is_empty() { + f.write_str("\n{\n")?; + for variant in variants { + f.write_str(" ")?; + variant.hir_fmt(f)?; + f.write_str(",\n")?; + } + f.write_str("}")?; + } + Ok(()) } } @@ -188,6 +233,18 @@ impl HirDisplay for Union { let def_id = GenericDefId::AdtId(AdtId::UnionId(self.id)); write_generic_params(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_char('\n')?; + } + f.write_str("}")?; + } + Ok(()) } } diff --git a/crates/ide/src/hover/tests.rs b/crates/ide/src/hover/tests.rs index e54bc48d55..287a518ed1 100644 --- a/crates/ide/src/hover/tests.rs +++ b/crates/ide/src/hover/tests.rs @@ -1137,6 +1137,9 @@ impl Thing { ```rust struct Thing + { + x: u32, + } ``` "#]], ); @@ -1156,6 +1159,9 @@ impl Thing { ```rust struct Thing + { + x: u32, + } ``` "#]], ); @@ -1175,6 +1181,9 @@ impl Thing { ```rust enum Thing + { + A, + } ``` "#]], ); @@ -1194,6 +1203,9 @@ impl Thing { ```rust enum Thing + { + A, + } ``` "#]], ); @@ -2005,7 +2017,11 @@ fn test_hover_layout_of_enum() { ``` ```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 +2362,7 @@ fn main() { let s$0t = S{ f1:0 }; } focus_range: 7..8, name: "S", kind: Struct, - description: "struct S", + description: "struct S\n{\n f1: u32,\n}", }, }, ], @@ -2379,7 +2395,7 @@ fn main() { let s$0t = S{ f1:Arg(0) }; } focus_range: 24..25, name: "S", kind: Struct, - description: "struct S", + description: "struct S\n{\n f1: T,\n}", }, }, HoverGotoTypeData { @@ -2392,7 +2408,7 @@ fn main() { let s$0t = S{ f1:Arg(0) }; } focus_range: 7..10, name: "Arg", kind: Struct, - description: "struct Arg", + description: "struct Arg(u32);", }, }, ], @@ -2438,7 +2454,7 @@ fn main() { let s$0t = S{ f1: S{ f1: Arg(0) } }; } focus_range: 24..25, name: "S", kind: Struct, - description: "struct S", + description: "struct S\n{\n f1: T,\n}", }, }, HoverGotoTypeData { @@ -2451,7 +2467,7 @@ fn main() { let s$0t = S{ f1: S{ f1: Arg(0) } }; } focus_range: 7..10, name: "Arg", kind: Struct, - description: "struct Arg", + description: "struct Arg(u32);", }, }, ], @@ -2487,7 +2503,7 @@ fn main() { let s$0t = (A(1), B(2), M::C(3) ); } focus_range: 7..8, name: "A", kind: Struct, - description: "struct A", + description: "struct A(u32);", }, }, HoverGotoTypeData { @@ -2500,7 +2516,7 @@ fn main() { let s$0t = (A(1), B(2), M::C(3) ); } focus_range: 22..23, name: "B", kind: Struct, - description: "struct B", + description: "struct B(u32);", }, }, HoverGotoTypeData { @@ -2514,7 +2530,7 @@ fn main() { let s$0t = (A(1), B(2), M::C(3) ); } name: "C", kind: Struct, container_name: "M", - description: "pub struct C", + description: "pub struct C(u32);", }, }, ], @@ -3335,7 +3351,7 @@ struct S$0T(T); ``` ```rust - struct ST + struct ST(T); ``` "#]], ); @@ -3356,7 +3372,7 @@ struct S$0T(T); ``` ```rust - struct ST + struct ST(T); ``` "#]], ); @@ -3378,7 +3394,7 @@ struct S$0T(T); ``` ```rust - struct ST + struct ST(T); ``` "#]], ); @@ -5935,7 +5951,7 @@ pub struct Foo(i32); ``` ```rust - pub struct Foo // size = 4, align = 4 + pub struct Foo(i32); // size = 4, align = 4 ``` --- From 46af7d30d05df6dad6c1950b8ff6b0705ee2d45b Mon Sep 17 00:00:00 2001 From: YangzeLuo Date: Wed, 8 Nov 2023 20:08:09 +0800 Subject: [PATCH 2/3] fix: match the standard Rust formatting && use .kind() instead of iter --- crates/hir/src/display.rs | 19 +++++++----------- crates/ide/src/hover/tests.rs | 37 +++++++++++++++-------------------- 2 files changed, 23 insertions(+), 33 deletions(-) diff --git a/crates/hir/src/display.rs b/crates/hir/src/display.rs index 4717ed24f8..e8d0a16574 100644 --- a/crates/hir/src/display.rs +++ b/crates/hir/src/display.rs @@ -1,6 +1,6 @@ //! HirDisplay implementations for various hir types. use hir_def::{ - data::adt::VariantData, + data::adt::{StructKind, VariantData}, generics::{ TypeOrConstParamData, TypeParamProvenance, WherePredicate, WherePredicateTypeTarget, }, @@ -164,12 +164,9 @@ impl HirDisplay for Struct { let def_id = GenericDefId::AdtId(AdtId::StructId(self.id)); write_generic_params(def_id, f)?; - let field_is_tuple_index = - self.variant_data(f.db).fields().iter().any(|(_, f)| f.name.as_tuple_index().is_some()); - - if field_is_tuple_index { + let variant_data = self.variant_data(f.db); + if let StructKind::Tuple = variant_data.kind() { f.write_char('(')?; - let variant_data = self.variant_data(f.db); let mut it = variant_data.fields().iter().peekable(); while let Some((id, _)) = it.next() { @@ -181,14 +178,12 @@ impl HirDisplay for Struct { } f.write_str(");")?; - return Ok(()); } write_where_clause(def_id, f)?; - let fields = self.fields(f.db); - if !fields.is_empty() { - f.write_str("\n{\n")?; + if let StructKind::Record = variant_data.kind() { + f.write_str(" {\n")?; for field in self.fields(f.db) { f.write_str(" ")?; field.hir_fmt(f)?; @@ -212,7 +207,7 @@ impl HirDisplay for Enum { let variants = self.variants(f.db); if !variants.is_empty() { - f.write_str("\n{\n")?; + f.write_str(" {\n")?; for variant in variants { f.write_str(" ")?; variant.hir_fmt(f)?; @@ -240,7 +235,7 @@ impl HirDisplay for Union { for field in self.fields(f.db) { f.write_str(" ")?; field.hir_fmt(f)?; - f.write_char('\n')?; + f.write_str(",\n")?; } f.write_str("}")?; } diff --git a/crates/ide/src/hover/tests.rs b/crates/ide/src/hover/tests.rs index 287a518ed1..a52d98e230 100644 --- a/crates/ide/src/hover/tests.rs +++ b/crates/ide/src/hover/tests.rs @@ -1136,8 +1136,7 @@ impl Thing { ``` ```rust - struct Thing - { + struct Thing { x: u32, } ``` @@ -1158,8 +1157,7 @@ impl Thing { ``` ```rust - struct Thing - { + struct Thing { x: u32, } ``` @@ -1180,8 +1178,7 @@ impl Thing { ``` ```rust - enum Thing - { + enum Thing { A, } ``` @@ -1202,8 +1199,7 @@ impl Thing { ``` ```rust - enum Thing - { + enum Thing { A, } ``` @@ -2017,8 +2013,7 @@ fn test_hover_layout_of_enum() { ``` ```rust - enum Foo - { + enum Foo { Variant1(u8, u16), Variant2(i32, u8, i64), } // size = 16 (0x10), align = 8, niches = 254 @@ -2362,7 +2357,7 @@ fn main() { let s$0t = S{ f1:0 }; } focus_range: 7..8, name: "S", kind: Struct, - description: "struct S\n{\n f1: u32,\n}", + description: "struct S {\n f1: u32,\n}", }, }, ], @@ -2395,7 +2390,7 @@ fn main() { let s$0t = S{ f1:Arg(0) }; } focus_range: 24..25, name: "S", kind: Struct, - description: "struct S\n{\n f1: T,\n}", + description: "struct S {\n f1: T,\n}", }, }, HoverGotoTypeData { @@ -2454,7 +2449,7 @@ fn main() { let s$0t = S{ f1: S{ f1: Arg(0) } }; } focus_range: 24..25, name: "S", kind: Struct, - description: "struct S\n{\n f1: T,\n}", + description: "struct S {\n f1: T,\n}", }, }, HoverGotoTypeData { @@ -2720,7 +2715,7 @@ fn main() { let s$0t = foo(); } focus_range: 39..41, name: "S1", kind: Struct, - description: "struct S1", + description: "struct S1 {\n}", }, }, HoverGotoTypeData { @@ -2733,7 +2728,7 @@ fn main() { let s$0t = foo(); } focus_range: 52..54, name: "S2", kind: Struct, - description: "struct S2", + description: "struct S2 {\n}", }, }, ], @@ -2824,7 +2819,7 @@ fn foo(ar$0g: &impl Foo + Bar) {} focus_range: 36..37, name: "S", kind: Struct, - description: "struct S", + description: "struct S {\n}", }, }, ], @@ -2924,7 +2919,7 @@ fn foo(ar$0g: &impl Foo) {} focus_range: 23..24, name: "S", kind: Struct, - description: "struct S", + description: "struct S {\n}", }, }, ], @@ -2961,7 +2956,7 @@ fn main() { let s$0t = foo(); } focus_range: 49..50, name: "B", kind: Struct, - description: "struct B", + description: "struct B {\n}", }, }, HoverGotoTypeData { @@ -3050,7 +3045,7 @@ fn foo(ar$0g: &dyn Foo) {} focus_range: 23..24, name: "S", kind: Struct, - description: "struct S", + description: "struct S {\n}", }, }, ], @@ -3098,7 +3093,7 @@ fn foo(a$0rg: &impl ImplTrait>>>) {} focus_range: 50..51, name: "B", kind: Struct, - description: "struct B", + description: "struct B {\n}", }, }, HoverGotoTypeData { @@ -3124,7 +3119,7 @@ fn foo(a$0rg: &impl ImplTrait>>>) {} focus_range: 65..66, name: "S", kind: Struct, - description: "struct S", + description: "struct S {\n}", }, }, ], From 4f89b201520a4719b1298d5461300e5e55263d7c Mon Sep 17 00:00:00 2001 From: YangzeLuo Date: Wed, 8 Nov 2023 20:46:04 +0800 Subject: [PATCH 3/3] fix: using {} for empty struct && remove semicolon for struct with tuple kind --- crates/hir/src/display.rs | 19 ++++++++++++------- crates/ide/src/hover/tests.rs | 34 +++++++++++++++++----------------- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/crates/hir/src/display.rs b/crates/hir/src/display.rs index e8d0a16574..08777b4947 100644 --- a/crates/hir/src/display.rs +++ b/crates/hir/src/display.rs @@ -177,19 +177,24 @@ impl HirDisplay for Struct { } } - f.write_str(");")?; + f.write_str(")")?; } write_where_clause(def_id, f)?; if let StructKind::Record = variant_data.kind() { - f.write_str(" {\n")?; - for field in self.fields(f.db) { - f.write_str(" ")?; - field.hir_fmt(f)?; - f.write_str(",\n")?; + 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("}")?; } - f.write_str("}")?; } Ok(()) diff --git a/crates/ide/src/hover/tests.rs b/crates/ide/src/hover/tests.rs index a52d98e230..2f160a213f 100644 --- a/crates/ide/src/hover/tests.rs +++ b/crates/ide/src/hover/tests.rs @@ -2403,7 +2403,7 @@ fn main() { let s$0t = S{ f1:Arg(0) }; } focus_range: 7..10, name: "Arg", kind: Struct, - description: "struct Arg(u32);", + description: "struct Arg(u32)", }, }, ], @@ -2462,7 +2462,7 @@ fn main() { let s$0t = S{ f1: S{ f1: Arg(0) } }; } focus_range: 7..10, name: "Arg", kind: Struct, - description: "struct Arg(u32);", + description: "struct Arg(u32)", }, }, ], @@ -2498,7 +2498,7 @@ fn main() { let s$0t = (A(1), B(2), M::C(3) ); } focus_range: 7..8, name: "A", kind: Struct, - description: "struct A(u32);", + description: "struct A(u32)", }, }, HoverGotoTypeData { @@ -2511,7 +2511,7 @@ fn main() { let s$0t = (A(1), B(2), M::C(3) ); } focus_range: 22..23, name: "B", kind: Struct, - description: "struct B(u32);", + description: "struct B(u32)", }, }, HoverGotoTypeData { @@ -2525,7 +2525,7 @@ fn main() { let s$0t = (A(1), B(2), M::C(3) ); } name: "C", kind: Struct, container_name: "M", - description: "pub struct C(u32);", + description: "pub struct C(u32)", }, }, ], @@ -2715,7 +2715,7 @@ fn main() { let s$0t = foo(); } focus_range: 39..41, name: "S1", kind: Struct, - description: "struct S1 {\n}", + description: "struct S1 {}", }, }, HoverGotoTypeData { @@ -2728,7 +2728,7 @@ fn main() { let s$0t = foo(); } focus_range: 52..54, name: "S2", kind: Struct, - description: "struct S2 {\n}", + description: "struct S2 {}", }, }, ], @@ -2819,7 +2819,7 @@ fn foo(ar$0g: &impl Foo + Bar) {} focus_range: 36..37, name: "S", kind: Struct, - description: "struct S {\n}", + description: "struct S {}", }, }, ], @@ -2919,7 +2919,7 @@ fn foo(ar$0g: &impl Foo) {} focus_range: 23..24, name: "S", kind: Struct, - description: "struct S {\n}", + description: "struct S {}", }, }, ], @@ -2956,7 +2956,7 @@ fn main() { let s$0t = foo(); } focus_range: 49..50, name: "B", kind: Struct, - description: "struct B {\n}", + description: "struct B {}", }, }, HoverGotoTypeData { @@ -3045,7 +3045,7 @@ fn foo(ar$0g: &dyn Foo) {} focus_range: 23..24, name: "S", kind: Struct, - description: "struct S {\n}", + description: "struct S {}", }, }, ], @@ -3093,7 +3093,7 @@ fn foo(a$0rg: &impl ImplTrait>>>) {} focus_range: 50..51, name: "B", kind: Struct, - description: "struct B {\n}", + description: "struct B {}", }, }, HoverGotoTypeData { @@ -3119,7 +3119,7 @@ fn foo(a$0rg: &impl ImplTrait>>>) {} focus_range: 65..66, name: "S", kind: Struct, - description: "struct S {\n}", + description: "struct S {}", }, }, ], @@ -3346,7 +3346,7 @@ struct S$0T(T); ``` ```rust - struct ST(T); + struct ST(T) ``` "#]], ); @@ -3367,7 +3367,7 @@ struct S$0T(T); ``` ```rust - struct ST(T); + struct ST(T) ``` "#]], ); @@ -3389,7 +3389,7 @@ struct S$0T(T); ``` ```rust - struct ST(T); + struct ST(T) ``` "#]], ); @@ -5946,7 +5946,7 @@ pub struct Foo(i32); ``` ```rust - pub struct Foo(i32); // size = 4, align = 4 + pub struct Foo(i32) // size = 4, align = 4 ``` ---