From 514e175ef4de48456dde6fa2ef31a3228ab375ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gero=20Schw=C3=A4ricke?= Date: Sat, 1 Mar 2025 21:22:29 +0100 Subject: [PATCH 1/3] feat: save file without formatting --- src/tui/editor.zig | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 8c562b1..8800624 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -4294,14 +4294,14 @@ pub const Editor = struct { } pub const open_scratch_buffer_meta = .{ .arguments = &.{ .string, .string } }; - pub fn save_file(self: *Self, ctx: Context) Result { + pub fn format_and_save_file(self: *Self, ctx: Context, format_file: bool) 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.config().enable_format_on_save) if (self.get_formatter()) |_| { + if (format_file and tui.config().enable_format_on_save) 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; @@ -4314,8 +4314,17 @@ pub const Editor = struct { if (then) return command.executeName(cmd, .{ .args = .{ .buf = args } }); } + + pub fn save_file(self: *Self, ctx: Context) Result { + return self.format_and_save_file(ctx, true); + } pub const save_file_meta = .{ .description = "Save file" }; + pub fn save_file_without_formatting(self: *Self, ctx: Context) Result { + return self.format_and_save_file(ctx, false); + } + pub const save_file_without_formatting_meta = .{ .description = "Save file without formatting" }; + pub fn save_file_as(self: *Self, ctx: Context) Result { var file_path: []const u8 = undefined; if (ctx.args.match(.{tp.extract(&file_path)}) catch false) { From 70612f211afbd3a5b706f4d8f464b2abc5c8b641 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Sun, 2 Mar 2025 21:47:44 +0100 Subject: [PATCH 2/3] feat: add save_file_with_formatting command --- src/tui/editor.zig | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 8800624..51c1bf5 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -4294,14 +4294,22 @@ pub const Editor = struct { } pub const open_scratch_buffer_meta = .{ .arguments = &.{ .string, .string } }; - pub fn format_and_save_file(self: *Self, ctx: Context, format_file: bool) Result { + pub const SaveOption = enum { default, format, no_format }; + + pub fn save_file(self: *Self, ctx: Context) Result { + var option: SaveOption = .default; 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) { + if (ctx.args.match(.{ tp.extract(&option), "then", .{ tp.extract(&cmd), tp.extract_cbor(&args) } }) catch false) { then = true; + } else if (ctx.args.match(.{ "then", .{ tp.extract(&cmd), tp.extract_cbor(&args) } }) catch false) { + then = true; + } else { + _ = ctx.args.match(.{tp.extract(&option)}) catch false; } - if (format_file and tui.config().enable_format_on_save) if (self.get_formatter()) |_| { + + if ((option == .default and tui.config().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; @@ -4314,14 +4322,15 @@ pub const Editor = struct { if (then) return command.executeName(cmd, .{ .args = .{ .buf = args } }); } - - pub fn save_file(self: *Self, ctx: Context) Result { - return self.format_and_save_file(ctx, true); - } pub const save_file_meta = .{ .description = "Save file" }; - pub fn save_file_without_formatting(self: *Self, ctx: Context) Result { - return self.format_and_save_file(ctx, false); + pub fn save_file_with_formatting(self: *Self, _: Context) Result { + return self.save_file(Context.fmt(.{"format"})); + } + pub const save_file_with_formatting_meta = .{ .description = "Save file with formatting" }; + + pub fn save_file_without_formatting(self: *Self, _: Context) Result { + return self.save_file(Context.fmt(.{"no_format"})); } pub const save_file_without_formatting_meta = .{ .description = "Save file without formatting" }; From afcda4fc0168200516e8aae8f13d2d1a3a58c8b4 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Sun, 2 Mar 2025 21:48:34 +0100 Subject: [PATCH 3/3] fix: don't restore editor view rows and cols --- src/tui/editor.zig | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 51c1bf5..6556c15 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -401,9 +401,13 @@ pub const Editor = struct { 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; + const rows = self.view.rows; + const cols = self.view.cols; if (!try self.view.extract(&view_cbor)) return error.RestoreView; self.scroll_dest = self.view.row; + self.view.rows = rows; + self.view.cols = cols; if (cursels_cbor.len > 0) self.clear_all_cursors();