From 082ba4d741896499388f4592e897165675819a7e Mon Sep 17 00:00:00 2001 From: Nik Revenco <154856872+NikitaRevenco@users.noreply.github.com> Date: Tue, 25 Mar 2025 21:27:14 +0000 Subject: [PATCH] refactor: `match` over `if` --- helix-term/src/ui/editor.rs | 90 +++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 43 deletions(-) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 56fd04241..585107e15 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -254,53 +254,57 @@ impl EditorView { decorations: &mut DecorationManager, theme: &Theme, ) { - if inline_blame.behaviour == InlineBlameBehaviour::CursorLine { - let cursor_line_idx = doc.cursor_line(view.id); + match inline_blame.behaviour { + InlineBlameBehaviour::Hidden => (), + InlineBlameBehaviour::CursorLine => { + let cursor_line_idx = doc.cursor_line(view.id); - // do not render inline blame for empty lines to reduce visual noise - if doc.text().line(cursor_line_idx) != doc.line_ending.as_str() { - if let Ok(line_blame) = doc.line_blame(cursor_line_idx as u32, &inline_blame.format) - { - decorations.add_decoration(InlineBlame::new( - theme, - text_decorations::blame::LineBlame::OneLine((cursor_line_idx, line_blame)), - )); - }; - } - } else if inline_blame.behaviour == InlineBlameBehaviour::AllLines { - let text = doc.text(); - let text_line_count = text.len_lines(); - let view_height = view.inner_height(); - let first_visible_line = - text.char_to_line(doc.view_offset(view.id).anchor.min(text.len_chars())); - let first_line = first_visible_line.saturating_sub(view_height); - let last_line = first_visible_line - .saturating_add(view_height.saturating_mul(2)) - .min(text_line_count); - - let mut blame_lines = vec![None; text_line_count]; - - // Compute ~3 times the current view height of inline blame, that way some scrolling - // will not show half the view with inline blame and half without while still being faster - // than rendering inline blame for the full file. - let blame_for_all_lines = (first_line..last_line).filter_map(|line_idx| { // do not render inline blame for empty lines to reduce visual noise - if text.line(line_idx) != doc.line_ending.as_str() { - doc.line_blame(line_idx as u32, &inline_blame.format) - .ok() - .map(|blame| (line_idx, blame)) - } else { - None + if doc.text().line(cursor_line_idx) != doc.line_ending.as_str() { + if let Ok(line_blame) = doc.line_blame(cursor_line_idx as u32, &inline_blame.format) + { + decorations.add_decoration(InlineBlame::new( + theme, + text_decorations::blame::LineBlame::OneLine((cursor_line_idx, line_blame)), + )); + }; } - }); + }, + InlineBlameBehaviour::AllLines => { + let text = doc.text(); + let text_line_count = text.len_lines(); + let view_height = view.inner_height(); + let first_visible_line = + text.char_to_line(doc.view_offset(view.id).anchor.min(text.len_chars())); + let first_line = first_visible_line.saturating_sub(view_height); + let last_line = first_visible_line + .saturating_add(view_height.saturating_mul(2)) + .min(text_line_count); - for (line_idx, blame) in blame_for_all_lines { - blame_lines[line_idx] = Some(blame); - } - decorations.add_decoration(InlineBlame::new( - theme, - text_decorations::blame::LineBlame::ManyLines(blame_lines), - )); + let mut blame_lines = vec![None; text_line_count]; + + // Compute ~3 times the current view height of inline blame, that way some scrolling + // will not show half the view with inline blame and half without while still being faster + // than rendering inline blame for the full file. + let blame_for_all_lines = (first_line..last_line).filter_map(|line_idx| { + // do not render inline blame for empty lines to reduce visual noise + if text.line(line_idx) != doc.line_ending.as_str() { + doc.line_blame(line_idx as u32, &inline_blame.format) + .ok() + .map(|blame| (line_idx, blame)) + } else { + None + } + }); + + for (line_idx, blame) in blame_for_all_lines { + blame_lines[line_idx] = Some(blame); + } + decorations.add_decoration(InlineBlame::new( + theme, + text_decorations::blame::LineBlame::ManyLines(blame_lines), + )); + }, } }