Skip to content

Commit 944dc7f

Browse files
committed
feat: Right align line numbers
1 parent e3514bd commit 944dc7f

File tree

6 files changed

+58
-58
lines changed

6 files changed

+58
-58
lines changed

compiler/rustc_errors/src/emitter.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -713,8 +713,7 @@ impl HumanEmitter {
713713
Style::LineNumber,
714714
);
715715
}
716-
buffer.puts(line_offset, 0, &self.maybe_anonymized(line_index), Style::LineNumber);
717-
716+
self.draw_line_num(buffer, line_index, line_offset, width_offset - 3);
718717
self.draw_col_separator_no_space(buffer, line_offset, width_offset - 2);
719718
left
720719
}
@@ -2128,12 +2127,7 @@ impl HumanEmitter {
21282127
// Account for a suggestion to completely remove a line(s) with whitespace (#94192).
21292128
let line_end = sm.lookup_char_pos(parts[0].span.hi()).line;
21302129
for line in line_start..=line_end {
2131-
buffer.puts(
2132-
row_num - 1 + line - line_start,
2133-
0,
2134-
&self.maybe_anonymized(line),
2135-
Style::LineNumber,
2136-
);
2130+
self.draw_line_num(&mut buffer, line, row_num -1 + line - line_start, max_line_num_len);
21372131
buffer.puts(
21382132
row_num - 1 + line - line_start,
21392133
max_line_num_len + 1,
@@ -2612,12 +2606,7 @@ impl HumanEmitter {
26122606
// For more info: https://github.com/rust-lang/rust/issues/92741
26132607
let lines_to_remove = file_lines.lines.iter().take(file_lines.lines.len() - 1);
26142608
for (index, line_to_remove) in lines_to_remove.enumerate() {
2615-
buffer.puts(
2616-
*row_num - 1,
2617-
0,
2618-
&self.maybe_anonymized(line_num + index),
2619-
Style::LineNumber,
2620-
);
2609+
self.draw_line_num(buffer, line_num + index, *row_num - 1, max_line_num_len);
26212610
buffer.puts(*row_num - 1, max_line_num_len + 1, "- ", Style::Removal);
26222611
let line = normalize_whitespace(
26232612
&file_lines.file.get_line(line_to_remove.line_index).unwrap(),
@@ -2634,12 +2623,7 @@ impl HumanEmitter {
26342623
let last_line_index = file_lines.lines[file_lines.lines.len() - 1].line_index;
26352624
let last_line = &file_lines.file.get_line(last_line_index).unwrap();
26362625
if last_line != line_to_add {
2637-
buffer.puts(
2638-
*row_num - 1,
2639-
0,
2640-
&self.maybe_anonymized(line_num + file_lines.lines.len() - 1),
2641-
Style::LineNumber,
2642-
);
2626+
self.draw_line_num(buffer, line_num + file_lines.lines.len() - 1, *row_num - 1, max_line_num_len);
26432627
buffer.puts(*row_num - 1, max_line_num_len + 1, "- ", Style::Removal);
26442628
buffer.puts(
26452629
*row_num - 1,
@@ -2661,7 +2645,7 @@ impl HumanEmitter {
26612645
// 2 - .await
26622646
// |
26632647
// *row_num -= 1;
2664-
buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber);
2648+
self.draw_line_num(buffer, line_num, *row_num, max_line_num_len);
26652649
buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition);
26662650
buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle);
26672651
} else {
@@ -2671,7 +2655,7 @@ impl HumanEmitter {
26712655
*row_num -= 2;
26722656
}
26732657
} else if is_multiline {
2674-
buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber);
2658+
self.draw_line_num(buffer, line_num, *row_num, max_line_num_len);
26752659
match &highlight_parts {
26762660
[SubstitutionHighlight { start: 0, end }] if *end == line_to_add.len() => {
26772661
buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition);
@@ -2702,11 +2686,11 @@ impl HumanEmitter {
27022686
Style::NoStyle,
27032687
);
27042688
} else if let DisplaySuggestion::Add = show_code_change {
2705-
buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber);
2689+
self.draw_line_num(buffer, line_num, *row_num, max_line_num_len);
27062690
buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition);
27072691
buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle);
27082692
} else {
2709-
buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber);
2693+
self.draw_line_num(buffer, line_num, *row_num, max_line_num_len);
27102694
self.draw_col_separator(buffer, *row_num, max_line_num_len + 1);
27112695
buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle);
27122696
}
@@ -3016,6 +3000,22 @@ impl HumanEmitter {
30163000
OutputTheme::Unicode => "…",
30173001
}
30183002
}
3003+
3004+
fn draw_line_num(
3005+
&self,
3006+
buffer: &mut StyledBuffer,
3007+
line_num: usize,
3008+
line_offset: usize,
3009+
max_line_num_len: usize,
3010+
) {
3011+
let line_num = self.maybe_anonymized(line_num);
3012+
buffer.puts(
3013+
line_offset,
3014+
max_line_num_len.saturating_sub(str_width(&line_num)),
3015+
&line_num,
3016+
Style::LineNumber,
3017+
);
3018+
}
30193019
}
30203020

30213021
#[derive(Debug, Clone, Copy)]

