@@ -27,6 +27,7 @@ extern crate unicode_segmentation;
27
27
use std:: collections:: HashMap ;
28
28
use std:: fmt;
29
29
use std:: io:: { self , stdout, Write } ;
30
+ use std:: iter:: repeat;
30
31
use std:: ops:: { Add , Sub } ;
31
32
use std:: path:: { Path , PathBuf } ;
32
33
use std:: rc:: Rc ;
@@ -456,7 +457,7 @@ impl fmt::Display for ErrorKind {
456
457
match * self {
457
458
ErrorKind :: LineOverflow ( found, maximum) => write ! (
458
459
fmt,
459
- "line exceeded maximum length (maximum: {}, found: {})" ,
460
+ "line exceeded maximum width (maximum: {}, found: {})" ,
460
461
maximum,
461
462
found
462
463
) ,
@@ -477,16 +478,36 @@ pub struct FormattingError {
477
478
impl FormattingError {
478
479
fn msg_prefix ( & self ) -> & str {
479
480
match self . kind {
480
- ErrorKind :: LineOverflow ( ..) | ErrorKind :: TrailingWhitespace => "Rustfmt failed at " ,
481
+ ErrorKind :: LineOverflow ( ..) | ErrorKind :: TrailingWhitespace => "error: " ,
481
482
ErrorKind :: BadIssue ( _) => "WARNING:" ,
482
483
}
483
484
}
484
485
485
- fn msg_suffix ( & self ) -> & str {
486
+ fn msg_suffix ( & self ) -> String {
486
487
match self . kind {
488
+ ErrorKind :: LineOverflow ( ..) if self . is_comment => format ! (
489
+ "use `error_on_lineoverflow_comments = false` to suppress \
490
+ the warning against line comments\n ",
491
+ ) ,
487
492
_ => String :: from ( "" ) ,
488
493
}
489
494
}
495
+
496
+ // (space, target)
497
+ pub fn format_len ( & self ) -> ( usize , usize ) {
498
+ match self . kind {
499
+ ErrorKind :: LineOverflow ( found, max) => ( max, found - max) ,
500
+ ErrorKind :: TrailingWhitespace => {
501
+ let trailing_ws_len = self . line_buffer
502
+ . chars ( )
503
+ . rev ( )
504
+ . take_while ( |c| c. is_whitespace ( ) )
505
+ . count ( ) ;
506
+ ( self . line_buffer . len ( ) - trailing_ws_len, trailing_ws_len)
507
+ }
508
+ _ => ( 0 , 0 ) , // unreachable
509
+ }
510
+ }
490
511
}
491
512
492
513
pub struct FormatReport {
@@ -518,17 +539,50 @@ impl fmt::Display for FormatReport {
518
539
fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> Result < ( ) , fmt:: Error > {
519
540
for ( file, errors) in & self . file_error_map {
520
541
for error in errors {
542
+ let prefix_space_len = error. line . to_string ( ) . len ( ) ;
543
+ let prefix_spaces: String = repeat ( " " ) . take ( 1 + prefix_space_len) . collect ( ) ;
544
+
545
+ let error_line_buffer = if error. line_buffer . is_empty ( ) {
546
+ String :: from ( " " )
547
+ } else {
548
+ let ( space_len, target_len) = error. format_len ( ) ;
549
+ format ! (
550
+ "{}|\n {} | {}\n {}| {}" ,
551
+ prefix_spaces,
552
+ error. line,
553
+ error. line_buffer,
554
+ prefix_spaces,
555
+ target_str( space_len, target_len)
556
+ )
557
+ } ;
558
+
559
+ let error_info = format ! ( "{} {}" , error. msg_prefix( ) , error. kind) ;
560
+ let file_info = format ! ( "{}--> {}:{}" , & prefix_spaces[ 1 ..] , file, error. line) ;
561
+ let msg_suffix = error. msg_suffix ( ) ;
562
+ let note = if msg_suffix. is_empty ( ) {
563
+ String :: new ( )
564
+ } else {
565
+ format ! ( "{}note= " , prefix_spaces)
566
+ } ;
567
+
521
568
write ! (
522
569
fmt,
523
- "{} {}:{}: {} {}\n " ,
524
- error . msg_prefix ( ) ,
525
- file ,
526
- error . line ,
527
- error . kind ,
570
+ "{}\n {} \n {} \n {} {}\n " ,
571
+ error_info ,
572
+ file_info ,
573
+ error_line_buffer ,
574
+ note ,
528
575
error. msg_suffix( )
529
576
) ?;
530
577
}
531
578
}
579
+ if !self . file_error_map . is_empty ( ) {
580
+ write ! (
581
+ fmt,
582
+ "warning: rustfmt may have failed to format. See previous {} errors.\n " ,
583
+ self . warning_count( ) ,
584
+ ) ?;
585
+ }
532
586
Ok ( ( ) )
533
587
}
534
588
}
0 commit comments