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) {