1
Fork 0

On long spans, trim the middle of them to make them fit in the terminal width

When encountering a single line span that is wider than the terminal, we keep context at the start and end of the span but otherwise remove the code from the middle. This is somewhat independent from whether the left and right margins of the output have been trimmed as well.

```
error[E0308]: mismatched types
  --> $DIR/long-span.rs:6:15
   |
LL | ... = [0, 0, 0, 0, ..., 0, 0];
   |       ^^^^^^^^^^^^^...^^^^^^^ expected `u8`, found `[{integer}; 1681]`
```

Address part of #137680 (missing handling of the long suggestion). Fix #125581.
This commit is contained in:
Esteban Küber 2025-02-27 21:11:22 +00:00
parent d975bd3a67
commit 72326bfe40
9 changed files with 86 additions and 4 deletions

View file

@ -1238,6 +1238,33 @@ impl HumanEmitter {
);
}
}
// We look for individual *long* spans, and we trim the *middle*, so that we render
// LL | ...= [0, 0, 0, ..., 0, 0];
// | ^^^^^^^^^^...^^^^^^^ expected `&[u8]`, found `[{integer}; 1680]`
for &(pos, annotation) in &annotations_position {
let AnnotationType::Singleline = annotation.annotation_type else { continue };
let width = annotation.end_col.display - annotation.start_col.display;
if pos == 0 && width > margin.column_width && width > 10 {
// If the terminal is *too* small, we keep at least a tiny bit of the span for
// display.
let pad = max(margin.column_width / 2, 5);
// Code line
buffer.replace(
line_offset,
annotation.start_col.file + pad,
annotation.end_col.file - pad,
self.margin(),
);
// Underline line
buffer.replace(
line_offset + 1,
annotation.start_col.file + pad,
annotation.end_col.file - pad,
self.margin(),
);
}
}
annotations_position
.iter()
.filter_map(|&(_, annotation)| match annotation.annotation_type {

View file

@ -89,6 +89,16 @@ impl StyledBuffer {
}
}
pub(crate) fn replace(&mut self, line: usize, start: usize, end: usize, string: &str) {
if start == end {
return;
}
let _ = self.lines[line].drain(start..(end - string.chars().count()));
for (i, c) in string.chars().enumerate() {
self.lines[line][start + i] = StyledChar::new(c, Style::LineNumber);
}
}
/// For given `line` inserts `string` with `style` before old content of that line,
/// adding lines if needed
pub(crate) fn prepend(&mut self, line: usize, string: &str, style: Style) {

View file

@ -0,0 +1,9 @@
error[E0308]: mismatched types
╭▸ $DIR/long-span.rs:7:15
LL │ …u8 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]…
╰╴ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━…━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ expected `u8`, found `[{integer}; 1680]`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0308`.

View file

@ -0,0 +1,9 @@
error[E0308]: mismatched types
--> $DIR/long-span.rs:7:15
|
LL | ... = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,... 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `u8`, found `[{integer}; 1680]`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0308`.

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,9 @@
error[E0308]: mismatched types
╭▸ $DIR/long-span.rs:7:15
LL │ …u8 = [0, 0, 0…0]…
╰╴ ━━━━━━━━…━━ expected `u8`, found `[{integer}; 1680]`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0308`.

View file

@ -0,0 +1,9 @@
error[E0308]: mismatched types
--> $DIR/long-span.rs:7:15
|
LL | ... = [0, 0, 0......
| ^^^^^^^^...^^ expected `u8`, found `[{integer}; 1680]`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0308`.

View file

@ -1,8 +1,8 @@
error: too many `#` symbols: raw strings may be delimited by up to 255 `#` symbols, but found 256
--> $DIR/too-many-hash.rs:4:19
|
LL | ... = r################################################################################################################################################################################################################################################################"very raw"##############################################################################################################################################################################################################################################################...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | ... = r############################################################################...#############################################################...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error

View file

@ -241,8 +241,8 @@ LL | demo2!(#"foo"## #);
error: invalid string literal
--> $DIR/reserved-guarded-strings.rs:71:12
|
LL | ...n!(####################################################################################################################################################################################################################################################################"foo...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | ...n!(######################################################################...#################################################################"foo...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: unprefixed guarded string literals are reserved for future use since Rust 2024
help: consider inserting whitespace here