@@ -6,7 +6,7 @@ use itertools::Itertools;
66use syntax:: ast;
77use syntax:: source_map:: { BytePos , Span } ;
88
9- use crate :: comment:: { combine_strs_with_missing_comments, contains_comment } ;
9+ use crate :: comment:: combine_strs_with_missing_comments;
1010use crate :: config:: lists:: * ;
1111use crate :: expr:: rewrite_field;
1212use crate :: items:: { rewrite_struct_field, rewrite_struct_field_prefix} ;
@@ -17,7 +17,9 @@ use crate::rewrite::{Rewrite, RewriteContext};
1717use crate :: shape:: { Indent , Shape } ;
1818use crate :: source_map:: SpanUtils ;
1919use crate :: spanned:: Spanned ;
20- use crate :: utils:: { contains_skip, is_attributes_extendable, mk_sp, rewrite_ident} ;
20+ use crate :: utils:: {
21+ contains_skip, is_attributes_extendable, mk_sp, rewrite_ident, trimmed_last_line_width,
22+ } ;
2123
2224pub ( crate ) trait AlignedItem {
2325 fn skip ( & self ) -> bool ;
@@ -183,13 +185,9 @@ fn struct_field_prefix_max_min_width<T: AlignedItem>(
183185 fields
184186 . iter ( )
185187 . map ( |field| {
186- field. rewrite_prefix ( context, shape) . and_then ( |field_str| {
187- if field_str. contains ( '\n' ) {
188- None
189- } else {
190- Some ( field_str. len ( ) )
191- }
192- } )
188+ field
189+ . rewrite_prefix ( context, shape)
190+ . map ( |field_str| trimmed_last_line_width ( & field_str) )
193191 } )
194192 . fold_options ( ( 0 , :: std:: usize:: MAX ) , |( max_len, min_len) , len| {
195193 ( cmp:: max ( max_len, len) , cmp:: min ( min_len, len) )
@@ -255,6 +253,9 @@ fn rewrite_aligned_items_inner<T: AlignedItem>(
255253 write_list ( & items, & fmt)
256254}
257255
256+ /// Returns the index in `fields` up to which a field belongs to the current group.
257+ /// The returned string is the group separator to use when rewriting the fields.
258+ /// Groups are defined by blank lines.
258259fn group_aligned_items < T : AlignedItem > (
259260 context : & RewriteContext < ' _ > ,
260261 fields : & [ T ] ,
@@ -264,25 +265,19 @@ fn group_aligned_items<T: AlignedItem>(
264265 if fields[ i] . skip ( ) {
265266 return ( "" , index) ;
266267 }
267- // See if there are comments or empty lines between fields.
268268 let span = mk_sp ( fields[ i] . get_span ( ) . hi ( ) , fields[ i + 1 ] . get_span ( ) . lo ( ) ) ;
269269 let snippet = context
270270 . snippet ( span)
271271 . lines ( )
272272 . skip ( 1 )
273273 . collect :: < Vec < _ > > ( )
274274 . join ( "\n " ) ;
275- let spacings = if snippet
275+ let has_blank_line = snippet
276276 . lines ( )
277277 . dropping_back ( 1 )
278- . any ( |l| l. trim ( ) . is_empty ( ) )
279- {
280- "\n "
281- } else {
282- ""
283- } ;
284- if contains_comment ( & snippet) || snippet. lines ( ) . count ( ) > 1 {
285- return ( spacings, index) ;
278+ . any ( |l| l. trim ( ) . is_empty ( ) ) ;
279+ if has_blank_line {
280+ return ( "\n " , index) ;
286281 }
287282 index += 1 ;
288283 }
0 commit comments