Skip to content

fix: Tweak rendering of source when it's a single ASCII whitespace #169

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 1 addition & 3 deletions src/renderer/display_list.rs
Original file line number Diff line number Diff line change
@@ -1400,9 +1400,7 @@ fn format_body(
}
})
.sum();
if line.chars().any(|c| !c.is_whitespace()) {
whitespace_margin = min(whitespace_margin, leading_whitespace);
}
whitespace_margin = min(whitespace_margin, leading_whitespace);
max_line_len = max(max_line_len, line_length);

let line_start_index = line_range.0;
26 changes: 26 additions & 0 deletions tests/formatter.rs
Original file line number Diff line number Diff line change
@@ -955,3 +955,29 @@ error: title
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}

// This tests that reasonable rendering is done in an odd case: when the source
// is a single ASCII whitespace and there's annotation pointing to immediately
// after it.
//
// Previously, this would end up with a `...` rendered instead of just the
// space itself. The `...` seems incorrect here because I don't believe any
// trimming occurs (or is needed).
#[test]
fn one_space_annotation() {
let source = " ";
let input = Level::Error.title("title").snippet(
Snippet::source(source)
.fold(false)
.annotation(Level::Error.span(1..1).label("annotation")),
);
let expected = "\
error: title
|
1 |\x20\x20
| ^ annotation
|\
";
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
Loading
Oops, something went wrong.