@@ -10,8 +10,9 @@ use hir_expand::{Lookup, mod_path::PathKind};
10
10
use itertools:: Itertools ;
11
11
use span:: Edition ;
12
12
13
+ use crate :: signatures:: StructFlags ;
13
14
use crate :: {
14
- DefWithBodyId , ItemTreeLoc , TypeParamId ,
15
+ AdtId , DefWithBodyId , GenericDefId , ItemTreeLoc , TypeParamId , VariantId ,
15
16
expr_store:: path:: { GenericArg , GenericArgs } ,
16
17
hir:: {
17
18
Array , BindingAnnotation , CaptureBy , ClosureKind , Literal , Movability , Statement ,
@@ -21,6 +22,7 @@ use crate::{
21
22
signatures:: { FnFlags , FunctionSignature , StructSignature } ,
22
23
type_ref:: { ConstRef , LifetimeRef , Mutability , TraitBoundModifier , TypeBound , UseArgRef } ,
23
24
} ;
25
+ use crate :: { item_tree:: FieldsShape , signatures:: FieldData } ;
24
26
25
27
use super :: * ;
26
28
@@ -40,13 +42,13 @@ macro_rules! wln {
40
42
}
41
43
42
44
#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
43
- pub ( crate ) enum LineFormat {
45
+ pub enum LineFormat {
44
46
Oneline ,
45
47
Newline ,
46
48
Indentation ,
47
49
}
48
50
49
- pub ( crate ) fn print_body_hir (
51
+ pub fn print_body_hir (
50
52
db : & dyn DefDatabase ,
51
53
body : & Body ,
52
54
owner : DefWithBodyId ,
@@ -112,7 +114,93 @@ pub(crate) fn print_body_hir(
112
114
p. buf
113
115
}
114
116
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 (
116
204
db : & dyn DefDatabase ,
117
205
store : & ExpressionStore ,
118
206
path : & Path ,
@@ -130,14 +218,11 @@ pub(crate) fn print_path(
130
218
p. buf
131
219
}
132
220
133
- pub ( crate ) fn print_struct (
221
+ pub fn print_struct (
134
222
db : & dyn DefDatabase ,
135
223
StructSignature { name, generic_params, store, flags, shape, repr } : & StructSignature ,
136
224
edition : Edition ,
137
225
) -> String {
138
- use crate :: item_tree:: FieldsShape ;
139
- use crate :: signatures:: StructFlags ;
140
-
141
226
let mut p = Printer {
142
227
db,
143
228
store,
@@ -180,7 +265,7 @@ pub(crate) fn print_struct(
180
265
p. buf
181
266
}
182
267
183
- pub ( crate ) fn print_function (
268
+ pub fn print_function (
184
269
db : & dyn DefDatabase ,
185
270
FunctionSignature {
186
271
name,
@@ -342,7 +427,7 @@ fn print_generic_params(db: &dyn DefDatabase, generic_params: &GenericParams, p:
342
427
}
343
428
}
344
429
345
- pub ( crate ) fn print_expr_hir (
430
+ pub fn print_expr_hir (
346
431
db : & dyn DefDatabase ,
347
432
store : & ExpressionStore ,
348
433
_owner : DefWithBodyId ,
@@ -361,7 +446,7 @@ pub(crate) fn print_expr_hir(
361
446
p. buf
362
447
}
363
448
364
- pub ( crate ) fn print_pat_hir (
449
+ pub fn print_pat_hir (
365
450
db : & dyn DefDatabase ,
366
451
store : & ExpressionStore ,
367
452
_owner : DefWithBodyId ,
0 commit comments