1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
use super::{Report, ReportKind, RemarkKind, Snippet};
use code::{FileMap, LineIdx, Span};
use term_painter::ToStyle;
use term_painter::Color::*;
use std::default::Default;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct PrintOptions {
pub unicode: bool,
pub color: bool,
pub line_wrap: bool,
}
impl Default for PrintOptions {
fn default() -> Self {
PrintOptions {
unicode: true,
color: true,
line_wrap: true,
}
}
}
pub fn print(rep: &Report, src: &FileMap, _: PrintOptions) {
trace!("Printing report: {:#?}", rep);
trace!("Printing with filemap: {:#?}", src);
let title = match rep.kind {
ReportKind::Error => White.bold().bg(Red).paint("ERROR"),
ReportKind::Warning => White.bold().bg(Yellow).paint("WARNING"),
};
let (sep, line) = if let Some(span) = rep.span {
(" : ", if span.is_dummy() {
"<dummy-span>".into()
} else {
let start = src.get_loc(span.lo);
let end = src.get_loc(span.hi);
trace!("Span is from {:?} to {:?}", start, end);
if start.line != end.line {
format!("{}-{}", start.line, end.line)
} else {
start.line.to_string()
}
})
} else {
("", "".into())
};
println!("+---- {} in {}{}{} ----+",
title,
src.filename(),
sep,
Magenta.bold().paint(line)
);
for rem in &rep.remarks {
trace!("Handling Remark {:?}", rem);
let (title, title_len) = match rem.kind {
RemarkKind::Error => (Red.paint("error:"), 6),
RemarkKind::Warning => (Yellow.paint("warning:"), 8),
RemarkKind::Note => (Green.paint("note:"), 5),
};
print!(" =====> {} ", title);
let indent = 6 + 6 + 2 + title_len + 1;
let block_width = 80 - indent;
let mut col = 0;
for word in rem.desc.split_whitespace() {
let word_len = word.chars().count();
if col + word_len >= block_width && col != 0 {
println!("");
print!(" > {0:>1$} ", " ", title_len);
col = 0;
}
print!("{} ", White.bold().paint(word));
col += word_len + 1;
}
println!("");
if let Some(span) = rem.snippet.span() {
print_snippet(src, span, &rem.snippet);
println!("");
}
}
println!("");
}
fn print_snippet(src: &FileMap, span: Span, snippet: &Snippet) {
let start = src.get_loc(span.lo);
let end = src.get_loc(span.hi);
trace!("Span is from {:?} to {:?}", start, end);
if span.is_dummy() {
println!(" {} {} ! no snippet due to <dummy-span>, this is a bug !",
Magenta.bold().paint("?"),
Magenta.bold().paint("|"),
);
}
else if start.line == end.line {
let line_orig = expect_line(src, start.line);
trace!("Printing single line span. Orig line: {:?}", line_orig);
let line = line_orig.replace("\t", " ");
let num_tabs = line_orig[..start.col.0 as usize]
.chars()
.filter(|&c| c == '\t')
.count();
let startcol = start.col.0 as usize + 3*num_tabs;
let endcol = end.col.0 as usize + 3*num_tabs;
let (middle, underline_len, color) = match *snippet {
Snippet::Replace { ref with, .. } => {
(&with[..], with.len(), Green)
},
Snippet::Orig(_) => {
(&line[startcol..endcol], endcol - startcol, Yellow)
},
_ => unreachable!(),
};
println!("{:>#4} {} {}{}{}",
Magenta.bold().paint(start.line),
Magenta.bold().paint("|"),
&line[..startcol],
color.paint(middle),
&line[endcol..],
);
color.with(|| {
println!(" {: <2$}{:^<3$}",
" ", "^",
startcol + 1,
underline_len,
);
});
}
else {
let (lines, color) = match *snippet {
Snippet::Replace { ref with, .. } => {
let mut lines = Vec::new();
if let Some(first_break) = with.find("\n") {
let last_break = with.rfind("\n").unwrap();
let line = expect_line(src, start.line);
let startcol = start.col.0 as usize;
lines.push((&line[..startcol], &with[..first_break], ""));
for line in with[..last_break].lines().skip(1) {
lines.push(("", line, ""));
}
let line = expect_line(src, end.line);
let endcol = end.col.0 as usize;
lines.push(("", &with[last_break + 1..], &line[endcol..]));
(lines, Green)
} else {
let first_line = expect_line(src, start.line);
let startcol = start.col.0 as usize;
let last_line = expect_line(src, end.line);
let endcol = end.col.0 as usize;
(vec![(
&first_line[..startcol],
&with[..],
&last_line[endcol..]
)], Green)
}
},
Snippet::Orig(_) => {
let mut lines = Vec::new();
let line = expect_line(src, start.line);
let startcol = start.col.0 as usize;
lines.push((&line[..startcol], &line[startcol..], ""));
for line_idx in (start.line.0 + 1)..end.line.0 {
let line = expect_line(src, LineIdx(line_idx));
lines.push(("", line, ""));
}
let line = expect_line(src, end.line);
let endcol = end.col.0 as usize;
lines.push(("", &line[..endcol], &line[endcol..]));
(lines, Yellow)
},
_ => unreachable!(),
};
for (i, &(pre, middle, post)) in lines.iter().enumerate() {
println!("{:>#4} {} {}{}{}",
Magenta.bold().paint(start.line + LineIdx(i as u32)),
Magenta.bold().paint("|"),
pre,
color.paint(middle),
post,
);
}
}
}
fn expect_line(src: &FileMap, line: LineIdx) -> &str {
src.get_line(line).expect("`Loc` from FileMap should return a valid line")
}