Skip to content

Commit 2e8e73c

Browse files
author
Jonathan Turner
committed
Add in styled_buffer.rs and remove some unused code
1 parent 012ff15 commit 2e8e73c

File tree

4 files changed

+158
-56
lines changed

4 files changed

+158
-56
lines changed

src/librustc/infer/error_reporting.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ use syntax::ast;
9494
use syntax::parse::token;
9595
use syntax::ptr::P;
9696
use syntax_pos::{self, Pos, Span};
97-
use errors::{DiagnosticBuilder, check_old_skool};
97+
use errors::{DiagnosticBuilder, check_old_school};
9898

9999
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
100100
pub fn note_and_explain_region(self,
@@ -485,7 +485,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
485485
"{}",
486486
trace.origin);
487487

488-
if !is_simple_error || check_old_skool() {
488+
if !is_simple_error || check_old_school() {
489489
err.note_expected_found(&"type", &expected, &found);
490490
}
491491

src/librustc_errors/emitter.rs

+21-52
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use self::Destination::*;
1313
use syntax_pos::{COMMAND_LINE_SP, DUMMY_SP, FileMap, Span, MultiSpan, LineInfo, CharPos};
1414
use registry;
1515

16-
use check_old_skool;
16+
use check_old_school;
1717
use {Level, CodeSuggestion, DiagnosticBuilder, CodeMapper};
1818
use RenderSpan::*;
1919
use snippet::{StyledString, Style, FormatMode, Annotation, Line};
@@ -36,7 +36,7 @@ impl Emitter for EmitterWriter {
3636
let old_school = match self.format_mode {
3737
FormatMode::NewErrorFormat => false,
3838
FormatMode::OriginalErrorFormat => true,
39-
FormatMode::EnvironmentSelected => check_old_skool()
39+
FormatMode::EnvironmentSelected => check_old_school()
4040
};
4141

4242
if old_school {
@@ -243,59 +243,32 @@ impl EmitterWriter {
243243
// For this reason, we group the lines into "highlight lines"
244244
// and "annotations lines", where the highlight lines have the `~`.
245245

246-
// let mut highlight_line = Self::whitespace(&source_string);
247-
let old_school = check_old_skool();
248-
249246
// Sort the annotations by (start, end col)
250247
let mut annotations = line.annotations.clone();
251248
annotations.sort();
252249

253250
// Next, create the highlight line.
254251
for annotation in &annotations {
255-
if old_school {
256-
for p in annotation.start_col..annotation.end_col {
257-
if p == annotation.start_col {
258-
buffer.putc(line_offset + 1,
259-
width_offset + p,
260-
'^',
261-
if annotation.is_primary {
262-
Style::UnderlinePrimary
263-
} else {
264-
Style::OldSchoolNote
265-
});
266-
} else {
267-
buffer.putc(line_offset + 1,
268-
width_offset + p,
269-
'~',
270-
if annotation.is_primary {
271-
Style::UnderlinePrimary
272-
} else {
273-
Style::OldSchoolNote
274-
});
252+
for p in annotation.start_col..annotation.end_col {
253+
if annotation.is_primary {
254+
buffer.putc(line_offset + 1,
255+
width_offset + p,
256+
'^',
257+
Style::UnderlinePrimary);
258+
if !annotation.is_minimized {
259+
buffer.set_style(line_offset,
260+
width_offset + p,
261+
Style::UnderlinePrimary);
275262
}
276-
}
277-
} else {
278-
for p in annotation.start_col..annotation.end_col {
279-
if annotation.is_primary {
280-
buffer.putc(line_offset + 1,
281-
width_offset + p,
282-
'^',
283-
Style::UnderlinePrimary);
284-
if !annotation.is_minimized {
285-
buffer.set_style(line_offset,
286-
width_offset + p,
287-
Style::UnderlinePrimary);
288-
}
289-
} else {
290-
buffer.putc(line_offset + 1,
291-
width_offset + p,
292-
'-',
293-
Style::UnderlineSecondary);
294-
if !annotation.is_minimized {
295-
buffer.set_style(line_offset,
296-
width_offset + p,
297-
Style::UnderlineSecondary);
298-
}
263+
} else {
264+
buffer.putc(line_offset + 1,
265+
width_offset + p,
266+
'-',
267+
Style::UnderlineSecondary);
268+
if !annotation.is_minimized {
269+
buffer.set_style(line_offset,
270+
width_offset + p,
271+
Style::UnderlineSecondary);
299272
}
300273
}
301274
}
@@ -311,10 +284,6 @@ impl EmitterWriter {
311284
if labeled_annotations.is_empty() {
312285
return;
313286
}
314-
if old_school {
315-
return;
316-
}
317-
318287
// Now add the text labels. We try, when possible, to stick the rightmost
319288
// annotation at the end of the highlight line:
320289
//

src/librustc_errors/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -756,13 +756,13 @@ pub fn expect<T, M>(diag: &Handler, opt: Option<T>, msg: M) -> T where
756756
///
757757
/// FIXME(#33240)
758758
#[cfg(not(test))]
759-
pub fn check_old_skool() -> bool {
759+
pub fn check_old_school() -> bool {
760760
use std::env;
761761
env::var("RUST_NEW_ERROR_FORMAT").is_err()
762762
}
763763

764764
/// For unit tests, use the new format.
765765
#[cfg(test)]
766-
pub fn check_old_skool() -> bool {
766+
pub fn check_old_school() -> bool {
767767
false
768768
}

src/librustc_errors/styled_buffer.rs

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Code for creating styled buffers
12+
13+
use snippet::{Style, StyledString};
14+
15+
#[derive(Debug)]
16+
pub struct StyledBuffer {
17+
text: Vec<Vec<char>>,
18+
styles: Vec<Vec<Style>>,
19+
}
20+
21+
impl StyledBuffer {
22+
pub fn new() -> StyledBuffer {
23+
StyledBuffer {
24+
text: vec![],
25+
styles: vec![],
26+
}
27+
}
28+
29+
pub fn render(&self) -> Vec<Vec<StyledString>> {
30+
let mut output: Vec<Vec<StyledString>> = vec![];
31+
let mut styled_vec: Vec<StyledString> = vec![];
32+
33+
for (row, row_style) in self.text.iter().zip(&self.styles) {
34+
let mut current_style = Style::NoStyle;
35+
let mut current_text = String::new();
36+
37+
for (&c, &s) in row.iter().zip(row_style) {
38+
if s != current_style {
39+
if !current_text.is_empty() {
40+
styled_vec.push(StyledString {
41+
text: current_text,
42+
style: current_style,
43+
});
44+
}
45+
current_style = s;
46+
current_text = String::new();
47+
}
48+
current_text.push(c);
49+
}
50+
if !current_text.is_empty() {
51+
styled_vec.push(StyledString {
52+
text: current_text,
53+
style: current_style,
54+
});
55+
}
56+
57+
// We're done with the row, push and keep going
58+
output.push(styled_vec);
59+
60+
styled_vec = vec![];
61+
}
62+
63+
output
64+
}
65+
66+
fn ensure_lines(&mut self, line: usize) {
67+
while line >= self.text.len() {
68+
self.text.push(vec![]);
69+
self.styles.push(vec![]);
70+
}
71+
}
72+
73+
pub fn putc(&mut self, line: usize, col: usize, chr: char, style: Style) {
74+
self.ensure_lines(line);
75+
if col < self.text[line].len() {
76+
self.text[line][col] = chr;
77+
self.styles[line][col] = style;
78+
} else {
79+
let mut i = self.text[line].len();
80+
while i < col {
81+
let s = match self.text[0].get(i) {
82+
Some(&'\t') => '\t',
83+
_ => ' ',
84+
};
85+
self.text[line].push(s);
86+
self.styles[line].push(Style::NoStyle);
87+
i += 1;
88+
}
89+
self.text[line].push(chr);
90+
self.styles[line].push(style);
91+
}
92+
}
93+
94+
pub fn puts(&mut self, line: usize, col: usize, string: &str, style: Style) {
95+
let mut n = col;
96+
for c in string.chars() {
97+
self.putc(line, n, c, style);
98+
n += 1;
99+
}
100+
}
101+
102+
pub fn set_style(&mut self, line: usize, col: usize, style: Style) {
103+
if self.styles.len() > line && self.styles[line].len() > col {
104+
self.styles[line][col] = style;
105+
}
106+
}
107+
108+
pub fn prepend(&mut self, line: usize, string: &str, style: Style) {
109+
self.ensure_lines(line);
110+
let string_len = string.len();
111+
112+
// Push the old content over to make room for new content
113+
for _ in 0..string_len {
114+
self.styles[line].insert(0, Style::NoStyle);
115+
self.text[line].insert(0, ' ');
116+
}
117+
118+
self.puts(line, 0, string, style);
119+
}
120+
121+
pub fn append(&mut self, line: usize, string: &str, style: Style) {
122+
if line >= self.text.len() {
123+
self.puts(line, 0, string, style);
124+
} else {
125+
let col = self.text[line].len();
126+
self.puts(line, col, string, style);
127+
}
128+
}
129+
130+
pub fn num_lines(&self) -> usize {
131+
self.text.len()
132+
}
133+
}

0 commit comments

Comments
 (0)