refactor: broadcast line numbering mode and style changes to all splits

This commit is contained in:
CJ van den Berg 2026-01-06 18:33:42 +01:00
parent 63275963b5
commit f7f227dd82
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 24 additions and 20 deletions

View file

@ -28,8 +28,8 @@ lines: u32 = 0,
view_rows: u32 = 1, view_rows: u32 = 1,
view_top: u32 = 1, view_top: u32 = 1,
line: usize = 0, line: usize = 0,
mode: ?LineNumberMode = null, line_number_mode: ?LineNumberMode = null,
render_style: DigitStyle, line_number_style: DigitStyle,
highlight: bool, highlight: bool,
symbols: bool, symbols: bool,
width: usize = 4, width: usize = 4,
@ -49,8 +49,8 @@ pub fn create(allocator: Allocator, parent: Widget, event_source: Widget, editor
.allocator = allocator, .allocator = allocator,
.plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent.plane.*), .plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent.plane.*),
.parent = parent, .parent = parent,
.mode = tui.config().gutter_line_numbers_mode, .line_number_mode = tui.config().gutter_line_numbers_mode,
.render_style = tui.config().gutter_line_numbers_style, .line_number_style = tui.config().gutter_line_numbers_style,
.highlight = tui.config().highlight_current_line_gutter, .highlight = tui.config().highlight_current_line_gutter,
.symbols = tui.config().gutter_symbols, .symbols = tui.config().gutter_symbols,
.editor = editor, .editor = editor,
@ -94,6 +94,8 @@ pub fn handle_event(self: *Self, _: tp.pid_ref, m: tp.message) tp.result {
pub fn receive(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool { pub fn receive(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool {
var y: i32 = undefined; var y: i32 = undefined;
var ypx: i32 = undefined; var ypx: i32 = undefined;
var line_number_mode: ?LineNumberMode = null;
var line_number_style: DigitStyle = undefined;
if (try m.match(.{ "B", input.event.press, @intFromEnum(input.mouse.BUTTON1), tp.any, tp.any, tp.extract(&y), tp.any, tp.extract(&ypx) })) if (try m.match(.{ "B", input.event.press, @intFromEnum(input.mouse.BUTTON1), tp.any, tp.any, tp.extract(&y), tp.any, tp.extract(&ypx) }))
return self.primary_click(y); return self.primary_click(y);
@ -107,14 +109,22 @@ pub fn receive(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool {
return self.mouse_click_button4(); return self.mouse_click_button4();
if (try m.match(.{ "B", input.event.press, @intFromEnum(input.mouse.BUTTON5), tp.more })) if (try m.match(.{ "B", input.event.press, @intFromEnum(input.mouse.BUTTON5), tp.more }))
return self.mouse_click_button5(); return self.mouse_click_button5();
if (try m.match(.{ "line_number_mode", tp.extract(&line_number_mode) })) {
self.line_number_mode = line_number_mode;
return false;
}
if (try m.match(.{ "line_number_style", tp.extract(&line_number_style) })) {
self.line_number_style = line_number_style;
return false;
}
return false; return false;
} }
fn update_width(self: *Self) void { fn update_width(self: *Self) void {
if (self.mode == .none) return; if (self.line_number_mode == .none) return;
const width = int_width(self.lines); const width = int_width(self.lines);
self.width = if (self.mode == .relative and width > 4) 4 else @max(width, 2); self.width = if (self.line_number_mode == .relative and width > 4) 4 else @max(width, 2);
self.width += if (self.symbols) 3 else 1; self.width += if (self.symbols) 3 else 1;
} }
@ -123,11 +133,11 @@ pub fn layout(self: *Self) Widget.Layout {
} }
inline fn get_width(self: *Self) usize { inline fn get_width(self: *Self) usize {
return if (self.mode != .none) self.width else if (self.symbols) 3 else 1; return if (self.line_number_mode != .none) self.width else if (self.symbols) 3 else 1;
} }
fn get_numbering_mode(self: *const Self) LineNumberMode { fn get_numbering_mode(self: *const Self) LineNumberMode {
return self.mode orelse switch (if (tui.input_mode()) |mode| mode.line_numbers else .absolute) { return self.line_number_mode orelse switch (if (tui.input_mode()) |mode| mode.line_numbers else .absolute) {
.relative => .relative, .relative => .relative,
.inherit => if (tui.input_mode_outer()) |mode| from_mode_enum(mode.line_numbers) else .absolute, .inherit => if (tui.input_mode_outer()) |mode| from_mode_enum(mode.line_numbers) else .absolute,
.absolute => .absolute, .absolute => .absolute,
@ -190,7 +200,7 @@ pub fn render_linear(self: *Self, theme: *const Widget.Theme) void {
self.plane.off_styles(styles.bold); self.plane.off_styles(styles.bold);
} }
try self.plane.cursor_move_yx(@intCast(pos), 0); try self.plane.cursor_move_yx(@intCast(pos), 0);
try self.print_digits(linenum, self.render_style); try self.print_digits(linenum, self.line_number_style);
if (self.highlight and linenum == self.line + 1) if (self.highlight and linenum == self.line + 1)
self.render_line_highlight(pos, theme); self.render_line_highlight(pos, theme);
self.render_diff_symbols(&diff_symbols, pos, linenum, theme); self.render_diff_symbols(&diff_symbols, pos, linenum, theme);
@ -216,7 +226,7 @@ pub fn render_relative(self: *Self, theme: *const Widget.Theme) void {
if (val > 999999) if (val > 999999)
_ = self.plane.print_aligned_right(@intCast(pos), "==> ", .{}) catch {} _ = self.plane.print_aligned_right(@intCast(pos), "==> ", .{}) catch {}
else else
self.print_digits(val, self.render_style) catch {}; self.print_digits(val, self.line_number_style) catch {};
if (self.highlight and linenum == 0) if (self.highlight and linenum == 0)
self.render_line_highlight(pos, theme); self.render_line_highlight(pos, theme);

View file

@ -946,7 +946,7 @@ const cmds = struct {
} }
pub const focus_split_meta: Meta = .{ .description = "Focus split view", .arguments = &.{.integer} }; pub const focus_split_meta: Meta = .{ .description = "Focus split view", .arguments = &.{.integer} };
pub fn gutter_mode_next(self: *Self, _: Ctx) Result { pub fn gutter_mode_next(_: *Self, _: Ctx) Result {
const config = tui.config_mut(); const config = tui.config_mut();
const mode: ?@import("config").LineNumberMode = if (config.gutter_line_numbers_mode) |mode| switch (mode) { const mode: ?@import("config").LineNumberMode = if (config.gutter_line_numbers_mode) |mode| switch (mode) {
.absolute => .relative, .absolute => .relative,
@ -956,14 +956,11 @@ const cmds = struct {
config.gutter_line_numbers_mode = mode; config.gutter_line_numbers_mode = mode;
try tui.save_config(); try tui.save_config();
if (self.widgets.get("editor_gutter")) |gutter_widget| { try tp.self_pid().send(.{ "line_number_mode", mode });
const gutter = gutter_widget.dynamic_cast(@import("editor_gutter.zig")) orelse return;
gutter.mode = mode;
}
} }
pub const gutter_mode_next_meta: Meta = .{ .description = "Next gutter mode" }; pub const gutter_mode_next_meta: Meta = .{ .description = "Next gutter mode" };
pub fn gutter_style_next(self: *Self, _: Ctx) Result { pub fn gutter_style_next(_: *Self, _: Ctx) Result {
const config = tui.config_mut(); const config = tui.config_mut();
config.gutter_line_numbers_style = switch (config.gutter_line_numbers_style) { config.gutter_line_numbers_style = switch (config.gutter_line_numbers_style) {
.ascii => .digital, .ascii => .digital,
@ -972,10 +969,7 @@ const cmds = struct {
.superscript => .ascii, .superscript => .ascii,
}; };
try tui.save_config(); try tui.save_config();
if (self.widgets.get("editor_gutter")) |gutter_widget| { try tp.self_pid().send(.{ "line_number_style", config.gutter_line_numbers_style });
const gutter = gutter_widget.dynamic_cast(@import("editor_gutter.zig")) orelse return;
gutter.render_style = config.gutter_line_numbers_style;
}
} }
pub const gutter_style_next_meta: Meta = .{ .description = "Next line number style" }; pub const gutter_style_next_meta: Meta = .{ .description = "Next line number style" };