Skip to content

Commit b59784d

Browse files
author
Zibi Braniecki
committed
More cleanups to the formatter
1 parent 8c64314 commit b59784d

File tree

4 files changed

+52
-82
lines changed

4 files changed

+52
-82
lines changed

src/formatter/mod.rs

+47-77
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
88
pub mod style;
99

10+
use std::cmp;
1011
use display_list::*;
11-
12-
use self::style::{StyleClass, Stylesheet};
12+
use self::style::{Style, StyleClass, Stylesheet};
1313

1414
#[cfg(feature = "ansi_term")]
1515
use stylesheets::color::AnsiTermStylesheet;
@@ -76,25 +76,11 @@ impl DisplayListFormatter {
7676
DisplayLine::Source {
7777
lineno: Some(lineno),
7878
..
79-
} => {
80-
let width = lineno.to_string().len();
81-
if width > max {
82-
width
83-
} else {
84-
max
85-
}
86-
}
79+
} => cmp::max(lineno.to_string().len(), max),
8780
_ => max,
8881
});
8982
let inline_marks_width = dl.body.iter().fold(0, |max, line| match line {
90-
DisplayLine::Source { inline_marks, .. } => {
91-
let width = inline_marks.len();
92-
if width > max {
93-
width
94-
} else {
95-
max
96-
}
97-
}
83+
DisplayLine::Source { inline_marks, .. } => cmp::max(inline_marks.len(), max),
9884
_ => max,
9985
});
10086

@@ -105,24 +91,35 @@ impl DisplayListFormatter {
10591
.join("\n")
10692
}
10793

108-
fn format_annotation_type(&self, annotation_type: &DisplayAnnotationType) -> String {
94+
fn format_annotation_type(&self, annotation_type: &DisplayAnnotationType) -> &'static str {
10995
match annotation_type {
110-
DisplayAnnotationType::Error => "error".to_string(),
111-
DisplayAnnotationType::Warning => "warning".to_string(),
112-
DisplayAnnotationType::Info => "info".to_string(),
113-
DisplayAnnotationType::Note => "note".to_string(),
114-
DisplayAnnotationType::Help => "help".to_string(),
115-
DisplayAnnotationType::None => "".to_string(),
96+
DisplayAnnotationType::Error => "error",
97+
DisplayAnnotationType::Warning => "warning",
98+
DisplayAnnotationType::Info => "info",
99+
DisplayAnnotationType::Note => "note",
100+
DisplayAnnotationType::Help => "help",
101+
DisplayAnnotationType::None => "",
116102
}
117103
}
118104

105+
fn get_annotation_style(&self, annotation_type: &DisplayAnnotationType) -> Box<Style> {
106+
self.stylesheet.get_style(match annotation_type {
107+
DisplayAnnotationType::Error => StyleClass::Error,
108+
DisplayAnnotationType::Warning => StyleClass::Warning,
109+
DisplayAnnotationType::Info => StyleClass::Info,
110+
DisplayAnnotationType::Note => StyleClass::Note,
111+
DisplayAnnotationType::Help => StyleClass::Help,
112+
DisplayAnnotationType::None => StyleClass::None,
113+
})
114+
}
115+
119116
fn format_label(&self, label: &[DisplayTextFragment]) -> String {
120117
let emphasis_style = self.stylesheet.get_style(StyleClass::Emphasis);
121118
label
122119
.iter()
123120
.map(|fragment| match fragment.style {
124121
DisplayTextStyle::Regular => fragment.content.clone(),
125-
DisplayTextStyle::Emphasis => emphasis_style.paint(fragment.content.clone()),
122+
DisplayTextStyle::Emphasis => emphasis_style.paint(&fragment.content),
126123
})
127124
.collect::<Vec<String>>()
128125
.join("")
@@ -134,43 +131,32 @@ impl DisplayListFormatter {
134131
continuation: bool,
135132
in_source: bool,
136133
) -> String {
137-
let style = match annotation.annotation_type {
138-
DisplayAnnotationType::Error => StyleClass::Error,
139-
DisplayAnnotationType::Warning => StyleClass::Warning,
140-
DisplayAnnotationType::Info => StyleClass::Info,
141-
DisplayAnnotationType::Note => StyleClass::Note,
142-
DisplayAnnotationType::Help => StyleClass::Help,
143-
DisplayAnnotationType::None => StyleClass::None,
134+
let color = self.get_annotation_style(&annotation.annotation_type);
135+
let formatted_type = if let Some(ref id) = annotation.id {
136+
format!(
137+
"{}[{}]",
138+
self.format_annotation_type(&annotation.annotation_type),
139+
id
140+
)
141+
} else {
142+
self.format_annotation_type(&annotation.annotation_type)
143+
.to_string()
144144
};
145-
let color = self.stylesheet.get_style(style);
146-
let formatted_type = self.format_annotation_type(&annotation.annotation_type);
147145
let label = self.format_label(&annotation.label);
148146

149147
let label_part = if label.is_empty() {
150148
"".to_string()
151149
} else if in_source {
152-
color.paint(format!(": {}", self.format_label(&annotation.label)))
150+
color.paint(&format!(": {}", self.format_label(&annotation.label)))
153151
} else {
154152
format!(": {}", self.format_label(&annotation.label))
155153
};
156154
if continuation {
157-
let indent = if let Some(ref id) = annotation.id {
158-
formatted_type.len() + id.len() + 4
159-
} else if !formatted_type.is_empty() {
160-
formatted_type.len() + 2
161-
} else {
162-
2
163-
};
155+
let indent = formatted_type.len() + 2;
164156
return format!("{}{}", repeat_char(' ', indent), label);
165157
}
166-
if let Some(ref id) = annotation.id {
167-
format!(
168-
"{}{}",
169-
color.paint(format!("{}[{}]", formatted_type, id)),
170-
label_part
171-
)
172-
} else if !formatted_type.is_empty() {
173-
format!("{}{}", color.paint(formatted_type), label_part)
158+
if !formatted_type.is_empty() {
159+
format!("{}{}", color.paint(&formatted_type), label_part)
174160
} else {
175161
label
176162
}
@@ -201,22 +187,14 @@ impl DisplayListFormatter {
201187
DisplayAnnotationType::Help => '-',
202188
DisplayAnnotationType::None => ' ',
203189
};
204-
let style = match annotation_type {
205-
DisplayAnnotationType::Error => StyleClass::Error,
206-
DisplayAnnotationType::Warning => StyleClass::Warning,
207-
DisplayAnnotationType::Info => StyleClass::Info,
208-
DisplayAnnotationType::Note => StyleClass::Note,
209-
DisplayAnnotationType::Help => StyleClass::Help,
210-
DisplayAnnotationType::None => StyleClass::None,
211-
};
212-
let color = self.stylesheet.get_style(style);
190+
let color = self.get_annotation_style(annotation_type);
213191
let indent_length = match annotation_part {
214192
DisplayAnnotationPart::LabelContinuation => range.1,
215193
DisplayAnnotationPart::Consequitive => range.1,
216194
_ => range.0,
217195
};
218-
let indent = color.paint(repeat_char(indent_char, indent_length + 1));
219-
let marks = color.paint(repeat_char(mark, range.1 - indent_length));
196+
let indent = color.paint(&repeat_char(indent_char, indent_length + 1));
197+
let marks = color.paint(&repeat_char(mark, range.1 - indent_length));
220198
let annotation = self.format_annotation(
221199
annotation,
222200
annotation_part == &DisplayAnnotationPart::LabelContinuation,
@@ -225,7 +203,7 @@ impl DisplayListFormatter {
225203
if annotation.is_empty() {
226204
return Some(format!("{}{}", indent, marks));
227205
}
228-
return Some(format!("{}{} {}", indent, marks, color.paint(annotation)));
206+
return Some(format!("{}{} {}", indent, marks, color.paint(&annotation)));
229207
}
230208
}
231209
}
@@ -245,8 +223,8 @@ impl DisplayListFormatter {
245223
header_type,
246224
} => {
247225
let header_sigil = match header_type {
248-
DisplayHeaderType::Initial => String::from("-->"),
249-
DisplayHeaderType::Continuation => String::from(":::"),
226+
DisplayHeaderType::Initial => "-->",
227+
DisplayHeaderType::Continuation => ":::",
250228
};
251229
let lineno_color = self.stylesheet.get_style(StyleClass::LineNo);
252230

@@ -285,7 +263,7 @@ impl DisplayListFormatter {
285263
format!(
286264
"{} {} {}",
287265
repeat_char(' ', lineno_width),
288-
lineno_color.paint("=".to_string()),
266+
lineno_color.paint("="),
289267
self.format_annotation(annotation, *continuation, false)
290268
)
291269
}
@@ -313,7 +291,7 @@ impl DisplayListFormatter {
313291
let lf = self.format_source_line(line);
314292
let lineno_color = self.stylesheet.get_style(StyleClass::LineNo);
315293

316-
let mut prefix = lineno_color.paint(format!("{} |", lineno));
294+
let mut prefix = lineno_color.paint(&format!("{} |", lineno));
317295

318296
match lf {
319297
Some(lf) => {
@@ -358,16 +336,8 @@ impl DisplayListFormatter {
358336
DisplayMarkType::AnnotationThrough => "|",
359337
DisplayMarkType::AnnotationStart => "/",
360338
};
361-
let style = match mark.annotation_type {
362-
DisplayAnnotationType::Error => StyleClass::Error,
363-
DisplayAnnotationType::Warning => StyleClass::Warning,
364-
DisplayAnnotationType::Info => StyleClass::Info,
365-
DisplayAnnotationType::Note => StyleClass::Note,
366-
DisplayAnnotationType::Help => StyleClass::Help,
367-
DisplayAnnotationType::None => StyleClass::None,
368-
};
369-
let color = self.stylesheet.get_style(style);
370-
color.paint(String::from(sigil))
339+
let color = self.get_annotation_style(&mark.annotation_type);
340+
color.paint(sigil)
371341
})
372342
.collect::<Vec<String>>()
373343
.join(""),

src/formatter/style.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
//! };
2626
//!
2727
//! impl Style for HTMLStyle {
28-
//! fn paint(&self, text: String) -> String {
28+
//! fn paint(&self, text: &str) -> String {
2929
//! format!("{}{}{}", self.prefix, text, self.postfix)
3030
//! }
3131
//!
@@ -84,7 +84,7 @@ pub enum StyleClass {
8484
/// This trait implements a return value for the `Stylesheet::get_style`.
8585
pub trait Style {
8686
/// The method used by the DisplayListFormatter to style the message.
87-
fn paint(&self, text: String) -> String;
87+
fn paint(&self, text: &str) -> String;
8888
/// The method used by the DisplayListFormatter to display the message
8989
/// in bold font.
9090
fn bold(&self) -> Box<Style>;

src/stylesheets/color.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ struct AnsiTermStyleWrapper {
1010
}
1111

1212
impl Style for AnsiTermStyleWrapper {
13-
fn paint(&self, text: String) -> String {
13+
fn paint(&self, text: &str) -> String {
1414
format!("{}", self.style.paint(text))
1515
}
1616

src/stylesheets/no_color.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use formatter::style::{Style, StyleClass, Stylesheet};
33
pub struct NoOpStyle {}
44

55
impl Style for NoOpStyle {
6-
fn paint(&self, text: String) -> String {
7-
text
6+
fn paint(&self, text: &str) -> String {
7+
text.to_string()
88
}
99

1010
fn bold(&self) -> Box<Style> {

0 commit comments

Comments
 (0)