feat: add limit_auto_save_file_types config option

closes #77
This commit is contained in:
CJ van den Berg 2025-07-16 19:38:27 +02:00
parent 99664742c3
commit ada40b989c
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 22 additions and 1 deletions

View file

@ -22,7 +22,7 @@ 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,
auto_save_file_types: []const []const u8 = &.{},
limit_auto_save_file_types: ?[]const []const u8 = null, // null means *all*
indent_size: usize = 4,
tab_width: usize = 8,

View file

@ -353,6 +353,8 @@ pub const Editor = struct {
enable_auto_save: bool,
enable_format_on_save: bool,
restored_state: bool = false,
need_save_after_filter: ?struct {
then: ?struct {
cmd: []const u8,
@ -396,6 +398,7 @@ 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;
@ -599,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();
@ -642,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);