feat: add enable_format_on_save configuration option

This commit is contained in:
CJ van den Berg 2024-08-27 21:39:23 +02:00
parent 3fe1e1ea26
commit d11ea9dde4
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 20 additions and 2 deletions

View file

@ -15,6 +15,7 @@ highlight_current_line_gutter: bool = true,
show_whitespace: bool = false,
animation_min_lag: usize = 0, //milliseconds
animation_max_lag: usize = 150, //milliseconds
enable_format_on_save: bool = false,
top_bar: []const u8 = "",
bottom_bar: []const u8 = "mode file log selection diagnostics linenumber",

View file

@ -259,6 +259,8 @@ pub const Editor = struct {
diag_info: usize = 0,
diag_hints: usize = 0,
need_save_after_filter: bool = false,
const StyleCache = std.AutoHashMap(u32, ?Widget.Theme.Token);
const Context = command.Context;
@ -3000,6 +3002,15 @@ pub const Editor = struct {
}
pub fn save_file(self: *Self, _: Context) Result {
if (tui.current().config.enable_format_on_save) if (self.get_formatter()) |_| {
self.need_save_after_filter = true;
const primary = self.get_primary();
const sel = primary.selection;
primary.selection = null;
defer primary.selection = sel;
try self.format(.{});
return;
};
try self.save();
}
@ -3518,19 +3529,24 @@ pub const Editor = struct {
self.get_primary().selection = sel;
}
fn get_formatter(self: *Self) ?[]const []const u8 {
if (self.syntax) |syn| if (syn.file_type.formatter) |fmtr| if (fmtr.len > 0) return fmtr;
return null;
}
pub fn format(self: *Self, ctx: Context) Result {
if (ctx.args.buf.len > 0 and try ctx.args.match(.{ tp.string, tp.more })) {
try self.filter_cmd(ctx.args);
return;
}
if (self.syntax) |syn| if (syn.file_type.formatter) |fmtr| if (fmtr.len > 0) {
if (self.get_formatter()) |fmtr| {
var args = std.ArrayList(u8).init(self.a);
const writer = args.writer();
try cbor.writeArrayHeader(writer, fmtr.len);
for (fmtr) |arg| try cbor.writeValue(writer, arg);
try self.filter_cmd(.{ .buf = args.items });
return;
};
}
return tp.exit("no formatter");
}
@ -3624,6 +3640,7 @@ pub const Editor = struct {
self.reset_syntax();
self.clamp();
self.need_render();
if (self.need_save_after_filter) try self.save();
}
fn filter_deinit(self: *Self) void {