feat: Right align line numbers

This commit is contained in:
Scott Schafer
2025-05-30 16:03:32 -06:00
parent e3514bde96
commit aa9767290e
6 changed files with 64 additions and 54 deletions

View File

@@ -713,8 +713,7 @@ impl HumanEmitter {
Style::LineNumber, Style::LineNumber,
); );
} }
buffer.puts(line_offset, 0, &self.maybe_anonymized(line_index), Style::LineNumber); self.draw_line_num(buffer, line_index, line_offset, width_offset - 3);
self.draw_col_separator_no_space(buffer, line_offset, width_offset - 2); self.draw_col_separator_no_space(buffer, line_offset, width_offset - 2);
left left
} }
@@ -2128,11 +2127,11 @@ impl HumanEmitter {
// Account for a suggestion to completely remove a line(s) with whitespace (#94192). // Account for a suggestion to completely remove a line(s) with whitespace (#94192).
let line_end = sm.lookup_char_pos(parts[0].span.hi()).line; let line_end = sm.lookup_char_pos(parts[0].span.hi()).line;
for line in line_start..=line_end { for line in line_start..=line_end {
buffer.puts( self.draw_line_num(
&mut buffer,
line,
row_num - 1 + line - line_start, row_num - 1 + line - line_start,
0, max_line_num_len,
&self.maybe_anonymized(line),
Style::LineNumber,
); );
buffer.puts( buffer.puts(
row_num - 1 + line - line_start, row_num - 1 + line - line_start,
@@ -2612,12 +2611,7 @@ impl HumanEmitter {
// For more info: https://github.com/rust-lang/rust/issues/92741 // For more info: https://github.com/rust-lang/rust/issues/92741
let lines_to_remove = file_lines.lines.iter().take(file_lines.lines.len() - 1); let lines_to_remove = file_lines.lines.iter().take(file_lines.lines.len() - 1);
for (index, line_to_remove) in lines_to_remove.enumerate() { for (index, line_to_remove) in lines_to_remove.enumerate() {
buffer.puts( self.draw_line_num(buffer, line_num + index, *row_num - 1, max_line_num_len);
*row_num - 1,
0,
&self.maybe_anonymized(line_num + index),
Style::LineNumber,
);
buffer.puts(*row_num - 1, max_line_num_len + 1, "- ", Style::Removal); buffer.puts(*row_num - 1, max_line_num_len + 1, "- ", Style::Removal);
let line = normalize_whitespace( let line = normalize_whitespace(
&file_lines.file.get_line(line_to_remove.line_index).unwrap(), &file_lines.file.get_line(line_to_remove.line_index).unwrap(),
@@ -2634,11 +2628,11 @@ impl HumanEmitter {
let last_line_index = file_lines.lines[file_lines.lines.len() - 1].line_index; let last_line_index = file_lines.lines[file_lines.lines.len() - 1].line_index;
let last_line = &file_lines.file.get_line(last_line_index).unwrap(); let last_line = &file_lines.file.get_line(last_line_index).unwrap();
if last_line != line_to_add { if last_line != line_to_add {
buffer.puts( self.draw_line_num(
buffer,
line_num + file_lines.lines.len() - 1,
*row_num - 1, *row_num - 1,
0, max_line_num_len,
&self.maybe_anonymized(line_num + file_lines.lines.len() - 1),
Style::LineNumber,
); );
buffer.puts(*row_num - 1, max_line_num_len + 1, "- ", Style::Removal); buffer.puts(*row_num - 1, max_line_num_len + 1, "- ", Style::Removal);
buffer.puts( buffer.puts(
@@ -2661,7 +2655,7 @@ impl HumanEmitter {
// 2 - .await // 2 - .await
// | // |
// *row_num -= 1; // *row_num -= 1;
buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber); self.draw_line_num(buffer, line_num, *row_num, max_line_num_len);
buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition); buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition);
buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle); buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle);
} else { } else {
@@ -2671,7 +2665,7 @@ impl HumanEmitter {
*row_num -= 2; *row_num -= 2;
} }
} else if is_multiline { } else if is_multiline {
buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber); self.draw_line_num(buffer, line_num, *row_num, max_line_num_len);
match &highlight_parts { match &highlight_parts {
[SubstitutionHighlight { start: 0, end }] if *end == line_to_add.len() => { [SubstitutionHighlight { start: 0, end }] if *end == line_to_add.len() => {
buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition); buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition);
@@ -2702,11 +2696,11 @@ impl HumanEmitter {
Style::NoStyle, Style::NoStyle,
); );
} else if let DisplaySuggestion::Add = show_code_change { } else if let DisplaySuggestion::Add = show_code_change {
buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber); self.draw_line_num(buffer, line_num, *row_num, max_line_num_len);
buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition); buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition);
buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle); buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle);
} else { } else {
buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber); self.draw_line_num(buffer, line_num, *row_num, max_line_num_len);
self.draw_col_separator(buffer, *row_num, max_line_num_len + 1); self.draw_col_separator(buffer, *row_num, max_line_num_len + 1);
buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle); buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle);
} }
@@ -3016,6 +3010,22 @@ impl HumanEmitter {
OutputTheme::Unicode => "", OutputTheme::Unicode => "",
} }
} }
fn draw_line_num(
&self,
buffer: &mut StyledBuffer,
line_num: usize,
line_offset: usize,
max_line_num_len: usize,
) {
let line_num = self.maybe_anonymized(line_num);
buffer.puts(
line_offset,
max_line_num_len.saturating_sub(str_width(&line_num)),
&line_num,
Style::LineNumber,
);
}
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]

