Skip to content

Commit

Permalink
fix: Add None case for finding max line number
Browse files Browse the repository at this point in the history
	- Line number width is 1 if max line number is 0
	- Line number witdh is 0 if max line number is None
	- Update test_line_number_0 in tests/formatter.rs to reflect new behavior
	- Fix issue #57
  • Loading branch information
chengr4 committed Aug 30, 2024
1 parent 90fa23d commit ca60949
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/renderer/display_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,17 @@ 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| {
let lineno_width = self.body.iter().fold(None, |max, set| {
set.display_lines.iter().fold(max, |max, line| match line {
DisplayLine::Source { lineno, .. } => cmp::max(lineno.unwrap_or(0), max),
DisplayLine::Source { lineno, .. } => std::cmp::max(max, *lineno),
_ => 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 lineno_width = match lineno_width {
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

0 comments on commit ca60949

Please sign in to comment.