@@ -456,15 +456,30 @@ impl<'a> FmtVisitor<'a> {
456
456
return Some ( self . snippet ( span) ) ;
457
457
}
458
458
459
+ let context = self . get_context ( ) ;
459
460
let indent = self . block_indent ;
460
- let mut result = try_opt ! ( field. node. attrs. rewrite( & self . get_context( ) ,
461
- Shape :: indented( indent, self . config) ) ) ;
461
+ let mut result = try_opt ! ( field
462
+ . node
463
+ . attrs
464
+ . rewrite( & context, Shape :: indented( indent, self . config) ) ) ;
462
465
if !result. is_empty ( ) {
463
- result. push ( '\n' ) ;
464
- result. push_str ( & indent. to_string ( self . config ) ) ;
466
+ let shape = Shape {
467
+ width : context. config . max_width ( ) ,
468
+ indent : self . block_indent ,
469
+ offset : self . block_indent . alignment ,
470
+ } ;
471
+ let missing_comment =
472
+ rewrite_missing_comment_on_field ( & context,
473
+ shape,
474
+ field. node . attrs [ field. node . attrs . len ( ) - 1 ]
475
+ . span
476
+ . hi ,
477
+ field. span . lo ,
478
+ & mut result)
479
+ . unwrap_or ( String :: new ( ) ) ;
480
+ result. push_str ( & missing_comment) ;
465
481
}
466
482
467
- let context = self . get_context ( ) ;
468
483
let variant_body = match field. node . data {
469
484
ast:: VariantData :: Tuple ( ..) |
470
485
ast:: VariantData :: Struct ( ..) => {
@@ -1194,6 +1209,31 @@ fn type_annotation_spacing(config: &Config) -> (&str, &str) {
1194
1209
} )
1195
1210
}
1196
1211
1212
+ fn rewrite_missing_comment_on_field ( context : & RewriteContext ,
1213
+ shape : Shape ,
1214
+ lo : BytePos ,
1215
+ hi : BytePos ,
1216
+ result : & mut String )
1217
+ -> Option < String > {
1218
+ let possibly_comment_snippet = context. snippet ( mk_sp ( lo, hi) ) ;
1219
+ let newline_index = possibly_comment_snippet. find ( '\n' ) ;
1220
+ let comment_index = possibly_comment_snippet. find ( '/' ) ;
1221
+ match ( newline_index, comment_index) {
1222
+ ( Some ( i) , Some ( j) ) if i > j => result. push ( ' ' ) ,
1223
+ _ => {
1224
+ result. push ( '\n' ) ;
1225
+ result. push_str ( & shape. indent . to_string ( context. config ) ) ;
1226
+ }
1227
+ }
1228
+ let trimmed = possibly_comment_snippet. trim ( ) ;
1229
+ if trimmed. is_empty ( ) {
1230
+ None
1231
+ } else {
1232
+ rewrite_comment ( trimmed, false , shape, context. config )
1233
+ . map ( |s| format ! ( "{}\n {}" , s, shape. indent. to_string( context. config) ) )
1234
+ }
1235
+ }
1236
+
1197
1237
impl Rewrite for ast:: StructField {
1198
1238
fn rewrite ( & self , context : & RewriteContext , shape : Shape ) -> Option < String > {
1199
1239
if contains_skip ( & self . attrs ) {
@@ -1208,25 +1248,12 @@ impl Rewrite for ast::StructField {
1208
1248
context. config) ) ) ;
1209
1249
// Try format missing comments after attributes
1210
1250
let missing_comment = if !self . attrs . is_empty ( ) {
1211
- let possibly_comment_snippet =
1212
- context. snippet ( mk_sp ( self . attrs [ self . attrs . len ( ) - 1 ] . span . hi , self . span . lo ) ) ;
1213
- let newline_index = possibly_comment_snippet. find ( '\n' ) ;
1214
- let comment_index = possibly_comment_snippet. find ( '/' ) ;
1215
- match ( newline_index, comment_index) {
1216
- ( Some ( i) , Some ( j) ) if i > j => attr_str. push ( ' ' ) ,
1217
- _ => {
1218
- attr_str. push ( '\n' ) ;
1219
- attr_str. push_str ( & shape. indent . to_string ( context. config ) ) ;
1220
- }
1221
- }
1222
- let trimmed = possibly_comment_snippet. trim ( ) ;
1223
- if trimmed. is_empty ( ) {
1224
- String :: new ( )
1225
- } else {
1226
- rewrite_comment ( trimmed, false , shape, context. config ) . map_or ( String :: new ( ) , |s| {
1227
- format ! ( "{}\n {}" , s, shape. indent. to_string( context. config) )
1228
- } )
1229
- }
1251
+ rewrite_missing_comment_on_field ( context,
1252
+ shape,
1253
+ self . attrs [ self . attrs . len ( ) - 1 ] . span . hi ,
1254
+ self . span . lo ,
1255
+ & mut attr_str)
1256
+ . unwrap_or ( String :: new ( ) )
1230
1257
} else {
1231
1258
String :: new ( )
1232
1259
} ;
0 commit comments