Center trim on the span labels and handle unicode

This commit is contained in:
Esteban Küber 2019-08-14 14:09:37 -07:00
parent 45a6be5458
commit de2e9fe2c4
4 changed files with 41 additions and 28 deletions

View File

@ -143,11 +143,13 @@ fn compute(&mut self, max_line_len: usize) {
self.computed_right = self.computed_left + self.column_width;
} else if self.label_right - self.span_left <= self.column_width {
// Attempt to fit the code window considering only the spans and labels.
self.computed_left = self.span_left;
let padding_left = (self.column_width - (self.label_right - self.span_left)) / 2;
self.computed_left = self.span_left - padding_left;
self.computed_right = self.computed_left + self.column_width;
} else if self.span_right - self.span_left <= self.column_width {
// Attempt to fit the code window considering the spans and labels plus padding.
self.computed_left = self.span_left;
let padding_left = (self.column_width - (self.span_right - self.span_left)) / 2;
self.computed_left = self.span_left - padding_left;
self.computed_right = self.computed_left + self.column_width;
} else { // Mostly give up but still don't show the full line.
self.computed_left = self.span_left;
@ -360,13 +362,11 @@ fn draw_line(
) {
let line_len = source_string.len();
// Create the source line we will highlight.
buffer.puts(
line_offset,
code_offset,
// On long lines, we strip the source line
&source_string[margin.left(line_len)..margin.right(line_len)],
Style::Quotation,
);
let left = margin.left(line_len);
let right = margin.right(line_len);
// On long lines, we strip the source line, accounting for unicode.
let code: String = source_string.chars().skip(left).take(right - left).collect();
buffer.puts(line_offset, code_offset, &code, Style::Quotation);
if margin.was_cut_left() {
// We have stripped some code/whitespace from the beginning, make it clear.
buffer.puts(line_offset, code_offset, "...", Style::LineNumber);
@ -419,6 +419,8 @@ fn render_source_line(
let line_offset = buffer.num_lines();
let left = margin.left(source_string.len()); // Left trim
self.draw_line(
buffer,
&source_string,
@ -680,15 +682,15 @@ fn render_source_line(
'_',
line_offset + pos,
width_offset + depth,
code_offset + annotation.start_col - margin.computed_left,
code_offset + annotation.start_col - left,
style,
);
}
_ if self.teach => {
buffer.set_style_range(
line_offset,
code_offset + annotation.start_col - margin.computed_left,
code_offset + annotation.end_col - margin.computed_left,
code_offset + annotation.start_col - left,
code_offset + annotation.end_col - left,
style,
annotation.is_primary,
);
@ -763,15 +765,20 @@ fn render_source_line(
Style::LabelSecondary
};
let (pos, col) = if pos == 0 {
(pos + 1, annotation.end_col + 1 - margin.computed_left)
(pos + 1, if annotation.end_col + 1 > left {
annotation.end_col + 1 - left
} else {
0
})
} else {
(pos + 2, annotation.start_col - margin.computed_left)
(pos + 2, if annotation.start_col > left {
annotation.start_col - left
} else {
0
})
};
if let Some(ref label) = annotation.label {
buffer.puts(line_offset + pos,
code_offset + col,
&label,
style);
buffer.puts(line_offset + pos, code_offset + col, &label, style);
}
}
@ -806,10 +813,16 @@ fn render_source_line(
('-', Style::UnderlineSecondary)
};
for p in annotation.start_col..annotation.end_col {
buffer.putc(line_offset + 1,
code_offset + p - margin.computed_left,
underline,
style);
buffer.putc(
line_offset + 1,
if code_offset + p > left {
code_offset + p - left
} else {
0
},
underline,
style,
);
}
}
annotations_position.iter().filter_map(|&(_, annotation)| {

View File

@ -1,6 +1,6 @@
// ignore-tidy-linelength
fn main() {
let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = 42; let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = ();
let _: usize = 0; let _: usize = 1; let _: usize = 2; let _: usize = 3; let _: usize = 4; let _: usize = 5; let _: usize = 6; let _: usize = 7; let _: usize = 8; let _: usize = 9; let _: usize = 10; let _: usize = 11; let _: usize = 12; let _: usize = 13; let _: usize = 14; let _: usize = 15; let _: () = 42; let _: usize = 0; let _: usize = 1; let _: usize = 2; let _: usize = 3; let _: usize = 4; let _: usize = 5; let _: usize = 6; let _: usize = 7; let _: usize = 8; let _: usize = 9; let _: usize = 10; let _: usize = 11; let _: usize = 12; let _: usize = 13; let _: usize = 14; let _: usize = 15;
//~^ ERROR mismatched types
}

View File

@ -1,8 +1,8 @@
error[E0308]: mismatched types
--> $DIR/non-whitespace-trimming-2.rs:4:241
--> $DIR/non-whitespace-trimming-2.rs:4:311
|
LL | ... = 42; let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = ();...
| ^^ expected (), found integer
LL | ...; let _: usize = 14; let _: usize = 15; let _: () = 42; let _: usize = 0; let _: usize = 1; let _: usize = 2; let _: usize = 3; let _:...
| ^^ expected (), found integer
|
= note: expected type `()`
found type `{integer}`

View File

@ -1,8 +1,8 @@
error[E0308]: mismatched types
--> $DIR/non-whitespace-trimming.rs:4:241
|
LL | ... = 42; let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = ();
| ^^ expected (), found integer
LL | ...) = (); let _: () = (); let _: () = (); let _: () = 42; let _: () = (); let _: () = (); let _: () = (); let _: () = (); let _: () = ()...
| ^^ expected (), found integer
|
= note: expected type `()`
found type `{integer}`