refactor: `match` over `if`

pull/13133/head
Nik Revenco 2025-03-25 21:27:14 +00:00
parent ab5663891c
commit 082ba4d741
1 changed files with 47 additions and 43 deletions

View File

@ -254,53 +254,57 @@ impl EditorView {
decorations: &mut DecorationManager, decorations: &mut DecorationManager,
theme: &Theme, theme: &Theme,
) { ) {
if inline_blame.behaviour == InlineBlameBehaviour::CursorLine { match inline_blame.behaviour {
let cursor_line_idx = doc.cursor_line(view.id); 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 // do not render inline blame for empty lines to reduce visual noise
if text.line(line_idx) != doc.line_ending.as_str() { if doc.text().line(cursor_line_idx) != doc.line_ending.as_str() {
doc.line_blame(line_idx as u32, &inline_blame.format) if let Ok(line_blame) = doc.line_blame(cursor_line_idx as u32, &inline_blame.format)
.ok() {
.map(|blame| (line_idx, blame)) decorations.add_decoration(InlineBlame::new(
} else { theme,
None 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 { let mut blame_lines = vec![None; text_line_count];
blame_lines[line_idx] = Some(blame);
} // Compute ~3 times the current view height of inline blame, that way some scrolling
decorations.add_decoration(InlineBlame::new( // will not show half the view with inline blame and half without while still being faster
theme, // than rendering inline blame for the full file.
text_decorations::blame::LineBlame::ManyLines(blame_lines), 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),
));
},
} }
} }