File tree 1 file changed +14
-26
lines changed
1 file changed +14
-26
lines changed Original file line number Diff line number Diff line change @@ -438,35 +438,23 @@ impl fmt::Display for MyError {
438
438
}
439
439
440
440
fn reset_indentation ( s : & str ) -> String {
441
- indent_recurse ( s . lines ( ) , 0 )
442
- }
441
+ let mut indent : u32 = 0 ;
442
+ let mut dst = String :: new ( ) ;
443
443
444
- fn indent_recurse ( mut lines : :: std:: str:: Lines , current_indent : usize ) -> String {
445
- if let Some ( line) = lines. next ( ) {
446
- let mut trimmed = line. trim ( ) . to_owned ( ) ;
447
- let mut next_indent = current_indent;
448
- let mut current_indent = current_indent;
449
- if trimmed. ends_with ( '{' ) {
450
- next_indent += 1 ;
444
+ for line in s. lines ( ) {
445
+ let line = line. trim ( ) ;
446
+ if line. starts_with ( '}' ) || line. ends_with ( '}' ) {
447
+ indent = indent. saturating_sub ( 1 ) ;
451
448
}
452
- if trimmed. starts_with ( '}' ) || trimmed. ends_with ( '}' ) {
453
- if current_indent > 0 {
454
- current_indent -= 1 ;
455
- }
456
- if next_indent > 0 {
457
- next_indent -= 1 ;
458
- }
449
+ let extra = if line. starts_with ( ':' ) || line. starts_with ( '?' ) { 1 } else { 0 } ;
450
+ for _ in 0 ..indent + extra {
451
+ dst. push_str ( " " ) ;
459
452
}
460
- if trimmed. starts_with ( '?' ) || trimmed. starts_with ( ':' ) {
461
- current_indent += 1 ;
453
+ dst. push_str ( line) ;
454
+ dst. push_str ( "\n " ) ;
455
+ if line. ends_with ( '{' ) {
456
+ indent += 1 ;
462
457
}
463
- format ! (
464
- "\n {}{}{}" ,
465
- " " . repeat( current_indent) ,
466
- & trimmed,
467
- & indent_recurse( lines, next_indent)
468
- )
469
- } else {
470
- String :: new ( )
471
458
}
459
+ return dst
472
460
}
You can’t perform that action at this time.
0 commit comments