1
1
use crate :: formatting:: FormattingError ;
2
2
use crate :: { ErrorKind , FormatReport } ;
3
- use annotate_snippets:: display_list:: { DisplayList , FormatOptions } ;
4
- use annotate_snippets:: snippet:: { Annotation , AnnotationType , Slice , Snippet , SourceAnnotation } ;
3
+ use annotate_snippets:: { Annotation , Level , Renderer , Snippet } ;
5
4
use std:: fmt:: { self , Display } ;
6
5
7
6
/// A builder for [`FormatReportFormatter`].
@@ -49,51 +48,35 @@ impl<'a> Display for FormatReportFormatter<'a> {
49
48
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
50
49
let errors_by_file = & self . report . internal . borrow ( ) . 0 ;
51
50
52
- let opt = FormatOptions {
53
- color : self . enable_colors ,
54
- ..Default :: default ( )
51
+ let renderer = if self . enable_colors {
52
+ Renderer :: styled ( )
53
+ } else {
54
+ Renderer :: plain ( )
55
55
} ;
56
56
57
57
for ( file, errors) in errors_by_file {
58
58
for error in errors {
59
59
let error_kind = error. kind . to_string ( ) ;
60
- let title = Some ( Annotation {
61
- id : if error. is_internal ( ) {
62
- Some ( "internal" )
63
- } else {
64
- None
65
- } ,
66
- label : Some ( & error_kind) ,
67
- annotation_type : error_kind_to_snippet_annotation_type ( & error. kind ) ,
68
- } ) ;
60
+ let mut message =
61
+ error_kind_to_snippet_annotation_level ( & error. kind ) . title ( & error_kind) ;
62
+ if error. is_internal ( ) {
63
+ message = message. id ( "internal" ) ;
64
+ }
69
65
70
66
let message_suffix = error. msg_suffix ( ) ;
71
- let footer = if !message_suffix. is_empty ( ) {
72
- Some ( Annotation {
73
- id : None ,
74
- label : Some ( message_suffix) ,
75
- annotation_type : AnnotationType :: Note ,
76
- } )
77
- } else {
78
- None
79
- } ;
67
+ if !message_suffix. is_empty ( ) {
68
+ message = message. footer ( Level :: Note . title ( & message_suffix) ) ;
69
+ }
80
70
81
71
let origin = format ! ( "{}:{}" , file, error. line) ;
82
- let slice = Slice {
83
- source : & error. line_buffer . clone ( ) ,
84
- line_start : error. line ,
85
- origin : Some ( origin. as_str ( ) ) ,
86
- fold : false ,
87
- annotations : slice_annotation ( error) . into_iter ( ) . collect ( ) ,
88
- } ;
89
-
90
- let snippet = Snippet {
91
- title,
92
- footer : footer. into_iter ( ) . collect ( ) ,
93
- slices : vec ! [ slice] ,
94
- opt,
95
- } ;
96
- writeln ! ( f, "{}\n " , DisplayList :: from( snippet) ) ?;
72
+ let snippet = Snippet :: source ( & error. line_buffer )
73
+ . line_start ( error. line )
74
+ . origin ( & origin)
75
+ . fold ( false )
76
+ . annotations ( annotation ( error) ) ;
77
+ message = message. snippet ( snippet) ;
78
+
79
+ writeln ! ( f, "{}\n " , renderer. render( message) ) ?;
97
80
}
98
81
}
99
82
@@ -102,39 +85,26 @@ impl<'a> Display for FormatReportFormatter<'a> {
102
85
"rustfmt has failed to format. See previous {} errors." ,
103
86
self . report. warning_count( )
104
87
) ;
105
- let snippet = Snippet {
106
- title : Some ( Annotation {
107
- id : None ,
108
- label : Some ( & label) ,
109
- annotation_type : AnnotationType :: Warning ,
110
- } ) ,
111
- footer : Vec :: new ( ) ,
112
- slices : Vec :: new ( ) ,
113
- opt,
114
- } ;
115
- writeln ! ( f, "{}" , DisplayList :: from( snippet) ) ?;
88
+ let message = Level :: Warning . title ( & label) ;
89
+ writeln ! ( f, "{}" , renderer. render( message) ) ?;
116
90
}
117
91
118
92
Ok ( ( ) )
119
93
}
120
94
}
121
95
122
- fn slice_annotation ( error : & FormattingError ) -> Option < SourceAnnotation < ' _ > > {
96
+ fn annotation ( error : & FormattingError ) -> Option < Annotation < ' _ > > {
123
97
let ( range_start, range_length) = error. format_len ( ) ;
124
98
let range_end = range_start + range_length;
125
99
126
100
if range_length > 0 {
127
- Some ( SourceAnnotation {
128
- annotation_type : AnnotationType :: Error ,
129
- range : ( range_start, range_end) ,
130
- label : "" ,
131
- } )
101
+ Some ( Level :: Error . span ( range_start..range_end) )
132
102
} else {
133
103
None
134
104
}
135
105
}
136
106
137
- fn error_kind_to_snippet_annotation_type ( error_kind : & ErrorKind ) -> AnnotationType {
107
+ fn error_kind_to_snippet_annotation_level ( error_kind : & ErrorKind ) -> Level {
138
108
match error_kind {
139
109
ErrorKind :: LineOverflow ( ..)
140
110
| ErrorKind :: TrailingWhitespace
@@ -144,7 +114,7 @@ fn error_kind_to_snippet_annotation_type(error_kind: &ErrorKind) -> AnnotationTy
144
114
| ErrorKind :: LostComment
145
115
| ErrorKind :: BadAttr
146
116
| ErrorKind :: InvalidGlobPattern ( _)
147
- | ErrorKind :: VersionMismatch => AnnotationType :: Error ,
148
- ErrorKind :: DeprecatedAttr => AnnotationType :: Warning ,
117
+ | ErrorKind :: VersionMismatch => Level :: Error ,
118
+ ErrorKind :: DeprecatedAttr => Level :: Warning ,
149
119
}
150
120
}
0 commit comments