refactor: simplify editor.render_blame

This commit is contained in:
CJ van den Berg 2026-01-26 21:54:34 +01:00
parent 7d1809ba57
commit c7df7e43d7

View file

@ -1456,55 +1456,23 @@ pub const Editor = struct {
_ = self.plane.putc(&cell) catch {}; _ = self.plane.putc(&cell) catch {};
} }
inline fn get_delta_lines_until_row(self: *const Self, row: usize) ?i32 {
var delta_lines: i32 = 0;
for (self.changes.items) |change| {
if (change.line > row)
break;
if (change.kind == .insert)
if (row >= change.line and row < change.line + change.lines)
return null;
switch (change.kind) {
.insert => delta_lines -= @intCast(change.lines),
.delete => delta_lines += @intCast(change.lines),
else => {},
}
}
return delta_lines;
}
fn render_blame(self: *Self, theme: *const Widget.Theme, cell_map: CellMap) !void { fn render_blame(self: *Self, theme: *const Widget.Theme, cell_map: CellMap) !void {
const cursor = self.get_primary().cursor; const cursor = self.get_primary().cursor;
const row_min = self.view.row; const pos = self.screen_cursor(&cursor) orelse return;
const row_max = row_min + self.view.rows; const buffer = self.buffer orelse return;
if (cursor.row < row_min or row_max < cursor.row) const commit = buffer.get_vcs_blame(cursor.row) orelse return;
return; const author = commit.author;
const row = cursor.row - self.view.row;
const col = cursor.col;
const screen_width = self.view.cols;
var style = theme.editor_hint; var style = theme.editor_hint;
style = .{ .fg = style.fg, .bg = theme.editor_hint.bg }; style = .{ .fg = style.fg, .bg = theme.editor_hint.bg };
// Get delta line from HEAD version with diffs self.plane.cursor_move_yx(@intCast(pos.row), @intCast(pos.col));
const casted_row: i32 = @intCast(row);
const delta = self.get_delta_lines_until_row(row) orelse return;
const head_line: i32 = delta + casted_row;
if (head_line < 0) return;
const commit = self.buffer.?.get_vcs_blame(@intCast(head_line)) orelse return;
const author = commit.author;
self.plane.cursor_move_yx(@intCast(row), @intCast(col));
self.render_diagnostic_cell(style); self.render_diagnostic_cell(style);
const screen_width = self.view.cols;
var space_begin = screen_width; var space_begin = screen_width;
while (space_begin > 0) : (space_begin -= 1) while (space_begin > 0) : (space_begin -= 1)
if (cell_map.get_yx(row, space_begin).cell_type != .empty) break; if (cell_map.get_yx(pos.row, space_begin).cell_type != .empty) break;
if (screen_width > min_diagnostic_view_len and space_begin < screen_width - min_diagnostic_view_len) { if (screen_width > min_diagnostic_view_len and space_begin < screen_width - min_diagnostic_view_len) {
self.plane.set_style(style); self.plane.set_style(style);
@ -1512,10 +1480,10 @@ pub const Editor = struct {
switch (tui.config().inline_diagnostics_alignment) { switch (tui.config().inline_diagnostics_alignment) {
.right => { .right => {
const width = self.plane.window.width; const width = self.plane.window.width;
self.plane.cursor_move_yx(@intCast(row), @intCast(width -| (screen_width - space_begin) + 2)); self.plane.cursor_move_yx(@intCast(pos.row), @intCast(width -| (screen_width - space_begin) + 2));
_ = self.plane.print("  {s}", .{author}) catch 0; _ = self.plane.print("  {s}", .{author}) catch 0;
}, },
.left => _ = self.plane.print_aligned_right(@intCast(row), "  {s}", .{author[0..@min(screen_width - space_begin - 3, author.len)]}) catch {}, .left => _ = self.plane.print_aligned_right(@intCast(pos.row), "  {s}", .{author[0..@min(screen_width - space_begin - 3, author.len)]}) catch {},
} }
} }
} }