compiler/rustc_parse/src/parser/tests.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,15 +2114,15 @@ fn foo() {
21142114
error: foo
21152115
--> test.rs:3:6
21162116
|
2117-
3 | X0 Y0 Z0
2117+
3 | X0 Y0 Z0
21182118
| _______^
2119-
4 | | X1 Y1 Z1
2119+
4 | | X1 Y1 Z1
21202120
| | ____^____-
21212121
| ||____|
21222122
| | `X` is a good letter
2123-
5 | | 1
2124-
6 | | 2
2125-
7 | | 3
2123+
5 | | 1
2124+
6 | | 2
2125+
7 | | 3
21262126
... |
21272127
15 | | X2 Y2 Z2
21282128
16 | | X3 Y3 Z3
@@ -2133,15 +2133,15 @@ error: foo
21332133
error: foo
21342134
╭▸ test.rs:3:6
21352135
2136-
3 │ X0 Y0 Z0
2136+
3 │ X0 Y0 Z0
21372137
│ ┏━━━━━━━┛
2138-
4 │ ┃ X1 Y1 Z1
2138+
4 │ ┃ X1 Y1 Z1
21392139
│ ┃┌────╿────┘
21402140
│ ┗│━━━━┥
21412141
│ │ `X` is a good letter
2142-
5 │ │ 1
2143-
6 │ │ 2
2144-
7 │ │ 3
2142+
5 │ │ 1
2143+
6 │ │ 2
2144+
7 │ │ 3
21452145
‡ │
21462146
15 │ │ X2 Y2 Z2
21472147
16 │ │ X3 Y3 Z3
@@ -2189,15 +2189,15 @@ fn foo() {
21892189
error: foo
21902190
--> test.rs:3:6
21912191
|
2192-
3 | X0 Y0 Z0
2192+
3 | X0 Y0 Z0
21932193
| _______^
2194-
4 | | 1
2195-
5 | | 2
2196-
6 | | 3
2197-
7 | | X1 Y1 Z1
2194+
4 | | 1
2195+
5 | | 2
2196+
6 | | 3
2197+
7 | | X1 Y1 Z1
21982198
| | _________-
2199-
8 | || 4
2200-
9 | || 5
2199+
8 | || 4
2200+
9 | || 5
22012201
10 | || 6
22022202
11 | || X2 Y2 Z2
22032203
| ||__________- `Z` is a good letter too
@@ -2211,15 +2211,15 @@ error: foo
22112211
error: foo
22122212
╭▸ test.rs:3:6
22132213
2214-
3 │ X0 Y0 Z0
2214+
3 │ X0 Y0 Z0
22152215
│ ┏━━━━━━━┛
2216-
4 │ ┃ 1
2217-
5 │ ┃ 2
2218-
6 │ ┃ 3
2219-
7 │ ┃ X1 Y1 Z1
2216+
4 │ ┃ 1
2217+
5 │ ┃ 2
2218+
6 │ ┃ 3
2219+
7 │ ┃ X1 Y1 Z1
22202220
│ ┃┌─────────┘
2221-
8 │ ┃│ 4
2222-
9 │ ┃│ 5
2221+
8 │ ┃│ 4
2222+
9 │ ┃│ 5
22232223
10 │ ┃│ 6
22242224
11 │ ┃│ X2 Y2 Z2
22252225
│ ┃└──────────┘ `Z` is a good letter too

tests/run-make/crate-loading-crate-depends-on-itself/foo.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ error[E0277]: the trait bound `foo::Struct: Trait` is not satisfied
77
note: there are multiple different versions of crate `foo` in the dependency graph
88
--> foo-current.rs:7:1
99
|
10-
4 | extern crate foo;
10+
4 | extern crate foo;
1111
| ----------------- one version of crate `foo` used here, as a direct dependency of the current crate
12-
5 |
13-
6 | pub struct Struct;
12+
5 |
13+
6 | pub struct Struct;
1414
| ----------------- this type implements the required trait
15-
7 | pub trait Trait {}
15+
7 | pub trait Trait {}
1616
| ^^^^^^^^^^^^^^^ this is the required trait
1717
|
1818
::: foo-prev.rs:X:Y
1919
|
20-
4 | pub struct Struct;
20+
4 | pub struct Struct;
2121
| ----------------- this type doesn't implement the required trait
22-
5 | pub trait Trait {}
22+
5 | pub trait Trait {}
2323
| --------------- this is the found trait
2424
= note: two types coming from two different versions of the same crate are different types even if they look the same
2525
= help: you can use `cargo tree` to explore your dependency tree

tests/rustdoc-ui/doctest/standalone-warning-2024.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ error: unknown attribute `standalone`
1515
note: the lint level is defined here
1616
--> $DIR/standalone-warning-2024.rs:9:9
1717
|
18-
9 | #![deny(warnings)]
18+
9 | #![deny(warnings)]
1919
| ^^^^^^^^
2020
= note: `#[deny(rustdoc::invalid_codeblock_attributes)]` implied by `#[deny(warnings)]`
2121

tests/ui/compiletest-self-test/ui-testing-optout.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ error[E0412]: cannot find type `D` in this scope
1616
error[E0412]: cannot find type `F` in this scope
1717
--> $DIR/ui-testing-optout.rs:92:10
1818
|
19-
4 | type A = B;
19+
4 | type A = B;
2020
| ----------- similarly named type alias `A` defined here
2121
...
2222
92 | type E = F;

tests/ui/modules/issue-107649.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ error[E0277]: `Dummy` doesn't implement `Debug`
99
help: consider annotating `Dummy` with `#[derive(Debug)]`
1010
--> $DIR/auxiliary/dummy_lib.rs:2:1
1111
|
12-
2 + #[derive(Debug)]
13-
3 | pub struct Dummy;
12+
2 + #[derive(Debug)]
13+
3 | pub struct Dummy;
1414
|
1515

1616
error: aborting due to 1 previous error

0 commit comments

Comments
 (0)