refactor: BREAKING merge gutter_line_numbers and gutter_line_numbers_relative config options into an enum

This commit is contained in:
CJ van den Berg 2025-02-20 21:09:44 +01:00
parent ec3bba2aff
commit b9cc3936c8
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
4 changed files with 30 additions and 32 deletions

View file

@ -3,8 +3,7 @@ const builtin = @import("builtin");
frame_rate: usize = 60, frame_rate: usize = 60,
theme: []const u8 = "default", theme: []const u8 = "default",
input_mode: []const u8 = "flow", input_mode: []const u8 = "flow",
gutter_line_numbers: bool = true, gutter_line_numbers_mode: ?LineNumberMode = null,
gutter_line_numbers_relative: bool = false,
gutter_line_numbers_style: DigitStyle = .ascii, gutter_line_numbers_style: DigitStyle = .ascii,
gutter_symbols: bool = true, gutter_symbols: bool = true,
enable_terminal_cursor: bool = true, enable_terminal_cursor: bool = true,
@ -34,3 +33,9 @@ pub const DigitStyle = enum {
subscript, subscript,
superscript, superscript,
}; };
pub const LineNumberMode = enum {
none,
relative,
absolute,
};

View file

@ -619,6 +619,13 @@ fn config_eql(comptime T: type, a: T, b: T) bool {
} }
switch (@typeInfo(T)) { switch (@typeInfo(T)) {
.Bool, .Int, .Float, .Enum => return a == b, .Bool, .Int, .Float, .Enum => return a == b,
.Optional => |info| {
if (a == null and b == null)
return true;
if (a == null or b == null)
return false;
return config_eql(info.child, a.?, b.?);
},
else => {}, else => {},
} }
@compileError("unsupported config type " ++ @typeName(T)); @compileError("unsupported config type " ++ @typeName(T));

View file

@ -17,6 +17,7 @@ const MessageFilter = @import("MessageFilter.zig");
const tui = @import("tui.zig"); const tui = @import("tui.zig");
const ed = @import("editor.zig"); const ed = @import("editor.zig");
const DigitStyle = @import("config").DigitStyle; const DigitStyle = @import("config").DigitStyle;
const LineNumberMode = @import("config").LineNumberMode;
allocator: Allocator, allocator: Allocator,
plane: Plane, plane: Plane,
@ -26,7 +27,6 @@ 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,
linenum: bool,
mode: ?LineNumberMode = null, mode: ?LineNumberMode = null,
render_style: DigitStyle, render_style: DigitStyle,
highlight: bool, highlight: bool,
@ -47,8 +47,7 @@ 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,
.linenum = tui.config().gutter_line_numbers, .mode = tui.config().gutter_line_numbers_mode,
.mode = if (tui.config().gutter_line_numbers_relative) .relative else null,
.render_style = tui.config().gutter_line_numbers_style, .render_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,
@ -111,7 +110,7 @@ pub fn receive(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool {
} }
fn update_width(self: *Self) void { fn update_width(self: *Self) void {
if (!self.linenum) return; if (self.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.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;
@ -122,7 +121,7 @@ pub fn layout(self: *Self) Widget.Layout {
} }
inline fn get_width(self: *Self) usize { inline fn get_width(self: *Self) usize {
return if (self.linenum) self.width else if (self.symbols) 3 else 1; return if (self.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 {
@ -133,8 +132,6 @@ fn get_numbering_mode(self: *const Self) LineNumberMode {
}; };
} }
const LineNumberMode = enum { relative, absolute };
fn from_mode_enum(mode: anytype) LineNumberMode { fn from_mode_enum(mode: anytype) LineNumberMode {
return switch (mode) { return switch (mode) {
.relative => .relative, .relative => .relative,
@ -151,14 +148,11 @@ pub fn render(self: *Self, theme: *const Widget.Theme) bool {
self.plane.home(); self.plane.home();
self.plane.set_style(theme.editor_gutter); self.plane.set_style(theme.editor_gutter);
_ = self.plane.fill(" "); _ = self.plane.fill(" ");
if (self.linenum) {
switch (self.get_numbering_mode()) { switch (self.get_numbering_mode()) {
.none => self.render_none(theme),
.relative => self.render_relative(theme), .relative => self.render_relative(theme),
.absolute => self.render_linear(theme), .absolute => self.render_linear(theme),
} }
} else {
self.render_none(theme);
}
if (self.symbols) if (self.symbols)
self.render_diagnostics(theme); self.render_diagnostics(theme);
return false; return false;

View file

@ -572,25 +572,17 @@ const cmds = struct {
pub fn gutter_mode_next(self: *Self, _: Ctx) Result { pub fn gutter_mode_next(self: *Self, _: Ctx) Result {
const config = tui.config_mut(); const config = tui.config_mut();
var ln = config.gutter_line_numbers; const mode: ?@import("config").LineNumberMode = if (config.gutter_line_numbers_mode) |mode| switch(mode) {
var lnr = config.gutter_line_numbers_relative; .absolute => .relative,
if (ln and !lnr) { .relative => .none,
ln = true; .none => null,
lnr = true; } else .relative;
} else if (ln and lnr) {
ln = false; config.gutter_line_numbers_mode = mode;
lnr = false;
} else {
ln = true;
lnr = false;
}
config.gutter_line_numbers = ln;
config.gutter_line_numbers_relative = lnr;
try tui.save_config(); try tui.save_config();
if (self.widgets.get("editor_gutter")) |gutter_widget| { if (self.widgets.get("editor_gutter")) |gutter_widget| {
const gutter = gutter_widget.dynamic_cast(@import("editor_gutter.zig")) orelse return; const gutter = gutter_widget.dynamic_cast(@import("editor_gutter.zig")) orelse return;
gutter.linenum = ln; gutter.mode = mode;
gutter.mode = if(lnr) .relative else null;
} }
} }
pub const gutter_mode_next_meta = .{ .description = "Next gutter mode" }; pub const gutter_mode_next_meta = .{ .description = "Next gutter mode" };