Skip to content

Commit

Permalink
refactor: Extract get_max_lineno free function
Browse files Browse the repository at this point in the history
  • Loading branch information
chengr4 committed Aug 27, 2024
1 parent e2a44d0 commit ea22691
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/renderer/display_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +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, .. } => std::cmp::max(max, *lineno),
_ => max,
})
});
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(),
Expand Down Expand Up @@ -149,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

0 comments on commit ea22691

Please sign in to comment.