From 60218a06b72eb4ca62f41b8b7d621f6f73dfb0ce Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Thu, 6 Jun 2024 21:12:26 +0200 Subject: [PATCH] feat: use language specific formatters defined in flow-syntax/file_types --- src/tui/editor.zig | 20 +++++++++++++++++++- src/tui/mode/input/flow.zig | 2 +- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 58bc335..8da4156 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -3444,6 +3444,22 @@ pub const Editor = struct { self.get_primary().selection = sel; } + pub fn format(self: *Self, ctx: command.Context) tp.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) { + var args = std.ArrayList(u8).init(self.a); + const writer = args.writer(); + cbor.writeArrayHeader(writer, fmtr.len) catch |e| return tp.exit_error(e); + for (fmtr) |arg| cbor.writeValue(writer, arg) catch |e| return tp.exit_error(e); + try self.filter_cmd(.{ .buf = args.items }); + return; + }; + return tp.exit("no formatter"); + } + pub fn filter(self: *Self, ctx: command.Context) tp.result { if (!try ctx.args.match(.{ tp.string, tp.more })) return tp.exit_error(error.InvalidArgument); @@ -3473,7 +3489,9 @@ pub const Editor = struct { }; errdefer self.filter_deinit(); const state = &self.filter.?; - self.logger.print("filter: start", .{}); + var buf: [1024]u8 = undefined; + const json = cmd.to_json(&buf) catch |e| return tp.exit_error(e); + self.logger.print("filter: start {s}", .{json}); var sp = tp.subprocess.init(self.a, cmd, "filter", .Pipe) catch |e| return tp.exit_error(e); defer { sp.close() catch {}; diff --git a/src/tui/mode/input/flow.zig b/src/tui/mode/input/flow.zig index 1ad55fd..62ebcd5 100644 --- a/src/tui/mode/input/flow.zig +++ b/src/tui/mode/input/flow.zig @@ -161,7 +161,7 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result { 'D' => self.cmd("dupe_up", .{}), // 'B' => self.cmd("select_word_left", .{}), // 'F' => self.cmd("select_word_right", .{}), - 'F' => self.cmd("filter", command.fmt(.{ "zig", "fmt", "--stdin" })), + 'F' => self.cmd("format", .{}), 'S' => self.cmd("filter", command.fmt(.{ "sort", "-u" })), 'V' => self.cmd("paste", .{}), 'I' => self.cmd("add_cursors_to_line_ends", .{}),