feat: port editor to use configurable file types

This commit is contained in:
CJ van den Berg 2025-07-14 13:25:58 +02:00
parent 818b2cf915
commit bffc56b618
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
3 changed files with 43 additions and 49 deletions

View file

@ -265,22 +265,6 @@ fn open_style_config(self: *Self, Style: type) command.Result {
if (self.get_active_buffer()) |buffer| buffer.mark_not_ephemeral();
}
fn get_file_type_config_file_path(allocator: std.mem.Allocator, file_type: []const u8) ![]const u8 {
var stream = std.ArrayList(u8).init(allocator);
const writer = stream.writer();
_ = try writer.writeAll(try root.get_config_dir());
_ = try writer.writeByte(std.fs.path.sep);
_ = try writer.writeAll("file_type");
_ = try writer.writeByte(std.fs.path.sep);
std.fs.makeDirAbsolute(stream.items) catch |e| switch (e) {
error.PathAlreadyExists => {},
else => return e,
};
_ = try writer.writeAll(file_type);
_ = try writer.writeAll(".conf");
return stream.toOwnedSlice();
}
const cmds = struct {
pub const Target = Self;
const Ctx = command.Context;
@ -524,7 +508,7 @@ const cmds = struct {
@import("mode/overlay/file_type_palette.zig").Variant("open_file_type_config", "Edit file type", true).Type,
);
const file_name = try get_file_type_config_file_path(self.allocator, file_type_name);
const file_name = try file_type_config.get_config_file_path(self.allocator, file_type_name);
defer self.allocator.free(file_name);
const file: ?std.fs.File = std.fs.openFileAbsolute(file_name, .{ .mode = .read_only }) catch null;
@ -533,17 +517,14 @@ const cmds = struct {
return tp.self_pid().send(.{ "cmd", "navigate", .{ .file = file_name } });
}
const file_type = syntax.FileType.get_by_name(file_type_name) orelse return error.UnknownFileType;
const config = file_type_config.from_file_type(file_type);
const content = try file_type_config.get_default(self.allocator, file_type_name);
defer self.allocator.free(content);
var conf = std.ArrayListUnmanaged(u8).empty;
defer conf.deinit(self.allocator);
root.write_config_to_writer(file_type_config, config, conf.writer(self.allocator)) catch {};
tui.reset_drag_context();
try self.create_editor();
try command.executeName("open_scratch_buffer", command.fmt(.{
file_name,
conf.items,
content,
"conf",
}));
if (self.get_active_buffer()) |buffer| buffer.mark_not_ephemeral();