feat: allow setting the file type in create_scratch_buffer command

Also, fix args forwarding.
This commit is contained in:
CJ van den Berg 2025-01-27 19:50:19 +01:00
parent 404ba8bb0e
commit a706e0b976
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 16 additions and 10 deletions

View file

@ -504,14 +504,14 @@ pub const Editor = struct {
} }
fn open(self: *Self, file_path: []const u8) !void { 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 { 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)); 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); errdefer self.buffer_manager.retire(new_buf);
self.cancel_all_selections(); self.cancel_all_selections();
self.get_primary().reset(); self.get_primary().reset();
@ -528,7 +528,7 @@ pub const Editor = struct {
self.syntax_no_render = true; self.syntax_no_render = true;
} }
self.syntax = syntax: { 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); var content = std.ArrayList(u8).init(self.allocator);
defer content.deinit(); defer content.deinit();
try new_buf.root.store(content.writer(), new_buf.file_eol_mode); 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 { pub fn open_scratch_buffer(self: *Self, ctx: Context) Result {
var file_path: []const u8 = undefined; var file_path: []const u8 = undefined;
var content: []const u8 = undefined; var content: []const u8 = undefined;
if (ctx.args.match(.{ tp.extract(&file_path), tp.extract(&content) }) catch false) { var file_type: []const u8 = undefined;
try self.open_scratch(file_path, content); 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(); self.clamp();
} else if (ctx.args.match(.{tp.extract(&file_path)}) catch false) { } 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(); self.clamp();
} else return error.InvalidOpenScratchBufferArgument; } else return error.InvalidOpenScratchBufferArgument;
} }

View file

@ -386,12 +386,14 @@ const cmds = struct {
pub const open_gui_config_meta = .{ .description = "Edit gui configuration file" }; pub const open_gui_config_meta = .{ .description = "Edit gui configuration file" };
pub fn create_scratch_buffer(self: *Self, ctx: Ctx) Result { 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(); tui.reset_drag_context();
try self.create_editor(); try self.create_editor();
try command.executeName("open_scratch_buffer", ctx); try command.executeName("open_scratch_buffer", .{ .args = args });
tui.need_render(); 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 { pub fn create_new_file(self: *Self, _: Ctx) Result {
var n: usize = 1; var n: usize = 1;