Skip to content

Commit

Permalink
refactor: Extract calculation to get_lineno_width method
Browse files Browse the repository at this point in the history
  • Loading branch information
chengr4 committed Aug 23, 2024
1 parent 1cc6e95 commit 43a1dc0
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions src/renderer/display_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,7 @@ impl<'a> fmt::Debug for DisplayList<'a> {

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

let multiline_depth = self.body.iter().fold(0, |max, set| {
set.display_lines.iter().fold(max, |max2, line| match line {
Expand Down Expand Up @@ -152,6 +136,27 @@ impl<'a> DisplayList<'a> {
}
Ok(())
}

fn get_lineno_width(&self) -> usize {
let max_lineno = self.body.iter().fold(None, |max, set| {
set.display_lines.iter().fold(max, |max, line| match line {
DisplayLine::Source { lineno, .. } => match (max, lineno) {
(Some(max), Some(lineno)) => Some(cmp::max(max, *lineno)),
(Some(max), None) => Some(max),
(None, Some(lineno)) => Some(*lineno),
(None, None) => None,
},
_ => max,
})
});
let lineno_width = match max_lineno {
None => 0,
Some(0) => 1,
Some(_max) if self.anonymized_line_numbers => ANONYMIZED_LINE_NUM.len(),
Some(max) => (max as f64).log10().floor() as usize + 1,
};
lineno_width
}
}

#[derive(Debug, PartialEq)]
Expand Down

0 comments on commit 43a1dc0

Please sign in to comment.