Skip to content

Commit

Permalink
refactor: Extract get_lineno_width method
Browse files Browse the repository at this point in the history
  • Loading branch information
chengr4 committed Aug 26, 2024
1 parent ae9a00c commit 7a51b73
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/renderer/display_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +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.get_max_lineno();
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 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 +146,17 @@ impl<'a> DisplayList<'a> {
});
max_lineno
}

fn get_lineno_width(&self) -> usize {
let max_lineno = self.get_max_lineno();
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,
};
lineno_width
}
}

#[derive(Debug, PartialEq)]
Expand Down

0 comments on commit 7a51b73

Please sign in to comment.