fix: queue quit command to run after an async save_file

closes #60
This commit is contained in:
CJ van den Berg 2024-10-27 20:31:07 +01:00
parent 67813fbb07
commit a9058ef949
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 27 additions and 7 deletions

View file

@ -262,7 +262,12 @@ pub const Editor = struct {
diag_info: usize = 0,
diag_hints: usize = 0,
need_save_after_filter: bool = false,
need_save_after_filter: ?struct {
then: ?struct {
cmd: []const u8,
args: []const u8,
} = null,
} = null,
case_data: ?CaseData = null,
@ -3167,9 +3172,15 @@ pub const Editor = struct {
}
pub const open_scratch_buffer_meta = .{ .interactive = false };
pub fn save_file(self: *Self, _: Context) Result {
pub fn save_file(self: *Self, ctx: Context) Result {
var then = false;
var cmd: []const u8 = undefined;
var args: []const u8 = undefined;
if (ctx.args.match(.{ "then", .{ tp.extract(&cmd), tp.extract_cbor(&args) } }) catch false) {
then = true;
}
if (tui.current().config.enable_format_on_save) if (self.get_formatter()) |_| {
self.need_save_after_filter = true;
self.need_save_after_filter = .{ .then = if (then) .{ .cmd = cmd, .args = args } else null };
const primary = self.get_primary();
const sel = primary.selection;
primary.selection = null;
@ -3178,6 +3189,8 @@ pub const Editor = struct {
return;
};
try self.save();
if (then)
return command.executeName(cmd, .{ .args = .{ .buf = args } });
}
pub const save_file_meta = .{ .description = "Save file" };
@ -3836,7 +3849,11 @@ pub const Editor = struct {
fn filter_error(self: *Self, bytes: []const u8) !void {
defer self.filter_deinit();
self.logger.print("filter: ERR: {s}", .{bytes});
if (self.need_save_after_filter) try self.save();
if (self.need_save_after_filter) |info| {
try self.save();
if (info.then) |then|
return command.executeName(then.cmd, .{ .args = .{ .buf = then.args } });
}
}
fn filter_done(self: *Self) !void {
@ -3866,7 +3883,11 @@ pub const Editor = struct {
self.reset_syntax();
self.clamp();
self.need_render();
if (self.need_save_after_filter) try self.save();
if (self.need_save_after_filter) |info| {
try self.save();
if (info.then) |then|
return command.executeName(then.cmd, .{ .args = .{ .buf = then.args } });
}
}
fn filter_deinit(self: *Self) void {

View file

@ -644,8 +644,7 @@ const cmds_ = struct {
pub const @"q!_meta" = .{ .description = "q! (quit without saving)" };
pub fn wq(self: *Self, _: Ctx) Result {
try self.cmd("save_file", .{});
try self.cmd("quit", .{});
try self.cmd("save_file", command.fmt(.{ "then", .{ "quit", .{} } }));
}
pub const wq_meta = .{ .description = "wq (write file and quit)" };