@@ -2,7 +2,7 @@ use syntax::ast::{self, BindingMode, FieldPat, Pat, PatKind, RangeEnd, RangeSynt
2
2
use syntax:: ptr;
3
3
use syntax:: source_map:: { self , BytePos , Span } ;
4
4
5
- use crate :: comment:: FindUncommented ;
5
+ use crate :: comment:: { combine_strs_with_missing_comments , FindUncommented } ;
6
6
use crate :: config:: lists:: * ;
7
7
use crate :: expr:: { can_be_overflowed_expr, rewrite_unary_prefix, wrap_struct_field} ;
8
8
use crate :: lists:: {
@@ -179,7 +179,13 @@ fn rewrite_struct_pat(
179
179
fields. iter ( ) ,
180
180
terminator,
181
181
"," ,
182
- |f| f. span . lo ( ) ,
182
+ |f| {
183
+ if f. node . attrs . is_empty ( ) {
184
+ f. span . lo ( )
185
+ } else {
186
+ f. node . attrs . first ( ) . unwrap ( ) . span . lo ( )
187
+ }
188
+ } ,
183
189
|f| f. span . hi ( ) ,
184
190
|f| f. node . rewrite ( context, v_shape) ,
185
191
context. snippet_provider . span_after ( span, "{" ) ,
@@ -225,25 +231,50 @@ fn rewrite_struct_pat(
225
231
226
232
impl Rewrite for FieldPat {
227
233
fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
228
- let pat = self . pat . rewrite ( context, shape) ;
234
+ let hi_pos = if let Some ( last) = self . attrs . last ( ) {
235
+ last. span . hi ( )
236
+ } else {
237
+ self . pat . span . lo ( )
238
+ } ;
239
+
240
+ let attrs_str = if self . attrs . is_empty ( ) {
241
+ String :: from ( "" )
242
+ } else {
243
+ self . attrs . rewrite ( context, shape) ?
244
+ } ;
245
+
246
+ let pat_str = self . pat . rewrite ( context, shape) ?;
229
247
if self . is_shorthand {
230
- pat
248
+ combine_strs_with_missing_comments (
249
+ context,
250
+ & attrs_str,
251
+ & pat_str,
252
+ mk_sp ( hi_pos, self . pat . span . lo ( ) ) ,
253
+ shape,
254
+ false ,
255
+ )
231
256
} else {
232
- let pat_str = pat? ;
257
+ let nested_shape = shape . block_indent ( context . config . tab_spaces ( ) ) ;
233
258
let id_str = rewrite_ident ( context, self . ident ) ;
234
259
let one_line_width = id_str. len ( ) + 2 + pat_str. len ( ) ;
235
- if one_line_width <= shape. width {
236
- Some ( format ! ( "{}: {}" , id_str, pat_str) )
260
+ let pat_and_id_str = if one_line_width <= shape. width {
261
+ format ! ( "{}: {}" , id_str, pat_str)
237
262
} else {
238
- let nested_shape = shape. block_indent ( context. config . tab_spaces ( ) ) ;
239
- let pat_str = self . pat . rewrite ( context, nested_shape) ?;
240
- Some ( format ! (
263
+ format ! (
241
264
"{}:\n {}{}" ,
242
265
id_str,
243
266
nested_shape. indent. to_string( context. config) ,
244
- pat_str,
245
- ) )
246
- }
267
+ self . pat. rewrite( context, nested_shape) ?
268
+ )
269
+ } ;
270
+ combine_strs_with_missing_comments (
271
+ context,
272
+ & attrs_str,
273
+ & pat_and_id_str,
274
+ mk_sp ( hi_pos, self . pat . span . lo ( ) ) ,
275
+ nested_shape,
276
+ false ,
277
+ )
247
278
}
248
279
}
249
280
}
0 commit comments