refactor: move auto_save flag from Editor to Buffer

This commit is contained in:
CJ van den Berg 2025-11-03 22:13:54 +01:00
parent fa6ea11d57
commit 30b1329d10
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 23 additions and 9 deletions

View file

@ -42,6 +42,7 @@ last_save_eol_mode: EolMode = .lf,
file_utf8_sanitized: bool = false, file_utf8_sanitized: bool = false,
hidden: bool = false, hidden: bool = false,
ephemeral: bool = false, ephemeral: bool = false,
auto_save: bool = false,
meta: ?[]const u8 = null, meta: ?[]const u8 = null,
lsp_version: usize = 1, lsp_version: usize = 1,
@ -1479,6 +1480,18 @@ pub fn mark_not_ephemeral(self: *Self) void {
self.ephemeral = false; self.ephemeral = false;
} }
pub fn enable_auto_save(self: *Self) void {
self.auto_save = true;
}
pub fn disable_auto_save(self: *Self) void {
self.auto_save = false;
}
pub fn is_auto_save(self: *const Self) bool {
return self.auto_save;
}
pub fn is_dirty(self: *const Self) bool { pub fn is_dirty(self: *const Self) bool {
return if (!self.file_exists) return if (!self.file_exists)
self.root.length() > 0 self.root.length() > 0
@ -1576,6 +1589,7 @@ pub fn write_state(self: *const Self, writer: *std.Io.Writer) error{ Stop, OutOf
try cbor.writeValue(writer, self.file_eol_mode); try cbor.writeValue(writer, self.file_eol_mode);
try cbor.writeValue(writer, self.hidden); try cbor.writeValue(writer, self.hidden);
try cbor.writeValue(writer, self.ephemeral); try cbor.writeValue(writer, self.ephemeral);
try cbor.writeValue(writer, self.auto_save);
try cbor.writeValue(writer, dirty); try cbor.writeValue(writer, dirty);
try cbor.writeValue(writer, self.meta); try cbor.writeValue(writer, self.meta);
try cbor.writeValue(writer, self.file_type_name); try cbor.writeValue(writer, self.file_type_name);
@ -1597,6 +1611,7 @@ pub fn extract_state(self: *Self, iter: *[]const u8) !void {
cbor.extract(&self.file_eol_mode), cbor.extract(&self.file_eol_mode),
cbor.extract(&self.hidden), cbor.extract(&self.hidden),
cbor.extract(&self.ephemeral), cbor.extract(&self.ephemeral),
cbor.extract(&self.auto_save),
cbor.extract(&dirty), cbor.extract(&dirty),
cbor.extract(&meta), cbor.extract(&meta),
cbor.extract(&file_type_name), cbor.extract(&file_type_name),

View file

@ -383,7 +383,6 @@ pub const Editor = struct {
completion_col: usize = 0, completion_col: usize = 0,
completion_is_complete: bool = true, completion_is_complete: bool = true,
enable_auto_save: bool,
enable_format_on_save: bool, enable_format_on_save: bool,
restored_state: bool = false, restored_state: bool = false,
@ -411,11 +410,10 @@ pub const Editor = struct {
} }
pub fn write_state(self: *const Self, writer: *std.Io.Writer) !void { pub fn write_state(self: *const Self, writer: *std.Io.Writer) !void {
try cbor.writeArrayHeader(writer, 11); try cbor.writeArrayHeader(writer, 10);
try cbor.writeValue(writer, self.file_path orelse ""); try cbor.writeValue(writer, self.file_path orelse "");
try cbor.writeValue(writer, self.last_find_query 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_format_on_save);
try cbor.writeValue(writer, self.enable_auto_save);
try cbor.writeValue(writer, self.indent_size); try cbor.writeValue(writer, self.indent_size);
try cbor.writeValue(writer, self.tab_width); try cbor.writeValue(writer, self.tab_width);
try cbor.writeValue(writer, self.indent_mode); try cbor.writeValue(writer, self.indent_mode);
@ -450,7 +448,6 @@ pub const Editor = struct {
tp.extract(&file_path), tp.extract(&file_path),
tp.extract(&last_find_query), tp.extract(&last_find_query),
tp.extract(&self.enable_format_on_save), tp.extract(&self.enable_format_on_save),
tp.extract(&self.enable_auto_save),
tp.extract(&self.indent_size), tp.extract(&self.indent_size),
tp.extract(&self.tab_width), tp.extract(&self.tab_width),
tp.extract(&self.indent_mode), tp.extract(&self.indent_mode),
@ -514,7 +511,6 @@ pub const Editor = struct {
.animation_lag = get_animation_max_lag(), .animation_lag = get_animation_max_lag(),
.animation_frame_rate = frame_rate, .animation_frame_rate = frame_rate,
.animation_last_time = time.microTimestamp(), .animation_last_time = time.microTimestamp(),
.enable_auto_save = tui.config().enable_auto_save,
.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 = from_whitespace_mode(tui.config().whitespace_mode),
@ -598,6 +594,7 @@ pub const Editor = struct {
defer frame.deinit(); defer frame.deinit();
break :blk try self.buffer_manager.open_file(file_path); break :blk try self.buffer_manager.open_file(file_path);
}; };
if (tui.config().enable_auto_save) buffer.enable_auto_save();
return self.open_buffer(file_path, buffer, null); return self.open_buffer(file_path, buffer, null);
} }
@ -689,6 +686,7 @@ pub const Editor = struct {
buffer.file_type_icon = fti; buffer.file_type_icon = fti;
buffer.file_type_color = ftc; buffer.file_type_color = ftc;
} }
const auto_save = if (self.buffer) |b| b.is_auto_save() else false;
if (buffer_meta) |meta| { if (buffer_meta) |meta| {
const frame_ = tracy.initZone(@src(), .{ .name = "extract_state" }); const frame_ = tracy.initZone(@src(), .{ .name = "extract_state" });
@ -700,8 +698,9 @@ pub const Editor = struct {
} }
fn maybe_enable_auto_save(self: *Self) void { fn maybe_enable_auto_save(self: *Self) void {
const buffer = self.buffer orelse return;
if (self.restored_state) return; if (self.restored_state) return;
self.enable_auto_save = false; buffer.disable_auto_save();
if (!tui.config().enable_auto_save) return; if (!tui.config().enable_auto_save) return;
const self_file_type = self.file_type orelse return; const self_file_type = self.file_type orelse return;
@ -712,7 +711,7 @@ pub const Editor = struct {
break :enable; break :enable;
return; return;
} }
self.enable_auto_save = true; buffer.enable_auto_save();
} }
fn detect_indent_mode(self: *Self, content: []const u8) void { fn detect_indent_mode(self: *Self, content: []const u8) void {
@ -1781,7 +1780,7 @@ pub const Editor = struct {
_ = try self.handlers.msg(.{ "E", "update", token_from(new_root), token_from(old_root), @intFromEnum(eol_mode) }); _ = try self.handlers.msg(.{ "E", "update", token_from(new_root), token_from(old_root), @intFromEnum(eol_mode) });
if (self.buffer) |buffer| if (self.syntax) |_| if (self.file_path) |file_path| if (old_root != null and new_root != null) if (self.buffer) |buffer| if (self.syntax) |_| if (self.file_path) |file_path| if (old_root != null and new_root != null)
project_manager.did_change(file_path, buffer.lsp_version, try text_from_root(new_root, eol_mode), try text_from_root(old_root, eol_mode), eol_mode) catch {}; project_manager.did_change(file_path, buffer.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) if (self.buffer) |b| if (b.is_auto_save())
tp.self_pid().send(.{ "cmd", "save_file", .{} }) catch {}; tp.self_pid().send(.{ "cmd", "save_file", .{} }) catch {};
} }
@ -5005,7 +5004,7 @@ pub const Editor = struct {
pub const SaveOption = enum { default, format, no_format }; pub const SaveOption = enum { default, format, no_format };
pub fn toggle_auto_save(self: *Self, _: Context) Result { pub fn toggle_auto_save(self: *Self, _: Context) Result {
self.enable_auto_save = !self.enable_auto_save; if (self.buffer) |b| if (b.is_auto_save()) b.disable_auto_save() else b.enable_auto_save();
} }
pub const toggle_auto_save_meta: Meta = .{ .description = "Toggle auto save" }; pub const toggle_auto_save_meta: Meta = .{ .description = "Toggle auto save" };