View File

@@ -2114,15 +2114,15 @@ fn foo() {
error: foo error: foo
--> test.rs:3:6 --> test.rs:3:6
| |
3 | X0 Y0 Z0 3 | X0 Y0 Z0
| _______^ | _______^
4 | | X1 Y1 Z1 4 | | X1 Y1 Z1
| | ____^____- | | ____^____-
| ||____| | ||____|
| | `X` is a good letter | | `X` is a good letter
5 | | 1 5 | | 1
6 | | 2 6 | | 2
7 | | 3 7 | | 3
... | ... |
15 | | X2 Y2 Z2 15 | | X2 Y2 Z2
16 | | X3 Y3 Z3 16 | | X3 Y3 Z3
@@ -2133,15 +2133,15 @@ error: foo
error: foo error: foo
test.rs:3:6 test.rs:3:6
3 X0 Y0 Z0 3 X0 Y0 Z0
4 X1 Y1 Z1 4 X1 Y1 Z1
`X` is a good letter `X` is a good letter
5 1 5 1
6 2 6 2
7 3 7 3
15 X2 Y2 Z2 15 X2 Y2 Z2
16 X3 Y3 Z3 16 X3 Y3 Z3
@@ -2189,15 +2189,15 @@ fn foo() {
error: foo error: foo
--> test.rs:3:6 --> test.rs:3:6
| |
3 | X0 Y0 Z0 3 | X0 Y0 Z0
| _______^ | _______^
4 | | 1 4 | | 1
5 | | 2 5 | | 2
6 | | 3 6 | | 3
7 | | X1 Y1 Z1 7 | | X1 Y1 Z1
| | _________- | | _________-
8 | || 4 8 | || 4
9 | || 5 9 | || 5
10 | || 6 10 | || 6
11 | || X2 Y2 Z2 11 | || X2 Y2 Z2
| ||__________- `Z` is a good letter too | ||__________- `Z` is a good letter too
@@ -2211,15 +2211,15 @@ error: foo
error: foo error: foo
test.rs:3:6 test.rs:3:6
3 X0 Y0 Z0 3 X0 Y0 Z0
4 1 4 1
5 2 5 2
6 3 6 3
7 X1 Y1 Z1 7 X1 Y1 Z1
8 4 8 4
9 5 9 5
10 6 10 6
11 X2 Y2 Z2 11 X2 Y2 Z2
`Z` is a good letter too `Z` is a good letter too

View File

@@ -7,19 +7,19 @@ error[E0277]: the trait bound `foo::Struct: Trait` is not satisfied
note: there are multiple different versions of crate `foo` in the dependency graph note: there are multiple different versions of crate `foo` in the dependency graph
--> foo-current.rs:7:1 --> foo-current.rs:7:1
| |
4 | extern crate foo; 4 | extern crate foo;
| ----------------- one version of crate `foo` used here, as a direct dependency of the current crate | ----------------- one version of crate `foo` used here, as a direct dependency of the current crate
5 | 5 |
6 | pub struct Struct; 6 | pub struct Struct;
| ----------------- this type implements the required trait | ----------------- this type implements the required trait
7 | pub trait Trait {} 7 | pub trait Trait {}
| ^^^^^^^^^^^^^^^ this is the required trait | ^^^^^^^^^^^^^^^ this is the required trait
| |
::: foo-prev.rs:X:Y ::: foo-prev.rs:X:Y
| |
4 | pub struct Struct; 4 | pub struct Struct;
| ----------------- this type doesn't implement the required trait | ----------------- this type doesn't implement the required trait
5 | pub trait Trait {} 5 | pub trait Trait {}
| --------------- this is the found trait | --------------- this is the found trait
= note: two types coming from two different versions of the same crate are different types even if they look the same = note: two types coming from two different versions of the same crate are different types even if they look the same
= help: you can use `cargo tree` to explore your dependency tree = help: you can use `cargo tree` to explore your dependency tree

View File

@@ -15,7 +15,7 @@ error: unknown attribute `standalone`
note: the lint level is defined here note: the lint level is defined here
--> $DIR/standalone-warning-2024.rs:9:9 --> $DIR/standalone-warning-2024.rs:9:9
| |
9 | #![deny(warnings)] 9 | #![deny(warnings)]
| ^^^^^^^^ | ^^^^^^^^
= note: `#[deny(rustdoc::invalid_codeblock_attributes)]` implied by `#[deny(warnings)]` = note: `#[deny(rustdoc::invalid_codeblock_attributes)]` implied by `#[deny(warnings)]`

View File

@@ -16,7 +16,7 @@ error[E0412]: cannot find type `D` in this scope
error[E0412]: cannot find type `F` in this scope error[E0412]: cannot find type `F` in this scope
--> $DIR/ui-testing-optout.rs:92:10 --> $DIR/ui-testing-optout.rs:92:10
| |
4 | type A = B; 4 | type A = B;
| ----------- similarly named type alias `A` defined here | ----------- similarly named type alias `A` defined here
... ...
92 | type E = F; 92 | type E = F;

View File

@@ -9,8 +9,8 @@ error[E0277]: `Dummy` doesn't implement `Debug`
help: consider annotating `Dummy` with `#[derive(Debug)]` help: consider annotating `Dummy` with `#[derive(Debug)]`
--> $DIR/auxiliary/dummy_lib.rs:2:1 --> $DIR/auxiliary/dummy_lib.rs:2:1
| |
2 + #[derive(Debug)] 2 + #[derive(Debug)]
3 | pub struct Dummy; 3 | pub struct Dummy;
| |
error: aborting due to 1 previous error error: aborting due to 1 previous error