feat: add enable_format_on_save configuration option
This commit is contained in:
parent
3fe1e1ea26
commit
d11ea9dde4
2 changed files with 20 additions and 2 deletions
|
@ -15,6 +15,7 @@ highlight_current_line_gutter: bool = true,
|
||||||
show_whitespace: bool = false,
|
show_whitespace: bool = false,
|
||||||
animation_min_lag: usize = 0, //milliseconds
|
animation_min_lag: usize = 0, //milliseconds
|
||||||
animation_max_lag: usize = 150, //milliseconds
|
animation_max_lag: usize = 150, //milliseconds
|
||||||
|
enable_format_on_save: bool = false,
|
||||||
|
|
||||||
top_bar: []const u8 = "",
|
top_bar: []const u8 = "",
|
||||||
bottom_bar: []const u8 = "mode file log selection diagnostics linenumber",
|
bottom_bar: []const u8 = "mode file log selection diagnostics linenumber",
|
||||||
|
|
|
@ -259,6 +259,8 @@ pub const Editor = struct {
|
||||||
diag_info: usize = 0,
|
diag_info: usize = 0,
|
||||||
diag_hints: usize = 0,
|
diag_hints: usize = 0,
|
||||||
|
|
||||||
|
need_save_after_filter: bool = false,
|
||||||
|
|
||||||
const StyleCache = std.AutoHashMap(u32, ?Widget.Theme.Token);
|
const StyleCache = std.AutoHashMap(u32, ?Widget.Theme.Token);
|
||||||
|
|
||||||
const Context = command.Context;
|
const Context = command.Context;
|
||||||
|
@ -3000,6 +3002,15 @@ pub const Editor = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn save_file(self: *Self, _: Context) Result {
|
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();
|
try self.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3518,19 +3529,24 @@ pub const Editor = struct {
|
||||||
self.get_primary().selection = sel;
|
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 {
|
pub fn format(self: *Self, ctx: Context) Result {
|
||||||
if (ctx.args.buf.len > 0 and try ctx.args.match(.{ tp.string, tp.more })) {
|
if (ctx.args.buf.len > 0 and try ctx.args.match(.{ tp.string, tp.more })) {
|
||||||
try self.filter_cmd(ctx.args);
|
try self.filter_cmd(ctx.args);
|
||||||
return;
|
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);
|
var args = std.ArrayList(u8).init(self.a);
|
||||||
const writer = args.writer();
|
const writer = args.writer();
|
||||||
try cbor.writeArrayHeader(writer, fmtr.len);
|
try cbor.writeArrayHeader(writer, fmtr.len);
|
||||||
for (fmtr) |arg| try cbor.writeValue(writer, arg);
|
for (fmtr) |arg| try cbor.writeValue(writer, arg);
|
||||||
try self.filter_cmd(.{ .buf = args.items });
|
try self.filter_cmd(.{ .buf = args.items });
|
||||||
return;
|
return;
|
||||||
};
|
}
|
||||||
return tp.exit("no formatter");
|
return tp.exit("no formatter");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3624,6 +3640,7 @@ pub const Editor = struct {
|
||||||
self.reset_syntax();
|
self.reset_syntax();
|
||||||
self.clamp();
|
self.clamp();
|
||||||
self.need_render();
|
self.need_render();
|
||||||
|
if (self.need_save_after_filter) try self.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn filter_deinit(self: *Self) void {
|
fn filter_deinit(self: *Self) void {
|
||||||
|
|
Loading…
Add table
Reference in a new issue