diff --git a/src/config.zig b/src/config.zig index 22c233c..257f6bb 100644 --- a/src/config.zig +++ b/src/config.zig @@ -10,7 +10,7 @@ enable_terminal_cursor: bool = true, enable_terminal_color_scheme: bool = builtin.os.tag != .windows, highlight_current_line: bool = true, highlight_current_line_gutter: bool = true, -highlight_columns: []const u8 = "80 100 120", +highlight_columns: []const u16 = &.{ 80, 100, 120 }, highlight_columns_alpha: u8 = 240, highlight_columns_enabled: bool = false, whitespace_mode: []const u8 = "none", @@ -21,6 +21,8 @@ enable_format_on_save: bool = false, restore_last_cursor_position: bool = true, follow_cursor_on_buffer_switch: bool = false, //scroll cursor into view on buffer switch default_cursor: []const u8 = "default", +enable_auto_save: bool = false, +limit_auto_save_file_types: ?[]const []const u8 = null, // null means *all* indent_size: usize = 4, tab_width: usize = 8, diff --git a/src/main.zig b/src/main.zig index 01641d9..07e1cf5 100644 --- a/src/main.zig +++ b/src/main.zig @@ -687,6 +687,14 @@ fn config_eql(comptime T: type, a: T, b: T) bool { return false; return config_eql(info.child, a.?, b.?); }, + .pointer => |info| switch (info.size) { + .slice => { + if (a.len != b.len) return false; + for (a, 0..) |x, i| if (!config_eql(info.child, x, b[i])) return false; + return true; + }, + else => @compileError("unsupported config type " ++ @typeName(T)), + }, else => {}, } @compileError("unsupported config type " ++ @typeName(T)); diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 74fafd9..c4bd75b 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -350,6 +350,11 @@ pub const Editor = struct { completions: std.ArrayListUnmanaged(u8) = .empty, + enable_auto_save: bool, + enable_format_on_save: bool, + + restored_state: bool = false, + need_save_after_filter: ?struct { then: ?struct { cmd: []const u8, @@ -365,10 +370,12 @@ pub const Editor = struct { const Result = command.Result; pub fn write_state(self: *const Self, writer: Buffer.MetaWriter) !void { - try cbor.writeArrayHeader(writer, 6); + try cbor.writeArrayHeader(writer, 8); try cbor.writeValue(writer, self.file_path orelse ""); try cbor.writeValue(writer, self.clipboard orelse ""); try cbor.writeValue(writer, self.last_find_query orelse ""); + try cbor.writeValue(writer, self.enable_format_on_save); + try cbor.writeValue(writer, self.enable_auto_save); if (self.find_history) |history| { try cbor.writeArrayHeader(writer, history.items.len); for (history.items) |item| @@ -391,16 +398,19 @@ pub const Editor = struct { pub fn extract_state(self: *Self, buf: []const u8, comptime op: enum { none, open_file }) !void { tp.trace(tp.channel.debug, .{ "extract_state", self.file_path }); tp.trace(tp.channel.debug, tp.message{ .buf = buf }); + self.restored_state = true; var file_path: []const u8 = undefined; var view_cbor: []const u8 = undefined; var cursels_cbor: []const u8 = undefined; var clipboard: []const u8 = undefined; - var query: []const u8 = undefined; + var last_find_query: []const u8 = undefined; var find_history: []const u8 = undefined; if (!try cbor.match(buf, .{ tp.extract(&file_path), tp.extract(&clipboard), - tp.extract(&query), + tp.extract(&last_find_query), + tp.extract(&self.enable_format_on_save), + tp.extract(&self.enable_auto_save), tp.extract_cbor(&find_history), tp.extract_cbor(&view_cbor), tp.extract_cbor(&cursels_cbor), @@ -409,7 +419,7 @@ pub const Editor = struct { if (op == .open_file) try self.open(file_path); self.clipboard = if (clipboard.len > 0) try self.allocator.dupe(u8, clipboard) else null; - self.last_find_query = if (query.len > 0) try self.allocator.dupe(u8, clipboard) else null; + self.last_find_query = if (last_find_query.len > 0) try self.allocator.dupe(u8, last_find_query) else null; const rows = self.view.rows; const cols = self.view.cols; if (!try self.view.extract(&view_cbor)) @@ -458,6 +468,8 @@ pub const Editor = struct { .animation_lag = get_animation_max_lag(), .animation_frame_rate = frame_rate, .animation_last_time = time.microTimestamp(), + .enable_auto_save = tui.config().enable_auto_save, + .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), }; @@ -590,6 +602,8 @@ pub const Editor = struct { file_type_config.guess_file_type(self.file_path, content.items); }; + self.maybe_enable_auto_save(); + const syn = blk: { const frame_ = tracy.initZone(@src(), .{ .name = "create" }); defer frame_.deinit(); @@ -633,6 +647,22 @@ pub const Editor = struct { try self.send_editor_open(file_path, new_buf.file_exists, ftn, fti, ftc); } + fn maybe_enable_auto_save(self: *Self) void { + if (self.restored_state) return; + self.enable_auto_save = false; + if (!tui.config().enable_auto_save) return; + const self_file_type = self.file_type orelse return; + + enable: { + const file_types = tui.config().limit_auto_save_file_types orelse break :enable; + for (file_types) |file_type| + if (std.mem.eql(u8, file_type, self_file_type.name)) + break :enable; + return; + } + self.enable_auto_save = true; + } + fn close(self: *Self) !void { var meta = std.ArrayListUnmanaged(u8).empty; defer meta.deinit(self.allocator); @@ -1684,6 +1714,8 @@ pub const Editor = struct { _ = try self.handlers.msg(.{ "E", "update", token_from(new_root), token_from(old_root), @intFromEnum(eol_mode) }); if (self.syntax) |_| if (self.file_path) |file_path| if (old_root != null and new_root != null) project_manager.did_change(file_path, self.lsp_version, try text_from_root(new_root, eol_mode), try text_from_root(old_root, eol_mode), eol_mode) catch {}; + if (self.enable_auto_save) + tp.self_pid().send(.{ "cmd", "save_file", .{} }) catch {}; } fn send_editor_eol_mode(self: *const Self, eol_mode: Buffer.EolMode, utf8_sanitized: bool) !void { @@ -4767,6 +4799,16 @@ pub const Editor = struct { pub const SaveOption = enum { default, format, no_format }; + pub fn toggle_auto_save(self: *Self, _: Context) Result { + self.enable_auto_save = !self.enable_auto_save; + } + pub const toggle_auto_save_meta: Meta = .{ .description = "Toggle auto save" }; + + pub fn toggle_format_on_save(self: *Self, _: Context) Result { + self.enable_format_on_save = !self.enable_format_on_save; + } + pub const toggle_format_on_save_meta: Meta = .{ .description = "Toggle format on save" }; + pub fn save_file(self: *Self, ctx: Context) Result { var option: SaveOption = .default; var then = false; @@ -4780,7 +4822,7 @@ pub const Editor = struct { _ = ctx.args.match(.{tp.extract(&option)}) catch false; } - if ((option == .default and tui.config().enable_format_on_save) or option == .format) if (self.get_formatter()) |_| { + if ((option == .default and self.enable_format_on_save) or option == .format) if (self.get_formatter()) |_| { self.need_save_after_filter = .{ .then = if (then) .{ .cmd = cmd, .args = args } else null }; const primary = self.get_primary(); const sel = primary.selection; diff --git a/src/tui/tui.zig b/src/tui/tui.zig index 23633c8..6472742 100644 --- a/src/tui/tui.zig +++ b/src/tui/tui.zig @@ -23,8 +23,9 @@ const Allocator = std.mem.Allocator; allocator: Allocator, rdr_: renderer, config_: @import("config"), -highlight_columns_: []u16, -highlight_columns_configured: []u16, +config_bufs: [][]const u8, +highlight_columns_: []const u16, +highlight_columns_configured: []const u16, frame_time: usize, // in microseconds frame_clock: tp.metronome, frame_clock_running: bool = false, @@ -103,19 +104,12 @@ const InitError = error{ fn init(allocator: Allocator) InitError!*Self { var conf, const conf_bufs = root.read_config(@import("config"), allocator); - defer root.free_config(allocator, conf_bufs); if (@hasDecl(renderer, "install_crash_handler") and conf.start_debugger_on_crash) renderer.jit_debugger_enabled = true; const theme_, const parsed_theme = get_theme_by_name(allocator, conf.theme) orelse get_theme_by_name(allocator, "dark_modern") orelse return error.UnknownTheme; conf.theme = theme_.name; - conf.whitespace_mode = try allocator.dupe(u8, conf.whitespace_mode); - conf.input_mode = try allocator.dupe(u8, conf.input_mode); - conf.top_bar = try allocator.dupe(u8, conf.top_bar); - conf.bottom_bar = try allocator.dupe(u8, conf.bottom_bar); - conf.include_files = try allocator.dupe(u8, conf.include_files); - conf.highlight_columns = try allocator.dupe(u8, conf.highlight_columns); if (build_options.gui) conf.enable_terminal_cursor = false; const frame_rate: usize = @intCast(tp.env.get().num("frame-rate")); @@ -125,21 +119,13 @@ fn init(allocator: Allocator) InitError!*Self { const frame_time = std.time.us_per_s / conf.frame_rate; const frame_clock = try tp.metronome.init(frame_time); - const hl_cols: usize = blk: { - var it = std.mem.splitScalar(u8, conf.highlight_columns, ' '); - var idx: usize = 0; - while (it.next()) |_| - idx += 1; - break :blk idx; - }; - const highlight_columns__ = try allocator.alloc(u16, hl_cols); - var self = try allocator.create(Self); self.* = .{ .allocator = allocator, .config_ = conf, - .highlight_columns_ = if (conf.highlight_columns_enabled) highlight_columns__ else &.{}, - .highlight_columns_configured = highlight_columns__, + .config_bufs = conf_bufs, + .highlight_columns_ = if (conf.highlight_columns_enabled) conf.highlight_columns else &.{}, + .highlight_columns_configured = conf.highlight_columns, .rdr_ = try renderer.init(allocator, self, tp.env.get().is("no-alternate"), dispatch_initialized), .frame_time = frame_time, .frame_clock = frame_clock, @@ -159,13 +145,6 @@ fn init(allocator: Allocator) InitError!*Self { instance_ = self; defer instance_ = null; - var it = std.mem.splitScalar(u8, conf.highlight_columns, ' '); - var idx: usize = 0; - while (it.next()) |arg| { - highlight_columns__[idx] = std.fmt.parseInt(u16, arg, 10) catch 0; - idx += 1; - } - self.default_cursor = std.meta.stringToEnum(keybind.CursorShape, conf.default_cursor) orelse .default; self.config_.default_cursor = @tagName(self.default_cursor); self.rdr_.handler_ctx = self; @@ -225,7 +204,6 @@ fn init_delayed(self: *Self) command.Result { } fn deinit(self: *Self) void { - self.allocator.free(self.highlight_columns_configured); if (self.mouse_idle_timer) |*t| { t.cancel() catch {}; t.deinit(); @@ -256,6 +234,7 @@ fn deinit(self: *Self) void { self.rdr_.deinit(); self.logger.deinit(); self.query_cache_.deinit(); + root.free_config(self.allocator, self.config_bufs); self.allocator.destroy(self); }