Skip to content
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

Fix line number 0 unaligned display #142

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
28 changes: 16 additions & 12 deletions src/renderer/display_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,12 @@ impl<'a> fmt::Debug for DisplayList<'a> {

impl<'a> Display for DisplayList<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let lineno_width = self.body.iter().fold(0, |max, set| {
set.display_lines.iter().fold(max, |max, line| match line {
DisplayLine::Source { lineno, .. } => cmp::max(lineno.unwrap_or(0), max),
_ => max,
})
});
let lineno_width = if lineno_width == 0 {
lineno_width
} else if self.anonymized_line_numbers {
ANONYMIZED_LINE_NUM.len()
} else {
((lineno_width as f64).log10().floor() as usize) + 1
let max_lineno = get_max_lineno(&self.body);
let lineno_width = match max_lineno {
None => 0,
Some(_max) if self.anonymized_line_numbers => ANONYMIZED_LINE_NUM.len(),
Some(0) => 1,
Some(max) => (max as f64).log10().floor() as usize + 1,
};

let multiline_depth = self.body.iter().fold(0, |max, set| {
Expand Down Expand Up @@ -150,6 +144,16 @@ impl<'a> DisplayList<'a> {
}
}

fn get_max_lineno(body: &[DisplaySet<'_>]) -> Option<usize> {
let max_lineno = body.iter().fold(None, |max, set| {
set.display_lines.iter().fold(max, |max, line| match line {
DisplayLine::Source { lineno, .. } => cmp::max(max, *lineno),
_ => max,
})
});
max_lineno
}

#[derive(Debug, PartialEq)]
pub(crate) struct DisplaySet<'a> {
pub(crate) display_lines: Vec<DisplayLine<'a>>,
Expand Down
22 changes: 22 additions & 0 deletions tests/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -905,3 +905,25 @@ error: unused optional dependency
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}

// for issue 57
#[test]
fn test_line_number_0() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you change up the commits so this test case is the first commit, with it passing ie showing the bad behavior?

Benefits

  • For someone viewing the commit with the behavior change, it makes it obvious what the intended behavior change is
  • It shows to you and reviewers that the test tests the intended case

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For someone viewing the commit with the behavior change, it makes it obvious what the intended behavior change is

This means the commit that changes behavior should also include test updates. Every commit should pass tests

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As reminder, every commit should pass tests

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ae4694f is said to be a fix and changes behavior but there is no test update in that commit.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I did not see the comment and please check out the update. Thanks.

let input = Level::Error.title("dummy").snippet(
Snippet::source("foo")
.origin("file/path")
.line_start(0)
.annotation(Level::Error.span(2..3)), // bar\nbaz
);

let expected = str![[r#"
error: dummy
--> file/path:0:3
|
0 | foo
| ^
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}