1
Fork 0

Make trimming logic work on more than one span at a time

This commit is contained in:
Esteban Küber 2025-02-28 00:30:45 +00:00
parent c75da0eda6
commit a04c47a0f3
6 changed files with 82 additions and 31 deletions

View file

@ -882,11 +882,16 @@ impl HumanEmitter {
// | x_span // | x_span
// <EMPTY LINE> // <EMPTY LINE>
// //
let mut overlap = vec![false; annotations.len()];
let mut annotations_position = vec![]; let mut annotations_position = vec![];
let mut line_len: usize = 0; let mut line_len: usize = 0;
let mut p = 0; let mut p = 0;
for (i, annotation) in annotations.iter().enumerate() { for (i, annotation) in annotations.iter().enumerate() {
for (j, next) in annotations.iter().enumerate() { for (j, next) in annotations.iter().enumerate() {
if overlaps(next, annotation, 0) && j > i {
overlap[i] = true;
overlap[j] = true;
}
if overlaps(next, annotation, 0) // This label overlaps with another one and both if overlaps(next, annotation, 0) // This label overlaps with another one and both
&& annotation.has_label() // take space (they have text and are not && annotation.has_label() // take space (they have text and are not
&& j > i // multiline lines). && j > i // multiline lines).
@ -1269,13 +1274,17 @@ impl HumanEmitter {
// We look for individual *long* spans, and we trim the *middle*, so that we render // We look for individual *long* spans, and we trim the *middle*, so that we render
// LL | ...= [0, 0, 0, ..., 0, 0]; // LL | ...= [0, 0, 0, ..., 0, 0];
// | ^^^^^^^^^^...^^^^^^^ expected `&[u8]`, found `[{integer}; 1680]` // | ^^^^^^^^^^...^^^^^^^ expected `&[u8]`, found `[{integer}; 1680]`
for &(pos, annotation) in &annotations_position { for (i, (_pos, annotation)) in annotations_position.iter().enumerate() {
// Skip cases where multiple spans overlap each other.
if overlap[i] {
continue;
};
let AnnotationType::Singleline = annotation.annotation_type else { continue }; let AnnotationType::Singleline = annotation.annotation_type else { continue };
let width = annotation.end_col.display - annotation.start_col.display; let width = annotation.end_col.display - annotation.start_col.display;
if pos == 0 && width > margin.column_width && width > 10 { if width > margin.column_width * 2 && width > 10 {
// If the terminal is *too* small, we keep at least a tiny bit of the span for // If the terminal is *too* small, we keep at least a tiny bit of the span for
// display. // display.
let pad = max(margin.column_width / 2, 5); let pad = max(margin.column_width / 3, 5);
// Code line // Code line
buffer.replace( buffer.replace(
line_offset, line_offset,
@ -1800,15 +1809,7 @@ impl HumanEmitter {
width_offset + annotated_file.multiline_depth + 1 width_offset + annotated_file.multiline_depth + 1
}; };
let column_width = if let Some(width) = self.diagnostic_width { let column_width = self.column_width(code_offset);
width.saturating_sub(code_offset)
} else if self.ui_testing || cfg!(miri) {
DEFAULT_COLUMN_WIDTH
} else {
termize::dimensions()
.map(|(w, _)| w.saturating_sub(code_offset))
.unwrap_or(DEFAULT_COLUMN_WIDTH)
};
let margin = Margin::new( let margin = Margin::new(
whitespace_margin, whitespace_margin,
@ -1965,6 +1966,18 @@ impl HumanEmitter {
Ok(()) Ok(())
} }
fn column_width(&self, code_offset: usize) -> usize {
if let Some(width) = self.diagnostic_width {
width.saturating_sub(code_offset)
} else if self.ui_testing || cfg!(miri) {
DEFAULT_COLUMN_WIDTH
} else {
termize::dimensions()
.map(|(w, _)| w.saturating_sub(code_offset))
.unwrap_or(DEFAULT_COLUMN_WIDTH)
}
}
fn emit_suggestion_default( fn emit_suggestion_default(
&mut self, &mut self,
span: &MultiSpan, span: &MultiSpan,

View file

@ -1,9 +1,18 @@
error[E0308]: mismatched types error[E0369]: cannot add `[{integer}; 1680]` to `[{integer}; 1680]`
╭▸ $DIR/long-span.rs:7:15 ╭▸ $DIR/long-span.rs:7:5056
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]; 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, 0, …, 0, 0, 0, 0, 0, 0, 0];
╰╴ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━…━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ expected `u8`, found `[{integer}; 1680]` │ ┬───────────────────────────…────────────────────── ━ ────────────────────────────…────────────────────── [{integer}; 1680]
│ │
╰╴ [{integer}; 1680]
error: aborting due to 1 previous error error[E0308]: mismatched types
╭▸ $DIR/long-span.rs:9:15
LL │ …u8 = [0, 0, 0, 0, 0, 0, 0, 0, 0, …, 0, 0, 0, 0, 0, 0, 0];
╰╴ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━…━━━━━━━━━━━━━━━━━━━━━━ expected `u8`, found `[{integer}; 1680]`
For more information about this error, try `rustc --explain E0308`. error: aborting due to 2 previous errors
Some errors have detailed explanations: E0308, E0369.
For more information about an error, try `rustc --explain E0308`.

View file

@ -1,9 +1,18 @@
error[E0308]: mismatched types error[E0369]: cannot add `[{integer}; 1680]` to `[{integer}; 1680]`
--> $DIR/long-span.rs:7:15 --> $DIR/long-span.rs:7:5056
| |
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, 0]; 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, 0, 0, 0...0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `u8`, found `[{integer}; 1680]` | -----------------------------------------...----------------------------------- ^ -----------------------------------------...----------------------------------- [{integer}; 1680]
| |
| [{integer}; 1680]
error: aborting due to 1 previous error error[E0308]: mismatched types
--> $DIR/long-span.rs:9: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];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `u8`, found `[{integer}; 1680]`
For more information about this error, try `rustc --explain E0308`. error: aborting due to 2 previous errors
Some errors have detailed explanations: E0308, E0369.
For more information about an error, try `rustc --explain E0308`.

File diff suppressed because one or more lines are too long

View file

@ -1,9 +1,18 @@
error[E0369]: cannot add `[{integer}; 1680]` to `[{integer}; 1680]`
╭▸ $DIR/long-span.rs:7:5056
LL │ …u8 = [0, 0, 0…0] + [0, 0, 0…0];
│ ┬───────…── ━ ────────…── [{integer}; 1680]
│ │
╰╴ [{integer}; 1680]
error[E0308]: mismatched types error[E0308]: mismatched types
╭▸ $DIR/long-span.rs:7:15 ╭▸ $DIR/long-span.rs:9:15
LL │ …u8 = [0, 0, 0…0]; LL │ …u8 = [0, 0, 0…0];
╰╴ ━━━━━━━━…━━ expected `u8`, found `[{integer}; 1680]` ╰╴ ━━━━━━━━…━━ expected `u8`, found `[{integer}; 1680]`
error: aborting due to 1 previous error error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0308`. Some errors have detailed explanations: E0308, E0369.
For more information about an error, try `rustc --explain E0308`.

View file

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