@@ -309,31 +309,51 @@ fn macro_style(mac: &ast::Mac, context: &RewriteContext) -> MacroStyle {
309
309
/// }
310
310
/// ```
311
311
fn indent_macro_snippet ( macro_str : & str , indent : Indent ) -> Option < String > {
312
- let min_prefix_space_width =
313
- try_opt ! ( macro_str. lines( ) . skip( 1 ) . map( get_prefix_space_width) . min( ) ) ;
314
-
315
312
let mut lines = macro_str. lines ( ) ;
316
- let first_line = try_opt ! ( lines. next( ) ) ;
313
+ let first_line = try_opt ! ( lines. next( ) . map( |s| s. trim_right( ) ) ) ;
314
+ let mut trimmed_lines = Vec :: with_capacity ( 16 ) ;
315
+
316
+ let min_prefix_space_width = try_opt ! (
317
+ lines
318
+ . filter_map( |line| {
319
+ let prefix_space_width = if is_empty_line( line) {
320
+ None
321
+ } else {
322
+ get_prefix_space_width( line)
323
+ } ;
324
+ trimmed_lines. push( ( line. trim( ) , prefix_space_width) ) ;
325
+ prefix_space_width
326
+ } )
327
+ . min( )
328
+ ) ;
317
329
318
330
Some (
319
331
String :: from ( first_line) + "\n " +
320
- & lines
321
- . map ( |line| {
322
- let new_indent_width = indent. width ( ) +
323
- get_prefix_space_width ( line)
324
- . checked_sub ( min_prefix_space_width)
325
- . unwrap_or ( 0 ) ;
326
- repeat_white_space ( new_indent_width) + line. trim ( )
332
+ & trimmed_lines
333
+ . iter ( )
334
+ . map ( |& ( line, prefix_space_width) | match prefix_space_width {
335
+ Some ( original_indent_width) => {
336
+ let new_indent_width = indent. width ( ) +
337
+ original_indent_width
338
+ . checked_sub ( min_prefix_space_width)
339
+ . unwrap_or ( 0 ) ;
340
+ repeat_white_space ( new_indent_width) + line. trim ( )
341
+ }
342
+ None => String :: new ( ) ,
327
343
} )
328
344
. collect :: < Vec < _ > > ( )
329
345
. join ( "\n " ) ,
330
346
)
331
347
}
332
348
333
- fn get_prefix_space_width ( s : & str ) -> usize {
334
- s. chars ( ) . position ( |c| c != ' ' ) . unwrap_or ( 0 )
349
+ fn get_prefix_space_width ( s : & str ) -> Option < usize > {
350
+ s. chars ( ) . position ( |c| c != ' ' )
335
351
}
336
352
337
353
fn repeat_white_space ( ws_count : usize ) -> String {
338
354
repeat ( " " ) . take ( ws_count) . collect :: < String > ( )
339
355
}
356
+
357
+ fn is_empty_line ( s : & str ) -> bool {
358
+ s. is_empty ( ) || s. chars ( ) . all ( char:: is_whitespace)
359
+ }
0 commit comments