From 3e0e75c9c8f5efcfb43a4fa4797e3533b6da947a Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Mon, 11 Aug 2025 14:29:23 +0200 Subject: [PATCH] feat: add interactive and non-interactive commands to set the current buffer's tab_width --- src/tui/editor.zig | 18 ++++++++++++++++++ src/tui/mode/mini/tab_width.zig | 16 ++++++++++++++++ src/tui/tui.zig | 5 +++++ 3 files changed, 39 insertions(+) create mode 100644 src/tui/mode/mini/tab_width.zig diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 701626d..b9fce38 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -432,6 +432,7 @@ pub const Editor = struct { tp.extract_cbor(&cursels_cbor), })) return error.RestoreStateMatch; + self.refresh_tab_width(); if (op == .open_file) try self.open(file_path); self.clipboard = if (clipboard.len > 0) try self.allocator.dupe(u8, clipboard) else null; @@ -702,6 +703,23 @@ pub const Editor = struct { return; } + fn refresh_tab_width(self: *Self) void { + self.metrics = self.plane.metrics(self.tab_width); + switch (self.indent_mode) { + .spaces, .auto => {}, + .tabs => self.indent_size = self.tab_width, + } + } + + pub fn set_tab_width(self: *Self, ctx: Context) Result { + var tab_width: usize = 0; + if (!try ctx.args.match(.{tp.extract(&tab_width)})) + return error.InvalidSetTabWidthArgument; + self.tab_width = tab_width; + self.refresh_tab_width(); + } + pub const set_tab_width_meta: Meta = .{ .arguments = &.{.integer} }; + fn close(self: *Self) !void { var meta = std.ArrayListUnmanaged(u8).empty; defer meta.deinit(self.allocator); diff --git a/src/tui/mode/mini/tab_width.zig b/src/tui/mode/mini/tab_width.zig new file mode 100644 index 0000000..a2ca836 --- /dev/null +++ b/src/tui/mode/mini/tab_width.zig @@ -0,0 +1,16 @@ +const command = @import("command"); + +pub const Type = @import("numeric_input.zig").Create(@This()); +pub const create = Type.create; + +pub fn name(_: *Type) []const u8 { + return " tab size"; +} + +pub const preview = goto; +pub const apply = goto; +pub const cancel = goto; + +fn goto(self: *Type) void { + command.executeName("set_tab_width", command.fmt(.{self.input orelse self.start})) catch {}; +} diff --git a/src/tui/tui.zig b/src/tui/tui.zig index 3a3e7c8..f762102 100644 --- a/src/tui/tui.zig +++ b/src/tui/tui.zig @@ -744,6 +744,11 @@ const cmds = struct { } pub const force_terminate_meta: Meta = .{ .description = "Force quit without saving" }; + pub fn tab_width(self: *Self, ctx: Ctx) Result { + return enter_mini_mode(self, @import("mode/mini/tab_width.zig"), ctx); + } + pub const tab_width_meta: Meta = .{ .description = "Set tab width" }; + pub fn set_theme(self: *Self, ctx: Ctx) Result { var name: []const u8 = undefined; if (!try ctx.args.match(.{tp.extract(&name)}))