refactor: convert whitespace_mode config options to an enum

This commit is contained in:
CJ van den Berg 2025-11-18 12:35:28 +01:00
parent e2009425f5
commit 3dc731d086
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
3 changed files with 26 additions and 37 deletions

View file

@ -14,7 +14,7 @@ highlight_current_line_gutter: bool = true,
highlight_columns: []const u16 = &.{ 80, 100, 120 }, highlight_columns: []const u16 = &.{ 80, 100, 120 },
highlight_columns_alpha: u8 = 240, highlight_columns_alpha: u8 = 240,
highlight_columns_enabled: bool = false, highlight_columns_enabled: bool = false,
whitespace_mode: []const u8 = "none", whitespace_mode: WhitespaceMode = .none,
inline_diagnostics: bool = true, inline_diagnostics: bool = true,
animation_min_lag: usize = 0, //milliseconds animation_min_lag: usize = 0, //milliseconds
animation_max_lag: usize = 50, //milliseconds animation_max_lag: usize = 50, //milliseconds
@ -103,3 +103,13 @@ pub const WidgetStyle = enum {
spacious, spacious,
compact, compact,
}; };
pub const WhitespaceMode = enum {
indent,
leading,
eol,
tabs,
visible,
full,
none,
};

View file

@ -25,6 +25,7 @@ const Widget = @import("Widget.zig");
const WidgetList = @import("WidgetList.zig"); const WidgetList = @import("WidgetList.zig");
const tui = @import("tui.zig"); const tui = @import("tui.zig");
const IndentMode = @import("config").IndentMode; const IndentMode = @import("config").IndentMode;
const WhitespaceMode = @import("config").WhitespaceMode;
pub const Cursor = Buffer.Cursor; pub const Cursor = Buffer.Cursor;
pub const View = Buffer.View; pub const View = Buffer.View;
@ -408,7 +409,6 @@ pub const Editor = struct {
} = null, } = null,
} = null, } = null,
const WhitespaceMode = enum { indent, leading, eol, tabs, visible, full, none };
const StyleCache = std.AutoHashMap(u32, ?Widget.Theme.Token); const StyleCache = std.AutoHashMap(u32, ?Widget.Theme.Token);
const Context = command.Context; const Context = command.Context;
@ -539,7 +539,7 @@ pub const Editor = struct {
.animation_last_time = time.microTimestamp(), .animation_last_time = time.microTimestamp(),
.enable_format_on_save = tui.config().enable_format_on_save, .enable_format_on_save = tui.config().enable_format_on_save,
.enable_terminal_cursor = tui.config().enable_terminal_cursor, .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()); 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 { pub fn need_render(_: *Self) void {
Widget.need_render(); Widget.need_render();
} }
@ -6478,6 +6461,7 @@ pub const EditorWidget = struct {
var ypx: c_int = undefined; var ypx: c_int = undefined;
var pos: u32 = 0; var pos: u32 = 0;
var bytes: []const u8 = ""; 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) })) { 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); 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(.{}); try self.editor.highlight_references(.{});
}, },
}; };
} else if (try m.match(.{ "whitespace_mode", tp.extract(&bytes) })) { } else if (try m.match(.{ "whitespace_mode", tp.extract(&whitespace_mode) })) {
self.editor.render_whitespace = Editor.from_whitespace_mode(bytes); self.editor.render_whitespace = whitespace_mode;
} else { } else {
return false; return false;
} }

View file

@ -990,25 +990,20 @@ const cmds = struct {
pub const theme_prev_meta: Meta = .{ .description = "Previous color theme" }; pub const theme_prev_meta: Meta = .{ .description = "Previous color theme" };
pub fn toggle_whitespace_mode(self: *Self, _: Ctx) Result { pub fn toggle_whitespace_mode(self: *Self, _: Ctx) Result {
self.config_.whitespace_mode = if (std.mem.eql(u8, self.config_.whitespace_mode, "none")) self.config_.whitespace_mode = switch (self.config_.whitespace_mode) {
"indent" .none => .indent,
else if (std.mem.eql(u8, self.config_.whitespace_mode, "indent")) .indent => .leading,
"leading" .leading => .eol,
else if (std.mem.eql(u8, self.config_.whitespace_mode, "leading")) .eol => .tabs,
"eol" .tabs => .visible,
else if (std.mem.eql(u8, self.config_.whitespace_mode, "eol")) .visible => .full,
"tabs" .full => .none,
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";
try save_config(); try save_config();
var buf: [32]u8 = undefined; var buf: [32]u8 = undefined;
const m = try tp.message.fmtbuf(&buf, .{ "whitespace_mode", self.config_.whitespace_mode }); const m = try tp.message.fmtbuf(&buf, .{ "whitespace_mode", self.config_.whitespace_mode });
_ = try self.send_widgets(tp.self_pid(), m); _ = 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" }; pub const toggle_whitespace_mode_meta: Meta = .{ .description = "Next whitespace mode" };