@@ -10,8 +10,9 @@ use hir_expand::{Lookup, mod_path::PathKind};
1010use itertools:: Itertools ;
1111use span:: Edition ;
1212
13+ use crate :: signatures:: StructFlags ;
1314use crate :: {
14- DefWithBodyId , ItemTreeLoc , TypeParamId ,
15+ AdtId , DefWithBodyId , GenericDefId , ItemTreeLoc , TypeParamId , VariantId ,
1516 expr_store:: path:: { GenericArg , GenericArgs } ,
1617 hir:: {
1718 Array , BindingAnnotation , CaptureBy , ClosureKind , Literal , Movability , Statement ,
@@ -21,6 +22,7 @@ use crate::{
2122 signatures:: { FnFlags , FunctionSignature , StructSignature } ,
2223 type_ref:: { ConstRef , LifetimeRef , Mutability , TraitBoundModifier , TypeBound , UseArgRef } ,
2324} ;
25+ use crate :: { item_tree:: FieldsShape , signatures:: FieldData } ;
2426
2527use super :: * ;
2628
@@ -40,13 +42,13 @@ macro_rules! wln {
4042}
4143
4244#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
43- pub ( crate ) enum LineFormat {
45+ pub enum LineFormat {
4446 Oneline ,
4547 Newline ,
4648 Indentation ,
4749}
4850
49- pub ( crate ) fn print_body_hir (
51+ pub fn print_body_hir (
5052 db : & dyn DefDatabase ,
5153 body : & Body ,
5254 owner : DefWithBodyId ,
@@ -112,7 +114,93 @@ pub(crate) fn print_body_hir(
112114 p. buf
113115}
114116
115- pub ( crate ) fn print_path (
117+ pub fn print_variant_body_hir ( db : & dyn DefDatabase , owner : VariantId , edition : Edition ) -> String {
118+ let header = match owner {
119+ VariantId :: StructId ( it) => {
120+ it. lookup ( db) . id . resolved ( db, |it| format ! ( "struct {}" , it. name. display( db, edition) ) )
121+ }
122+ VariantId :: EnumVariantId ( enum_variant_id) => {
123+ let loc = enum_variant_id. lookup ( db) ;
124+ let enum_loc = loc. parent . lookup ( db) ;
125+ format ! (
126+ "enum {}::{}" ,
127+ enum_loc. id. item_tree( db) [ enum_loc. id. value] . name. display( db, edition) ,
128+ loc. id. item_tree( db) [ loc. id. value] . name. display( db, edition) ,
129+ )
130+ }
131+ VariantId :: UnionId ( union_id) => union_id
132+ . lookup ( db)
133+ . id
134+ . resolved ( db, |it| format ! ( "union {}" , it. name. display( db, edition) ) ) ,
135+ } ;
136+
137+ let fields = db. variant_fields ( owner) ;
138+
139+ let mut p = Printer {
140+ db,
141+ store : & fields. store ,
142+ buf : header,
143+ indent_level : 0 ,
144+ line_format : LineFormat :: Newline ,
145+ edition,
146+ } ;
147+ match fields. shape {
148+ FieldsShape :: Record => wln ! ( p, " {{" ) ,
149+ FieldsShape :: Tuple => wln ! ( p, "(" ) ,
150+ FieldsShape :: Unit => ( ) ,
151+ }
152+
153+ for ( _, data) in fields. fields ( ) . iter ( ) {
154+ let FieldData { name, type_ref, visibility, is_unsafe } = data;
155+ match visibility {
156+ crate :: item_tree:: RawVisibility :: Module ( interned, _visibility_explicitness) => {
157+ w ! ( p, "{}" , interned. display( db, p. edition) )
158+ }
159+ crate :: item_tree:: RawVisibility :: Public => w ! ( p, "pub " ) ,
160+ }
161+ if * is_unsafe {
162+ w ! ( p, "unsafe " ) ;
163+ }
164+ w ! ( p, "{}: " , name. display( db, p. edition) ) ;
165+ p. print_type_ref ( * type_ref) ;
166+ }
167+
168+ match fields. shape {
169+ FieldsShape :: Record => wln ! ( p, "}}" ) ,
170+ FieldsShape :: Tuple => wln ! ( p, ");" ) ,
171+ FieldsShape :: Unit => wln ! ( p, ";" ) ,
172+ }
173+ p. buf
174+ }
175+
176+ pub fn print_signature ( db : & dyn DefDatabase , owner : GenericDefId , edition : Edition ) -> String {
177+ match owner {
178+ GenericDefId :: AdtId ( id) => match id {
179+ AdtId :: StructId ( id) => {
180+ let signature = db. struct_signature ( id) ;
181+ print_struct ( db, & signature, edition)
182+ }
183+ AdtId :: UnionId ( id) => {
184+ format ! ( "unimplemented {id:?}" )
185+ }
186+ AdtId :: EnumId ( id) => {
187+ format ! ( "unimplemented {id:?}" )
188+ }
189+ } ,
190+ GenericDefId :: ConstId ( id) => format ! ( "unimplemented {id:?}" ) ,
191+ GenericDefId :: FunctionId ( id) => {
192+ let signature = db. function_signature ( id) ;
193+ print_function ( db, & signature, edition)
194+ }
195+ GenericDefId :: ImplId ( id) => format ! ( "unimplemented {id:?}" ) ,
196+ GenericDefId :: StaticId ( id) => format ! ( "unimplemented {id:?}" ) ,
197+ GenericDefId :: TraitAliasId ( id) => format ! ( "unimplemented {id:?}" ) ,
198+ GenericDefId :: TraitId ( id) => format ! ( "unimplemented {id:?}" ) ,
199+ GenericDefId :: TypeAliasId ( id) => format ! ( "unimplemented {id:?}" ) ,
200+ }
201+ }
202+
203+ pub fn print_path (
116204 db : & dyn DefDatabase ,
117205 store : & ExpressionStore ,
118206 path : & Path ,
@@ -130,14 +218,11 @@ pub(crate) fn print_path(
130218 p. buf
131219}
132220
133- pub ( crate ) fn print_struct (
221+ pub fn print_struct (
134222 db : & dyn DefDatabase ,
135223 StructSignature { name, generic_params, store, flags, shape, repr } : & StructSignature ,
136224 edition : Edition ,
137225) -> String {
138- use crate :: item_tree:: FieldsShape ;
139- use crate :: signatures:: StructFlags ;
140-
141226 let mut p = Printer {
142227 db,
143228 store,
@@ -180,7 +265,7 @@ pub(crate) fn print_struct(
180265 p. buf
181266}
182267
183- pub ( crate ) fn print_function (
268+ pub fn print_function (
184269 db : & dyn DefDatabase ,
185270 FunctionSignature {
186271 name,
@@ -342,7 +427,7 @@ fn print_generic_params(db: &dyn DefDatabase, generic_params: &GenericParams, p:
342427 }
343428}
344429
345- pub ( crate ) fn print_expr_hir (
430+ pub fn print_expr_hir (
346431 db : & dyn DefDatabase ,
347432 store : & ExpressionStore ,
348433 _owner : DefWithBodyId ,
@@ -361,7 +446,7 @@ pub(crate) fn print_expr_hir(
361446 p. buf
362447}
363448
364- pub ( crate ) fn print_pat_hir (
449+ pub fn print_pat_hir (
365450 db : & dyn DefDatabase ,
366451 store : & ExpressionStore ,
367452 _owner : DefWithBodyId ,
0 commit comments