fix: whitespace rendering on scroll left

This also simplifies the renderer a little.
This commit is contained in:
CJ van den Berg 2025-01-02 22:51:36 +01:00
parent 919d5ee9bb
commit e3d93da9e6
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -820,7 +820,7 @@ pub const Editor = struct {
9 => ctx.self.render_tab(c, n, ctx.buf_col), 9 => ctx.self.render_tab(c, n, ctx.buf_col),
else => render_egc(c, n, chunk), else => render_egc(c, n, chunk),
}; };
const cell_map_val: CellType = switch (chunk[0]) { var cell_map_val: CellType = switch (chunk[0]) {
32 => .space, 32 => .space,
9 => .tab, 9 => .tab,
else => .character, else => .character,
@ -830,33 +830,42 @@ pub const Editor = struct {
self_.render_matches(&ctx.match_idx, ctx.theme, c); self_.render_matches(&ctx.match_idx, ctx.theme, c);
self_.render_selections(ctx.theme, c); self_.render_selections(ctx.theme, c);
const advanced = if (ctx.buf_col >= view.col) n.putc(c) catch break else colcount; var advance = colcount;
const new_col = ctx.buf_col + colcount - advanced; if (ctx.buf_col < view.col) {
if (ctx.buf_col < view.col and ctx.buf_col + advanced > view.col) advance = if (ctx.buf_col + advance >= view.col)
n.cursor_move_rel(0, @intCast(ctx.buf_col + advanced - view.col)) catch {}; ctx.buf_col + advance - view.col
ctx.buf_col += advanced; else
0;
}
if (ctx.buf_col >= view.col) {
_ = n.putc(c) catch {};
ctx.cell_map.set_yx(ctx.y, ctx.x, .{ .cell_type = cell_map_val }); ctx.cell_map.set_yx(ctx.y, ctx.x, .{ .cell_type = cell_map_val });
ctx.x += advanced; if (cell_map_val == .tab) cell_map_val = .extension;
advance -= 1;
while (ctx.buf_col < new_col) { ctx.x += 1;
if (ctx.buf_col >= view.col + view.cols) n.cursor_move_yx(@intCast(ctx.y), @intCast(ctx.x)) catch {};
break; }
while (advance > 0) : (advance -= 1) {
if (ctx.x >= view.cols) break;
var cell_ = n.cell_init(); var cell_ = n.cell_init();
const c_ = &cell_; const c_ = &cell_;
if (ctx.hl_row) |hl_row| if (hl_row == ctx.buf_row) if (ctx.hl_row) |hl_row| if (hl_row == ctx.buf_row)
self_.render_line_highlight_cell(ctx.theme, c_); self_.render_line_highlight_cell(ctx.theme, c_);
self_.render_matches(&ctx.match_idx, ctx.theme, c_); self_.render_matches(&ctx.match_idx, ctx.theme, c_);
self_.render_selections(ctx.theme, c_); self_.render_selections(ctx.theme, c_);
const advanced_ = n.putc(c_) catch break; _ = n.putc(c_) catch {};
ctx.buf_col += advanced_; ctx.cell_map.set_yx(ctx.y, ctx.x, .{ .cell_type = cell_map_val });
ctx.cell_map.set_yx(ctx.y, ctx.x, .{ .cell_type = .extension }); if (cell_map_val == .tab) cell_map_val = .extension;
ctx.x += advanced; ctx.x += 1;
n.cursor_move_yx(@intCast(ctx.y), @intCast(ctx.x)) catch {};
} }
ctx.buf_col += colcount;
chunk = chunk[bytes..]; chunk = chunk[bytes..];
} }
if (leaf.eol) { if (leaf.eol) {
var c = ctx.self.render_eol(n, ctx.theme); if (ctx.buf_col >= view.col) {
var c = ctx.self.render_eol(n);
if (ctx.hl_row) |hl_row| if (hl_row == ctx.buf_row) if (ctx.hl_row) |hl_row| if (hl_row == ctx.buf_row)
self_.render_line_highlight_cell(ctx.theme, &c); self_.render_line_highlight_cell(ctx.theme, &c);
self_.render_matches(&ctx.match_idx, ctx.theme, &c); self_.render_matches(&ctx.match_idx, ctx.theme, &c);
@ -866,14 +875,14 @@ pub const Editor = struct {
if (ctx.hl_row) |hl_row| if (hl_row == ctx.buf_row) if (ctx.hl_row) |hl_row| if (hl_row == ctx.buf_row)
self_.render_line_highlight_cell(ctx.theme, &term_cell); self_.render_line_highlight_cell(ctx.theme, &term_cell);
_ = n.putc(&term_cell) catch {}; _ = n.putc(&term_cell) catch {};
n.cursor_move_yx(-1, 0) catch |e| return Buffer.Walker{ .err = e }; ctx.cell_map.set_yx(ctx.y, ctx.x, .{ .cell_type = .eol });
n.cursor_move_rel(1, 0) catch |e| return Buffer.Walker{ .err = e }; }
ctx.buf_row += 1; ctx.buf_row += 1;
ctx.buf_col = 0; ctx.buf_col = 0;
ctx.cell_map.set_yx(ctx.y, ctx.x, .{ .cell_type = .eol });
ctx.y += 1; ctx.y += 1;
ctx.x = 0; ctx.x = 0;
ctx.leading = true; ctx.leading = true;
n.cursor_move_yx(@intCast(ctx.y), @intCast(ctx.x)) catch {};
} }
return Buffer.Walker.keep_walking; return Buffer.Walker.keep_walking;
} }
@ -1086,16 +1095,11 @@ pub const Editor = struct {
return .{ 1, 1 }; return .{ 1, 1 };
} }
inline fn render_eol(self: *const Self, n: *Plane, theme: *const Widget.Theme) Cell { inline fn render_eol(_: *const Self, n: *Plane) Cell {
const char = whitespace.char; const char = whitespace.char;
var cell = n.cell_init(); var cell = n.cell_init();
const c = &cell; const c = &cell;
if (self.render_whitespace == .visible) {
c.set_style(theme.editor_whitespace);
_ = n.cell_load(c, char.eol) catch {};
} else {
_ = n.cell_load(c, char.blank) catch {}; _ = n.cell_load(c, char.blank) catch {};
}
return cell; return cell;
} }
@ -1117,7 +1121,7 @@ pub const Editor = struct {
inline fn render_tab(self: *const Self, c: *Cell, n: *Plane, abs_col: usize) struct { usize, usize } { inline fn render_tab(self: *const Self, c: *Cell, n: *Plane, abs_col: usize) struct { usize, usize } {
const char = whitespace.char; const char = whitespace.char;
const colcount = 1 + self.tab_width - (abs_col % self.tab_width); const colcount = self.tab_width - (abs_col % self.tab_width);
_ = n.cell_load(c, char.blank) catch {}; _ = n.cell_load(c, char.blank) catch {};
return .{ 1, colcount }; return .{ 1, colcount };
} }