From a706e0b976be8361ff4d9d0bcfacb1eb6b8cf261 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Mon, 27 Jan 2025 19:50:19 +0100 Subject: [PATCH] feat: allow setting the file type in create_scratch_buffer command Also, fix args forwarding. --- src/tui/editor.zig | 20 ++++++++++++-------- src/tui/mainview.zig | 6 ++++-- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 4c71ba4..b368c65 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -504,14 +504,14 @@ pub const Editor = struct { } fn open(self: *Self, file_path: []const u8) !void { - return self.open_buffer(file_path, try self.buffer_manager.open_file(file_path)); + return self.open_buffer(file_path, try self.buffer_manager.open_file(file_path), null); } - fn open_scratch(self: *Self, file_path: []const u8, content: []const u8) !void { - return self.open_buffer(file_path, try self.buffer_manager.open_scratch(file_path, content)); + fn open_scratch(self: *Self, file_path: []const u8, content: []const u8, file_type: ?[]const u8) !void { + return self.open_buffer(file_path, try self.buffer_manager.open_scratch(file_path, content), file_type); } - fn open_buffer(self: *Self, file_path: []const u8, new_buf: *Buffer) !void { + fn open_buffer(self: *Self, file_path: []const u8, new_buf: *Buffer, file_type: ?[]const u8) !void { errdefer self.buffer_manager.retire(new_buf); self.cancel_all_selections(); self.get_primary().reset(); @@ -528,7 +528,7 @@ pub const Editor = struct { self.syntax_no_render = true; } self.syntax = syntax: { - const lang_override = tp.env.get().str("language"); + const lang_override = file_type orelse tp.env.get().str("language"); var content = std.ArrayList(u8).init(self.allocator); defer content.deinit(); try new_buf.root.store(content.writer(), new_buf.file_eol_mode); @@ -3751,11 +3751,15 @@ pub const Editor = struct { pub fn open_scratch_buffer(self: *Self, ctx: Context) Result { var file_path: []const u8 = undefined; var content: []const u8 = undefined; - if (ctx.args.match(.{ tp.extract(&file_path), tp.extract(&content) }) catch false) { - try self.open_scratch(file_path, content); + var file_type: []const u8 = undefined; + if (ctx.args.match(.{ tp.extract(&file_path), tp.extract(&content), tp.extract(&file_type) }) catch false) { + try self.open_scratch(file_path, content, file_type); + self.clamp(); + } else if (ctx.args.match(.{ tp.extract(&file_path), tp.extract(&content) }) catch false) { + try self.open_scratch(file_path, content, null); self.clamp(); } else if (ctx.args.match(.{tp.extract(&file_path)}) catch false) { - try self.open_scratch(file_path, ""); + try self.open_scratch(file_path, "", null); self.clamp(); } else return error.InvalidOpenScratchBufferArgument; } diff --git a/src/tui/mainview.zig b/src/tui/mainview.zig index 9aee593..42a89b8 100644 --- a/src/tui/mainview.zig +++ b/src/tui/mainview.zig @@ -386,12 +386,14 @@ const cmds = struct { pub const open_gui_config_meta = .{ .description = "Edit gui configuration file" }; pub fn create_scratch_buffer(self: *Self, ctx: Ctx) Result { + const args = try ctx.args.clone(self.allocator); + defer self.allocator.free(args.buf); tui.reset_drag_context(); try self.create_editor(); - try command.executeName("open_scratch_buffer", ctx); + try command.executeName("open_scratch_buffer", .{ .args = args }); tui.need_render(); } - pub const create_scratch_buffer_meta = .{ .arguments = &.{ .string, .string } }; + pub const create_scratch_buffer_meta = .{ .arguments = &.{ .string, .string, .string } }; pub fn create_new_file(self: *Self, _: Ctx) Result { var n: usize = 1;