Skip to content

Commit e7c3b6b

Browse files
committed
fix: Align multi-line secondary titles by level text
1 parent 2af175c commit e7c3b6b

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

src/renderer/mod.rs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -562,17 +562,37 @@ impl Renderer {
562562
is_cont,
563563
);
564564

565-
if title.level.name != Some(None) {
565+
let label_width = if title.level.name != Some(None) {
566566
buffer.append(
567567
buffer_msg_line_offset,
568568
title.level.as_str(),
569569
ElementStyle::MainHeaderMsg,
570570
);
571571
buffer.append(buffer_msg_line_offset, ": ", ElementStyle::NoStyle);
572-
}
572+
title.level.as_str().len() + 2
573+
} else {
574+
0
575+
};
576+
// The extra 3 ` ` is padding that's always needed to align to the
577+
// label i.e. `note: `:
578+
//
579+
// error: message
580+
// --> file.rs:13:20
581+
// |
582+
// 13 | <CODE>
583+
// | ^^^^
584+
// |
585+
// = note: multiline
586+
// message
587+
// ++^^^------
588+
// | | |
589+
// | | |
590+
// | | width of label
591+
// | magic `3`
592+
// `max_line_num_len`
593+
let padding = max_line_num_len + 3 + label_width;
573594

574-
let printed_lines =
575-
self.msgs_to_buffer(buffer, title.title, max_line_num_len, "note", None);
595+
let printed_lines = self.msgs_to_buffer(buffer, title.title, padding, None);
576596
if is_cont && matches!(self.theme, OutputTheme::Unicode) {
577597
// There's another note after this one, associated to the subwindow above.
578598
// We write additional vertical lines to join them:
@@ -660,26 +680,9 @@ impl Renderer {
660680
buffer: &mut StyledBuffer,
661681
title: &str,
662682
padding: usize,
663-
label: &str,
664683
override_style: Option<ElementStyle>,
665684
) -> usize {
666-
// The extra 5 ` ` is padding that's always needed to align to the `note: `:
667-
//
668-
// error: message
669-
// --> file.rs:13:20
670-
// |
671-
// 13 | <CODE>
672-
// | ^^^^
673-
// |
674-
// = note: multiline
675-
// message
676-
// ++^^^----xx
677-
// | | | |
678-
// | | | magic `2`
679-
// | | length of label
680-
// | magic `3`
681-
// `max_line_num_len`
682-
let padding = " ".repeat(padding + label.len() + 5);
685+
let padding = " ".repeat(padding);
683686

684687
let mut line_number = buffer.num_lines().saturating_sub(1);
685688

tests/formatter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2535,7 +2535,7 @@ LL | let s: &str = include_bytes!("file.txt"); //~ ERROR mismatched types
25352535
| |
25362536
| expected due to this
25372537
= expected reference `&str`
2538-
found reference `&'static [u8; 0]`
2538+
found reference `&'static [u8; 0]`
25392539
"#]];
25402540
let renderer = Renderer::plain().anonymized_line_numbers(true);
25412541
assert_data_eq!(renderer.render(input), expected);
@@ -2582,7 +2582,7 @@ LL | let s: &str = include_bytes!("file.txt"); //~ ERROR mismatched types
25822582
| expected due to this
25832583
|
25842584
= custom: expected reference `&str`
2585-
found reference `&'static [u8; 0]`
2585+
found reference `&'static [u8; 0]`
25862586
"#]];
25872587
let renderer = Renderer::plain().anonymized_line_numbers(true);
25882588
assert_data_eq!(renderer.render(input), expected);

0 commit comments

Comments
 (0)