|
1 |
| -use crate::config::FileName; |
2 | 1 | use crate::{
|
3 | 2 | formatting::report::FormatReport,
|
4 | 3 | result::{ErrorKind, FormatError},
|
5 | 4 | };
|
6 |
| -use annotate_snippets::display_list::DisplayList; |
7 |
| -use annotate_snippets::formatter::DisplayListFormatter; |
| 5 | +use annotate_snippets::display_list::{DisplayList, FormatOptions}; |
8 | 6 | use annotate_snippets::snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation};
|
9 | 7 | use std::fmt::{self, Display};
|
10 | 8 |
|
@@ -50,85 +48,71 @@ pub struct FormatReportFormatter<'a> {
|
50 | 48 |
|
51 | 49 | impl<'a> Display for FormatReportFormatter<'a> {
|
52 | 50 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
53 |
| - let formatter = DisplayListFormatter::new(self.enable_colors, false); |
54 |
| - |
| 51 | + let opt = FormatOptions { |
| 52 | + color: self.enable_colors, |
| 53 | + ..Default::default() |
| 54 | + }; |
55 | 55 | for (file, errors) in self.report.format_result_as_rc().borrow().iter() {
|
56 | 56 | for error in errors.errors_excluding_macro() {
|
57 |
| - let snippet = formatting_error_to_snippet(file, error); |
58 |
| - writeln!(f, "{}\n", formatter.format(&DisplayList::from(snippet)))?; |
| 57 | + let annotation_type = error_kind_to_snippet_annotation_type(&error.kind()); |
| 58 | + let label = error.kind().to_string(); |
| 59 | + let title = Annotation { |
| 60 | + id: None, |
| 61 | + label: Some(&label), |
| 62 | + annotation_type, |
| 63 | + }; |
| 64 | + let origin = error |
| 65 | + .line_num() |
| 66 | + .as_ref() |
| 67 | + .map(|line_num| format!("{}:{}", file, line_num)); |
| 68 | + let slice = Slice { |
| 69 | + source: error.line_str().unwrap_or(""), |
| 70 | + line_start: error.line_num().unwrap_or(0), |
| 71 | + origin: origin.as_ref().map(|s| s.as_str()), |
| 72 | + fold: false, |
| 73 | + annotations: slice_annotation(error).into_iter().collect(), |
| 74 | + }; |
| 75 | + |
| 76 | + let snippet = Snippet { |
| 77 | + title: Some(title), |
| 78 | + slices: vec![slice], |
| 79 | + footer: vec![], |
| 80 | + opt, |
| 81 | + }; |
| 82 | + writeln!(f, "{}\n", DisplayList::from(snippet))?; |
59 | 83 | }
|
60 | 84 | }
|
61 | 85 |
|
62 | 86 | if self.report.has_errors() {
|
63 |
| - let snippet = formatting_failure_snippet(self.report.warning_count()); |
64 |
| - writeln!(f, "{}", formatter.format(&DisplayList::from(snippet)))?; |
| 87 | + let label = format!( |
| 88 | + "rustfmt has failed to format. See previous {} errors.", |
| 89 | + self.report.warning_count() |
| 90 | + ); |
| 91 | + let snippet = Snippet { |
| 92 | + title: Some(Annotation { |
| 93 | + id: None, |
| 94 | + label: Some(&label), |
| 95 | + annotation_type: AnnotationType::Warning, |
| 96 | + }), |
| 97 | + slices: Vec::new(), |
| 98 | + footer: vec![], |
| 99 | + opt, |
| 100 | + }; |
| 101 | + writeln!(f, "{}", DisplayList::from(snippet))?; |
65 | 102 | }
|
66 | 103 |
|
67 | 104 | Ok(())
|
68 | 105 | }
|
69 | 106 | }
|
70 | 107 |
|
71 |
| -fn formatting_failure_snippet(warning_count: usize) -> Snippet { |
72 |
| - Snippet { |
73 |
| - title: Some(Annotation { |
74 |
| - id: None, |
75 |
| - label: Some(format!( |
76 |
| - "rustfmt has failed to format. See previous {} errors.", |
77 |
| - warning_count |
78 |
| - )), |
79 |
| - annotation_type: AnnotationType::Warning, |
80 |
| - }), |
81 |
| - slices: Vec::new(), |
82 |
| - footer: vec![], |
83 |
| - } |
84 |
| -} |
85 |
| - |
86 |
| -fn formatting_error_to_snippet(file: &FileName, error: &FormatError) -> Snippet { |
87 |
| - let slices = vec![snippet_code_slice(file, error)]; |
88 |
| - let title = Some(snippet_title(error)); |
89 |
| - |
90 |
| - Snippet { |
91 |
| - title, |
92 |
| - slices, |
93 |
| - footer: vec![], |
94 |
| - } |
95 |
| -} |
96 |
| - |
97 |
| -fn snippet_title(error: &FormatError) -> Annotation { |
98 |
| - let annotation_type = error_kind_to_snippet_annotation_type(&error.kind()); |
99 |
| - |
100 |
| - Annotation { |
101 |
| - id: None, |
102 |
| - label: Some(error.kind().to_string()), |
103 |
| - annotation_type, |
104 |
| - } |
105 |
| -} |
106 |
| - |
107 |
| -fn snippet_code_slice(file: &FileName, error: &FormatError) -> Slice { |
108 |
| - let annotations = slice_annotation(error).into_iter().collect(); |
109 |
| - let origin = error |
110 |
| - .line_num() |
111 |
| - .as_ref() |
112 |
| - .map(|line_num| format!("{}:{}", file, line_num)); |
113 |
| - let source = error.line_str().unwrap_or("").to_owned(); |
114 |
| - |
115 |
| - Slice { |
116 |
| - source, |
117 |
| - line_start: error.line_num().unwrap_or(0), |
118 |
| - origin, |
119 |
| - fold: false, |
120 |
| - annotations, |
121 |
| - } |
122 |
| -} |
123 |
| - |
124 |
| -fn slice_annotation(error: &FormatError) -> Option<SourceAnnotation> { |
| 108 | +fn slice_annotation(error: &FormatError) -> Option<SourceAnnotation<'static>> { |
125 | 109 | match error.format_len() {
|
126 | 110 | Some((range_start, range_length)) if range_length > 0 => {
|
127 | 111 | let range_end = range_start + range_length;
|
128 | 112 | Some(SourceAnnotation {
|
129 | 113 | annotation_type: AnnotationType::Error,
|
130 | 114 | range: (range_start, range_end),
|
131 |
| - label: String::new(), |
| 115 | + label: "", |
132 | 116 | })
|
133 | 117 | }
|
134 | 118 | _ => None,
|
|
0 commit comments