@@ -2,7 +2,7 @@ use syntax::ast::{self, BindingMode, FieldPat, Pat, PatKind, RangeEnd, RangeSynt
22use syntax:: ptr;
33use syntax:: source_map:: { self , BytePos , Span } ;
44
5- use crate :: comment:: FindUncommented ;
5+ use crate :: comment:: { combine_strs_with_missing_comments , FindUncommented } ;
66use crate :: config:: lists:: * ;
77use crate :: expr:: { can_be_overflowed_expr, rewrite_unary_prefix, wrap_struct_field} ;
88use crate :: lists:: {
@@ -179,7 +179,13 @@ fn rewrite_struct_pat(
179179 fields. iter ( ) ,
180180 terminator,
181181 "," ,
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+ } ,
183189 |f| f. span . hi ( ) ,
184190 |f| f. node . rewrite ( context, v_shape) ,
185191 context. snippet_provider . span_after ( span, "{" ) ,
@@ -225,25 +231,50 @@ fn rewrite_struct_pat(
225231
226232impl Rewrite for FieldPat {
227233 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) ?;
229247 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+ )
231256 } else {
232- let pat_str = pat? ;
257+ let nested_shape = shape . block_indent ( context . config . tab_spaces ( ) ) ;
233258 let id_str = rewrite_ident ( context, self . ident ) ;
234259 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)
237262 } 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 ! (
241264 "{}:\n {}{}" ,
242265 id_str,
243266 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+ )
247278 }
248279 }
249280}
0 commit comments