refactor: convert whitespace_mode config options to an enum
This commit is contained in:
parent
e2009425f5
commit
3dc731d086
3 changed files with 26 additions and 37 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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" };
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue