feat: add command to edit file type configuration files
This commit is contained in:
parent
8281f65011
commit
2897d8d745
5 changed files with 219 additions and 120 deletions
|
@ -317,6 +317,13 @@ pub fn build_exe(
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const file_type_config_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/file_type_config.zig"),
|
||||||
|
.imports = &.{
|
||||||
|
.{ .name = "syntax", .module = syntax_mod },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
const log_mod = b.createModule(.{
|
const log_mod = b.createModule(.{
|
||||||
.root_source_file = b.path("src/log.zig"),
|
.root_source_file = b.path("src/log.zig"),
|
||||||
.imports = &.{
|
.imports = &.{
|
||||||
|
@ -531,6 +538,7 @@ pub fn build_exe(
|
||||||
.{ .name = "cbor", .module = cbor_mod },
|
.{ .name = "cbor", .module = cbor_mod },
|
||||||
.{ .name = "config", .module = config_mod },
|
.{ .name = "config", .module = config_mod },
|
||||||
.{ .name = "gui_config", .module = gui_config_mod },
|
.{ .name = "gui_config", .module = gui_config_mod },
|
||||||
|
.{ .name = "file_type_config", .module = file_type_config_mod },
|
||||||
.{ .name = "log", .module = log_mod },
|
.{ .name = "log", .module = log_mod },
|
||||||
.{ .name = "command", .module = command_mod },
|
.{ .name = "command", .module = command_mod },
|
||||||
.{ .name = "EventHandler", .module = EventHandler_mod },
|
.{ .name = "EventHandler", .module = EventHandler_mod },
|
||||||
|
|
27
src/file_type_config.zig
Normal file
27
src/file_type_config.zig
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
description: ?[]const u8 = null,
|
||||||
|
extensions: ?[]const []const u8 = null,
|
||||||
|
icon: ?[]const u8 = null,
|
||||||
|
color: ?u24 = null,
|
||||||
|
comment: ?[]const u8 = null,
|
||||||
|
formatter: ?[]const []const u8 = null,
|
||||||
|
language_server: ?[]const []const u8 = null,
|
||||||
|
first_line_matches_prefix: ?[]const u8 = null,
|
||||||
|
first_line_matches_content: ?[]const u8 = null,
|
||||||
|
|
||||||
|
include_files: []const u8 = "",
|
||||||
|
|
||||||
|
pub fn from_file_type(file_type: *const FileType) @This() {
|
||||||
|
return .{
|
||||||
|
.color = file_type.color,
|
||||||
|
.icon = file_type.icon,
|
||||||
|
.description = file_type.description,
|
||||||
|
.extensions = file_type.extensions,
|
||||||
|
.first_line_matches_prefix = if (file_type.first_line_matches) |flm| flm.prefix else null,
|
||||||
|
.first_line_matches_content = if (file_type.first_line_matches) |flm| flm.content else null,
|
||||||
|
.comment = file_type.comment,
|
||||||
|
.formatter = file_type.formatter,
|
||||||
|
.language_server = file_type.language_server,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const FileType = @import("syntax").FileType;
|
|
@ -8,6 +8,8 @@ const location_history = @import("location_history");
|
||||||
const project_manager = @import("project_manager");
|
const project_manager = @import("project_manager");
|
||||||
const log = @import("log");
|
const log = @import("log");
|
||||||
const shell = @import("shell");
|
const shell = @import("shell");
|
||||||
|
const syntax = @import("syntax");
|
||||||
|
const file_type_config = @import("file_type_config");
|
||||||
const builtin = @import("builtin");
|
const builtin = @import("builtin");
|
||||||
const build_options = @import("build_options");
|
const build_options = @import("build_options");
|
||||||
|
|
||||||
|
@ -263,6 +265,22 @@ fn open_style_config(self: *Self, Style: type) command.Result {
|
||||||
if (self.get_active_buffer()) |buffer| buffer.mark_not_ephemeral();
|
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 {
|
const cmds = struct {
|
||||||
pub const Target = Self;
|
pub const Target = Self;
|
||||||
const Ctx = command.Context;
|
const Ctx = command.Context;
|
||||||
|
@ -492,6 +510,49 @@ const cmds = struct {
|
||||||
}
|
}
|
||||||
pub const open_home_style_config_meta: Meta = .{ .description = "Edit home screen" };
|
pub const open_home_style_config_meta: Meta = .{ .description = "Edit home screen" };
|
||||||
|
|
||||||
|
pub fn change_file_type(_: *Self, _: Ctx) Result {
|
||||||
|
return tui.open_overlay(
|
||||||
|
@import("mode/overlay/file_type_palette.zig").Variant("set_file_type", "Select file type").Type,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
pub const change_file_type_meta: Meta = .{ .description = "Change file type" };
|
||||||
|
|
||||||
|
pub fn open_file_type_config(self: *Self, ctx: Ctx) Result {
|
||||||
|
var file_type_name: []const u8 = undefined;
|
||||||
|
if (!(ctx.args.match(.{tp.extract(&file_type_name)}) catch false))
|
||||||
|
return tui.open_overlay(
|
||||||
|
@import("mode/overlay/file_type_palette.zig").Variant("open_file_type_config", "Edit file type").Type,
|
||||||
|
);
|
||||||
|
|
||||||
|
const file_name = try get_file_type_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;
|
||||||
|
if (file) |f| {
|
||||||
|
f.close();
|
||||||
|
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);
|
||||||
|
|
||||||
|
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,
|
||||||
|
"conf",
|
||||||
|
}));
|
||||||
|
if (self.get_active_buffer()) |buffer| buffer.mark_not_ephemeral();
|
||||||
|
}
|
||||||
|
pub const open_file_type_config_meta: Meta = .{
|
||||||
|
.arguments = &.{.string},
|
||||||
|
.description = "Edit file type configuration",
|
||||||
|
};
|
||||||
|
|
||||||
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);
|
const args = try ctx.args.clone(self.allocator);
|
||||||
defer self.allocator.free(args.buf);
|
defer self.allocator.free(args.buf);
|
||||||
|
|
|
@ -6,9 +6,11 @@ const syntax = @import("syntax");
|
||||||
const Widget = @import("../../Widget.zig");
|
const Widget = @import("../../Widget.zig");
|
||||||
const tui = @import("../../tui.zig");
|
const tui = @import("../../tui.zig");
|
||||||
|
|
||||||
|
pub fn Variant(comptime command: []const u8, comptime label_: []const u8) type {
|
||||||
|
return struct {
|
||||||
pub const Type = @import("palette.zig").Create(@This());
|
pub const Type = @import("palette.zig").Create(@This());
|
||||||
|
|
||||||
pub const label = "Select file type";
|
pub const label = label_;
|
||||||
pub const name = " file type";
|
pub const name = " file type";
|
||||||
pub const description = "file type";
|
pub const description = "file type";
|
||||||
|
|
||||||
|
@ -126,5 +128,7 @@ fn select(menu: **Type.MenuState, button: *Type.ButtonState) void {
|
||||||
if (previous_file_type) |prev| if (std.mem.eql(u8, prev, name_))
|
if (previous_file_type) |prev| if (std.mem.eql(u8, prev, name_))
|
||||||
return;
|
return;
|
||||||
tp.self_pid().send(.{ "cmd", "exit_overlay_mode" }) catch |e| menu.*.opts.ctx.logger.err("file_type_palette", e);
|
tp.self_pid().send(.{ "cmd", "exit_overlay_mode" }) catch |e| menu.*.opts.ctx.logger.err("file_type_palette", e);
|
||||||
tp.self_pid().send(.{ "cmd", "set_file_type", .{name_} }) catch |e| menu.*.opts.ctx.logger.err("file_type_palette", e);
|
tp.self_pid().send(.{ "cmd", command, .{name_} }) catch |e| menu.*.opts.ctx.logger.err("file_type_palette", e);
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -917,11 +917,6 @@ const cmds = struct {
|
||||||
}
|
}
|
||||||
pub const change_theme_meta: Meta = .{ .description = "Change color theme" };
|
pub const change_theme_meta: Meta = .{ .description = "Change color theme" };
|
||||||
|
|
||||||
pub fn change_file_type(self: *Self, _: Ctx) Result {
|
|
||||||
return self.enter_overlay_mode(@import("mode/overlay/file_type_palette.zig").Type);
|
|
||||||
}
|
|
||||||
pub const change_file_type_meta: Meta = .{ .description = "Change file type" };
|
|
||||||
|
|
||||||
pub fn change_fontface(self: *Self, _: Ctx) Result {
|
pub fn change_fontface(self: *Self, _: Ctx) Result {
|
||||||
if (build_options.gui)
|
if (build_options.gui)
|
||||||
self.rdr_.get_fontfaces();
|
self.rdr_.get_fontfaces();
|
||||||
|
@ -1117,6 +1112,10 @@ pub fn mini_mode() ?*MiniMode {
|
||||||
return if (current().mini_mode_) |*p| p else null;
|
return if (current().mini_mode_) |*p| p else null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn open_overlay(mode: type) command.Result {
|
||||||
|
return current().enter_overlay_mode(mode);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn query_cache() *syntax.QueryCache {
|
pub fn query_cache() *syntax.QueryCache {
|
||||||
return current().query_cache_;
|
return current().query_cache_;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue