From 3dc731d0861d1a3814cdf3e8a49b682660728382 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Tue, 18 Nov 2025 12:35:28 +0100 Subject: [PATCH] refactor: convert whitespace_mode config options to an enum --- src/config.zig | 12 +++++++++++- src/tui/editor.zig | 26 +++++--------------------- src/tui/tui.zig | 25 ++++++++++--------------- 3 files changed, 26 insertions(+), 37 deletions(-) diff --git a/src/config.zig b/src/config.zig index 14377af..8368c93 100644 --- a/src/config.zig +++ b/src/config.zig @@ -14,7 +14,7 @@ highlight_current_line_gutter: bool = true, highlight_columns: []const u16 = &.{ 80, 100, 120 }, highlight_columns_alpha: u8 = 240, highlight_columns_enabled: bool = false, -whitespace_mode: []const u8 = "none", +whitespace_mode: WhitespaceMode = .none, inline_diagnostics: bool = true, animation_min_lag: usize = 0, //milliseconds animation_max_lag: usize = 50, //milliseconds @@ -103,3 +103,13 @@ pub const WidgetStyle = enum { spacious, compact, }; + +pub const WhitespaceMode = enum { + indent, + leading, + eol, + tabs, + visible, + full, + none, +}; diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 5c99f1d..025e561 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -25,6 +25,7 @@ const Widget = @import("Widget.zig"); const WidgetList = @import("WidgetList.zig"); const tui = @import("tui.zig"); const IndentMode = @import("config").IndentMode; +const WhitespaceMode = @import("config").WhitespaceMode; pub const Cursor = Buffer.Cursor; pub const View = Buffer.View; @@ -408,7 +409,6 @@ pub const Editor = struct { } = null, } = null, - const WhitespaceMode = enum { indent, leading, eol, tabs, visible, full, none }; const StyleCache = std.AutoHashMap(u32, ?Widget.Theme.Token); const Context = command.Context; @@ -539,7 +539,7 @@ pub const Editor = struct { .animation_last_time = time.microTimestamp(), .enable_format_on_save = tui.config().enable_format_on_save, .enable_terminal_cursor = tui.config().enable_terminal_cursor, - .render_whitespace = from_whitespace_mode(tui.config().whitespace_mode), + .render_whitespace = tui.config().whitespace_mode, }; } @@ -558,23 +558,6 @@ pub const Editor = struct { if (self.buffer) |p| self.buffer_manager.retire(p, meta.written()); } - fn from_whitespace_mode(whitespace_mode: []const u8) WhitespaceMode { - return if (std.mem.eql(u8, whitespace_mode, "indent")) - .indent - else if (std.mem.eql(u8, whitespace_mode, "leading")) - .leading - else if (std.mem.eql(u8, whitespace_mode, "eol")) - .eol - else if (std.mem.eql(u8, whitespace_mode, "tabs")) - .tabs - else if (std.mem.eql(u8, whitespace_mode, "visible")) - .visible - else if (std.mem.eql(u8, whitespace_mode, "full")) - .full - else - .none; - } - pub fn need_render(_: *Self) void { Widget.need_render(); } @@ -6478,6 +6461,7 @@ pub const EditorWidget = struct { var ypx: c_int = undefined; var pos: u32 = 0; var bytes: []const u8 = ""; + var whitespace_mode: WhitespaceMode = .none; if (try m.match(.{ "M", tp.extract(&x), tp.extract(&y), tp.extract(&xpx), tp.extract(&ypx) })) { const hover_y, const hover_x = self.editor.plane.abs_yx_to_rel(y, x); @@ -6526,8 +6510,8 @@ pub const EditorWidget = struct { try self.editor.highlight_references(.{}); }, }; - } else if (try m.match(.{ "whitespace_mode", tp.extract(&bytes) })) { - self.editor.render_whitespace = Editor.from_whitespace_mode(bytes); + } else if (try m.match(.{ "whitespace_mode", tp.extract(&whitespace_mode) })) { + self.editor.render_whitespace = whitespace_mode; } else { return false; } diff --git a/src/tui/tui.zig b/src/tui/tui.zig index 581baa8..338dbfc 100644 --- a/src/tui/tui.zig +++ b/src/tui/tui.zig @@ -990,25 +990,20 @@ const cmds = struct { pub const theme_prev_meta: Meta = .{ .description = "Previous color theme" }; pub fn toggle_whitespace_mode(self: *Self, _: Ctx) Result { - self.config_.whitespace_mode = if (std.mem.eql(u8, self.config_.whitespace_mode, "none")) - "indent" - else if (std.mem.eql(u8, self.config_.whitespace_mode, "indent")) - "leading" - else if (std.mem.eql(u8, self.config_.whitespace_mode, "leading")) - "eol" - else if (std.mem.eql(u8, self.config_.whitespace_mode, "eol")) - "tabs" - else if (std.mem.eql(u8, self.config_.whitespace_mode, "tabs")) - "visible" - else if (std.mem.eql(u8, self.config_.whitespace_mode, "visible")) - "full" - else - "none"; + self.config_.whitespace_mode = switch (self.config_.whitespace_mode) { + .none => .indent, + .indent => .leading, + .leading => .eol, + .eol => .tabs, + .tabs => .visible, + .visible => .full, + .full => .none, + }; try save_config(); var buf: [32]u8 = undefined; const m = try tp.message.fmtbuf(&buf, .{ "whitespace_mode", self.config_.whitespace_mode }); _ = try self.send_widgets(tp.self_pid(), m); - self.logger.print("whitespace rendering {s}", .{self.config_.whitespace_mode}); + self.logger.print("whitespace rendering {s}", .{@tagName(self.config_.whitespace_mode)}); } pub const toggle_whitespace_mode_meta: Meta = .{ .description = "Next whitespace mode" };