Skip to content

Commit 26fc197

Browse files
author
Jonathan Turner
committed
Fix nits. Add styling for primary source. Fix fn:line:col
1 parent fd02f09 commit 26fc197

File tree

3 files changed

+31
-32
lines changed

3 files changed

+31
-32
lines changed

src/libsyntax/errors/emitter.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ mod test {
575575
let str = from_utf8(vec).unwrap();
576576
println!("r#\"\n{}\"#", str);
577577
assert_eq!(str, &r#"
578-
--> dummy.txt (8:1)
578+
--> dummy.txt:8:1
579579
8 |> line8
580580
|> ^^^^^^^^^^^^^
581581
...
@@ -620,7 +620,7 @@ mod test {
620620
let sp34 = " ~~~~~~~ ";
621621

622622
let expect_start = &r#"
623-
--> dummy.txt (1:6)
623+
--> dummy.txt:1:6
624624
1 |> _____aaaaaa____bbbbbb__cccccdd_
625625
|> ------ ------ -------
626626
"#[1..];
@@ -692,7 +692,7 @@ mod test {
692692
let sp5 = span(10, 10, (4, 6));
693693

694694
let expect0 = &r#"
695-
--> dummy.txt (5:1)
695+
--> dummy.txt:5:1
696696
5 |> ccccc
697697
|> -----
698698
...
@@ -705,7 +705,7 @@ mod test {
705705
"#[1..];
706706

707707
let expect = &r#"
708-
--> dummy.txt (1:1)
708+
--> dummy.txt:1:1
709709
1 |> aaaaa
710710
|> -----
711711
...

src/libsyntax/errors/snippet/mod.rs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -253,27 +253,26 @@ impl RenderedLineKind {
253253

254254
impl StyledBuffer {
255255
fn new() -> StyledBuffer {
256-
StyledBuffer { text: Vec::new(), styles: Vec::new() }
256+
StyledBuffer { text: vec![], styles: vec![] }
257257
}
258258

259259
fn render(&self, source_kind: RenderedLineKind) -> Vec<RenderedLine> {
260-
let mut output : Vec<RenderedLine> = Vec::new();
261-
let mut styled_vec : Vec<StyledString> = Vec::new();
260+
let mut output: Vec<RenderedLine> = vec![];
261+
let mut styled_vec: Vec<StyledString> = vec![];
262262

263-
264-
for (row, row_style) in self.text.iter().zip(self.styles.iter()) {
263+
for (row, row_style) in self.text.iter().zip(&self.styles) {
265264
let mut current_style = NoStyle;
266265
let mut current_text = String::new();
267266

268-
for (c, s) in row.iter().zip(row_style.iter()) {
269-
if *s != current_style {
267+
for (&c, &s) in row.iter().zip(row_style) {
268+
if s != current_style {
270269
if !current_text.is_empty() {
271270
styled_vec.push(StyledString { text: current_text, style: current_style });
272271
}
273-
current_style = *s;
272+
current_style = s;
274273
current_text = String::new();
275274
}
276-
current_text.push(*c);
275+
current_text.push(c);
277276
}
278277
if !current_text.is_empty() {
279278
styled_vec.push(StyledString { text: current_text, style: current_style });
@@ -285,23 +284,22 @@ impl StyledBuffer {
285284
} else {
286285
output.push(RenderedLine { text: styled_vec, kind: Annotations });
287286
}
288-
styled_vec = Vec::new();
287+
styled_vec = vec![];
289288
}
290289

291290
output
292291
}
293292

294293
fn putc(&mut self, line: usize, col: usize, chr: char, style: Style) {
295294
while line >= self.text.len() {
296-
self.text.push(Vec::new());
297-
self.styles.push(Vec::new());
295+
self.text.push(vec![]);
296+
self.styles.push(vec![]);
298297
}
299298

300299
if col < self.text[line].len() {
301300
self.text[line][col] = chr;
302301
self.styles[line][col] = style;
303-
}
304-
else {
302+
} else {
305303
while self.text[line].len() < col {
306304
self.text[line].push(' ');
307305
self.styles[line].push(NoStyle);
@@ -319,11 +317,16 @@ impl StyledBuffer {
319317
}
320318
}
321319

320+
fn set_style(&mut self, line: usize, col: usize, style: Style) {
321+
if self.styles.len() > line && self.styles[line].len() > col {
322+
self.styles[line][col] = style;
323+
}
324+
}
325+
322326
fn append(&mut self, line: usize, string: &str, style: Style) {
323327
if line >= self.text.len() {
324328
self.puts(line, 0, string, style);
325-
}
326-
else {
329+
} else {
327330
let col = self.text[line].len();
328331
self.puts(line, col, string, style);
329332
}
@@ -466,7 +469,7 @@ impl FileInfo {
466469
text: lo.file.name.clone(),
467470
style: FileNameStyle,
468471
}, StyledString {
469-
text: format!(" ({}:{})", lo.line, lo.col.0 + 1),
472+
text: format!(":{}:{}", lo.line, lo.col.0 + 1),
470473
style: LineAndColumn,
471474
}],
472475
kind: PrimaryFileName,
@@ -507,12 +510,10 @@ impl FileInfo {
507510
None => annotation.start_col == 0 &&
508511
annotation.end_col == source_string.len()
509512
}
510-
}
511-
else {
513+
} else {
512514
false
513515
}
514-
}
515-
else {
516+
} else {
516517
false
517518
};
518519

@@ -531,12 +532,10 @@ impl FileInfo {
531532

532533
prev_ends_at_eol = annotation_ends_at_eol;
533534
}
534-
}
535-
else {
535+
} else {
536536
if group.len() > 1 {
537537
output.push(RenderedLine::from((String::new(), NoStyle, Elision)));
538-
}
539-
else {
538+
} else {
540539
let mut v: Vec<RenderedLine> =
541540
group.iter().flat_map(|line| self.render_line(line)).collect();
542541
output.append(&mut v);
@@ -596,8 +595,8 @@ impl FileInfo {
596595
for p in annotation.start_col .. annotation.end_col {
597596
if annotation.is_primary {
598597
styled_buffer.putc(1, p, '^', UnderlinePrimary);
599-
}
600-
else {
598+
styled_buffer.set_style(0, p, UnderlinePrimary);
599+
} else {
601600
styled_buffer.putc(1, p, '-', UnderlineSecondary);
602601
}
603602
}

src/libsyntax/errors/snippet/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ fn bar() {
172172

173173
// Note that the `|>` remain aligned across both files:
174174
assert_eq!(&text[..], &r#"
175-
--> foo.rs (3:14)
175+
--> foo.rs:3:14
176176
3 |> vec.push(vec.pop().unwrap());
177177
|> --- ^^^ - c
178178
|> | |

0 commit comments

Comments
 (0)