|
1 | | -use crate::config::FileName; |
2 | 1 | use crate::formatting::FormattingError; |
3 | 2 | use crate::{ErrorKind, FormatReport}; |
4 | | -use annotate_snippets::display_list::DisplayList; |
5 | | -use annotate_snippets::formatter::DisplayListFormatter; |
| 3 | +use annotate_snippets::display_list::{DisplayList, FormatOptions}; |
6 | 4 | use annotate_snippets::snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation}; |
7 | 5 | use std::fmt::{self, Display}; |
8 | 6 |
|
@@ -48,115 +46,93 @@ pub struct FormatReportFormatter<'a> { |
48 | 46 |
|
49 | 47 | impl<'a> Display for FormatReportFormatter<'a> { |
50 | 48 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
51 | | - let formatter = DisplayListFormatter::new(self.enable_colors, false); |
52 | 49 | let errors_by_file = &self.report.internal.borrow().0; |
53 | 50 |
|
| 51 | + let opt = FormatOptions { |
| 52 | + color: self.enable_colors, |
| 53 | + ..Default::default() |
| 54 | + }; |
| 55 | + |
54 | 56 | for (file, errors) in errors_by_file { |
55 | 57 | for error in errors { |
56 | | - let snippet = formatting_error_to_snippet(file, error); |
57 | | - writeln!(f, "{}\n", formatter.format(&DisplayList::from(snippet)))?; |
| 58 | + let error_kind = error.kind.to_string(); |
| 59 | + let title = Some(Annotation { |
| 60 | + id: if error.is_internal() { |
| 61 | + Some("internal") |
| 62 | + } else { |
| 63 | + None |
| 64 | + }, |
| 65 | + label: Some(&error_kind), |
| 66 | + annotation_type: error_kind_to_snippet_annotation_type(&error.kind), |
| 67 | + }); |
| 68 | + |
| 69 | + let message_suffix = error.msg_suffix(); |
| 70 | + let footer = if !message_suffix.is_empty() { |
| 71 | + Some(Annotation { |
| 72 | + id: None, |
| 73 | + label: Some(message_suffix), |
| 74 | + annotation_type: AnnotationType::Note, |
| 75 | + }) |
| 76 | + } else { |
| 77 | + None |
| 78 | + }; |
| 79 | + |
| 80 | + let origin = format!("{}:{}", file, error.line); |
| 81 | + let slice = Slice { |
| 82 | + source: &error.line_buffer.clone(), |
| 83 | + line_start: error.line, |
| 84 | + origin: Some(origin.as_str()), |
| 85 | + fold: false, |
| 86 | + annotations: slice_annotation(error).into_iter().collect(), |
| 87 | + }; |
| 88 | + |
| 89 | + let snippet = Snippet { |
| 90 | + title, |
| 91 | + footer: footer.into_iter().collect(), |
| 92 | + slices: vec![slice], |
| 93 | + opt, |
| 94 | + }; |
| 95 | + writeln!(f, "{}\n", DisplayList::from(snippet))?; |
58 | 96 | } |
59 | 97 | } |
60 | 98 |
|
61 | 99 | if !errors_by_file.is_empty() { |
62 | | - let snippet = formatting_failure_snippet(self.report.warning_count()); |
63 | | - writeln!(f, "{}", formatter.format(&DisplayList::from(snippet)))?; |
| 100 | + let label = format!( |
| 101 | + "rustfmt has failed to format. See previous {} errors.", |
| 102 | + self.report.warning_count() |
| 103 | + ); |
| 104 | + let snippet = Snippet { |
| 105 | + title: Some(Annotation { |
| 106 | + id: None, |
| 107 | + label: Some(&label), |
| 108 | + annotation_type: AnnotationType::Warning, |
| 109 | + }), |
| 110 | + footer: Vec::new(), |
| 111 | + slices: Vec::new(), |
| 112 | + opt, |
| 113 | + }; |
| 114 | + writeln!(f, "{}", DisplayList::from(snippet))?; |
64 | 115 | } |
65 | 116 |
|
66 | 117 | Ok(()) |
67 | 118 | } |
68 | 119 | } |
69 | 120 |
|
70 | | -fn formatting_failure_snippet(warning_count: usize) -> Snippet { |
71 | | - Snippet { |
72 | | - title: Some(Annotation { |
73 | | - id: None, |
74 | | - label: Some(format!( |
75 | | - "rustfmt has failed to format. See previous {} errors.", |
76 | | - warning_count |
77 | | - )), |
78 | | - annotation_type: AnnotationType::Warning, |
79 | | - }), |
80 | | - footer: Vec::new(), |
81 | | - slices: Vec::new(), |
82 | | - } |
83 | | -} |
84 | | - |
85 | | -fn formatting_error_to_snippet(file: &FileName, error: &FormattingError) -> Snippet { |
86 | | - let slices = vec![snippet_code_slice(file, error)]; |
87 | | - let title = Some(snippet_title(error)); |
88 | | - let footer = snippet_footer(error).into_iter().collect(); |
89 | | - |
90 | | - Snippet { |
91 | | - title, |
92 | | - footer, |
93 | | - slices, |
94 | | - } |
95 | | -} |
96 | | - |
97 | | -fn snippet_title(error: &FormattingError) -> Annotation { |
98 | | - let annotation_type = error_kind_to_snippet_annotation_type(&error.kind); |
99 | | - |
100 | | - Annotation { |
101 | | - id: title_annotation_id(error), |
102 | | - label: Some(error.kind.to_string()), |
103 | | - annotation_type, |
104 | | - } |
105 | | -} |
106 | | - |
107 | | -fn snippet_footer(error: &FormattingError) -> Option<Annotation> { |
108 | | - let message_suffix = error.msg_suffix(); |
109 | | - |
110 | | - if !message_suffix.is_empty() { |
111 | | - Some(Annotation { |
112 | | - id: None, |
113 | | - label: Some(message_suffix.to_string()), |
114 | | - annotation_type: AnnotationType::Note, |
115 | | - }) |
116 | | - } else { |
117 | | - None |
118 | | - } |
119 | | -} |
120 | | - |
121 | | -fn snippet_code_slice(file: &FileName, error: &FormattingError) -> Slice { |
122 | | - let annotations = slice_annotation(error).into_iter().collect(); |
123 | | - let origin = Some(format!("{}:{}", file, error.line)); |
124 | | - let source = error.line_buffer.clone(); |
125 | | - |
126 | | - Slice { |
127 | | - source, |
128 | | - line_start: error.line, |
129 | | - origin, |
130 | | - fold: false, |
131 | | - annotations, |
132 | | - } |
133 | | -} |
134 | | - |
135 | | -fn slice_annotation(error: &FormattingError) -> Option<SourceAnnotation> { |
| 121 | +fn slice_annotation(error: &FormattingError) -> Option<SourceAnnotation<'_>> { |
136 | 122 | let (range_start, range_length) = error.format_len(); |
137 | 123 | let range_end = range_start + range_length; |
138 | 124 |
|
139 | 125 | if range_length > 0 { |
140 | 126 | Some(SourceAnnotation { |
141 | 127 | annotation_type: AnnotationType::Error, |
142 | 128 | range: (range_start, range_end), |
143 | | - label: String::new(), |
| 129 | + label: "", |
144 | 130 | }) |
145 | 131 | } else { |
146 | 132 | None |
147 | 133 | } |
148 | 134 | } |
149 | 135 |
|
150 | | -fn title_annotation_id(error: &FormattingError) -> Option<String> { |
151 | | - const INTERNAL_ERROR_ID: &str = "internal"; |
152 | | - |
153 | | - if error.is_internal() { |
154 | | - Some(INTERNAL_ERROR_ID.to_string()) |
155 | | - } else { |
156 | | - None |
157 | | - } |
158 | | -} |
159 | | - |
160 | 136 | fn error_kind_to_snippet_annotation_type(error_kind: &ErrorKind) -> AnnotationType { |
161 | 137 | match error_kind { |
162 | 138 | ErrorKind::LineOverflow(..) |
|
0 commit comments