Skip to content

fix: Align multi-line secondary titles by level text #229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 32 additions & 28 deletions src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,23 +555,44 @@ impl Renderer {
buffer.prepend(buffer_msg_line_offset, " ", ElementStyle::NoStyle);
}

if title.level.name != Some(None) {
self.draw_note_separator(
buffer,
buffer_msg_line_offset,
max_line_num_len + 1,
is_cont,
);
self.draw_note_separator(
buffer,
buffer_msg_line_offset,
max_line_num_len + 1,
is_cont,
);

let label_width = if title.level.name != Some(None) {
buffer.append(
buffer_msg_line_offset,
title.level.as_str(),
ElementStyle::MainHeaderMsg,
);
buffer.append(buffer_msg_line_offset, ": ", ElementStyle::NoStyle);
}
title.level.as_str().len() + 2
} else {
0
};
// The extra 3 ` ` is padding that's always needed to align to the
// label i.e. `note: `:
//
// error: message
// --> file.rs:13:20
// |
// 13 | <CODE>
// | ^^^^
// |
// = note: multiline
// message
// ++^^^------
// | | |
// | | |
// | | width of label
// | magic `3`
// `max_line_num_len`
let padding = max_line_num_len + 3 + label_width;

let printed_lines =
self.msgs_to_buffer(buffer, title.title, max_line_num_len, "note", None);
let printed_lines = self.msgs_to_buffer(buffer, title.title, padding, None);
if is_cont && matches!(self.theme, OutputTheme::Unicode) {
// There's another note after this one, associated to the subwindow above.
// We write additional vertical lines to join them:
Expand Down Expand Up @@ -659,26 +680,9 @@ impl Renderer {
buffer: &mut StyledBuffer,
title: &str,
padding: usize,
label: &str,
override_style: Option<ElementStyle>,
) -> usize {
// The extra 5 ` ` is padding that's always needed to align to the `note: `:
//
// error: message
// --> file.rs:13:20
// |
// 13 | <CODE>
// | ^^^^
// |
// = note: multiline
// message
// ++^^^----xx
// | | | |
// | | | magic `2`
// | | length of label
// | magic `3`
// `max_line_num_len`
let padding = " ".repeat(padding + label.len() + 5);
let padding = " ".repeat(padding);

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

Expand Down
93 changes: 93 additions & 0 deletions tests/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2494,3 +2494,96 @@ LL │ �|�␂!5�cc␕␂�Ӻi��WWj�ȥ�'�}�␒�J�ȉ��W
let renderer_unicode = renderer_ascii.theme(OutputTheme::Unicode);
assert_data_eq!(renderer_unicode.render(input), expected_unicode);
}

#[test]
fn secondary_title_no_level_text() {
let source = r#"fn main() {
let b: &[u8] = include_str!("file.txt"); //~ ERROR mismatched types
let s: &str = include_bytes!("file.txt"); //~ ERROR mismatched types
}"#;

let input = Level::ERROR.header("mismatched types").id("E0308").group(
Group::new()
.element(
Snippet::source(source)
.path("$DIR/mismatched-types.rs")
.fold(true)
.annotation(
AnnotationKind::Primary
.span(105..131)
.label("expected `&str`, found `&[u8; 0]`"),
)
.annotation(
AnnotationKind::Context
.span(98..102)
.label("expected due to this"),
),
)
.element(
Level::NOTE
.text(None)
.title("expected reference `&str`\nfound reference `&'static [u8; 0]`"),
),
);

let expected = str![[r#"
error[E0308]: mismatched types
--> $DIR/mismatched-types.rs:3:19
|
LL | let s: &str = include_bytes!("file.txt"); //~ ERROR mismatched types
| ---- ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&str`, found `&[u8; 0]`
| |
| expected due to this
= expected reference `&str`
found reference `&'static [u8; 0]`
"#]];
let renderer = Renderer::plain().anonymized_line_numbers(true);
assert_data_eq!(renderer.render(input), expected);
}

#[test]
fn secondary_title_custom_level_text() {
let source = r#"fn main() {
let b: &[u8] = include_str!("file.txt"); //~ ERROR mismatched types
let s: &str = include_bytes!("file.txt"); //~ ERROR mismatched types
}"#;

let input = Level::ERROR.header("mismatched types").id("E0308").group(
Group::new()
.element(
Snippet::source(source)
.path("$DIR/mismatched-types.rs")
.fold(true)
.annotation(
AnnotationKind::Primary
.span(105..131)
.label("expected `&str`, found `&[u8; 0]`"),
)
.annotation(
AnnotationKind::Context
.span(98..102)
.label("expected due to this"),
),
)
.element(
Level::NOTE
.text(Some("custom"))
.title("expected reference `&str`\nfound reference `&'static [u8; 0]`"),
),
);

let expected = str![[r#"
error[E0308]: mismatched types
--> $DIR/mismatched-types.rs:3:19
|
LL | let s: &str = include_bytes!("file.txt"); //~ ERROR mismatched types
| ---- ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&str`, found `&[u8; 0]`
| |
| expected due to this
|
= custom: expected reference `&str`
found reference `&'static [u8; 0]`
"#]];
let renderer = Renderer::plain().anonymized_line_numbers(true);
assert_data_eq!(renderer.render(input), expected);
}