TUI editor and editor backend written in Zig

make sure to write the whole input to zig format

reykjalin.org d4bedc5f b6841660

verified
+21 -3
+10 -1
src/libfn/Editor.zig
··· 164 164 /// Saves the text to the current location based on the `filename` field. 165 165 pub fn saveFile(self: *Editor, gpa: std.mem.Allocator) !void { 166 166 if (self.language) |*l| format_text: { 167 - const formatted = l.formatter.format(gpa, self.text.items) catch break :format_text; 167 + const formatted = l.formatter.format(gpa, self.text.items) catch { 168 + std.log.debug("failed to format buffer", .{}); 169 + break :format_text; 170 + }; 168 171 defer gpa.free(formatted); 172 + 173 + // If, for whatever reason, the formatter doesn't return anything, we likely do not want to 174 + // actually empty the text buffer, so we do nothing in that case. 175 + if (std.mem.eql(u8, formatted, "")) break :format_text; 176 + 177 + std.log.debug("formatted source:\n{s}", .{formatted}); 169 178 170 179 self.text.clearRetainingCapacity(); 171 180 try self.text.ensureTotalCapacity(gpa, formatted.len);
+11 -2
src/libfn/formatters/ZigFmt.zig
··· 14 14 15 15 pub fn format(f: *Formatter, gpa: std.mem.Allocator, input: []const u8) anyerror![]const u8 { 16 16 _ = f; 17 + const log = std.log.scoped(.zig_fmt); 18 + 19 + log.debug("formatting with `zig fmt`:\n{s}", .{input}); 17 20 18 21 var child = std.process.Child.init(&.{ "zig", "fmt", "--stdin" }, gpa); 19 22 child.stdin_behavior = .Pipe; 20 23 child.stdout_behavior = .Pipe; 21 24 child.stderr_behavior = .Pipe; 25 + errdefer _ = child.kill() catch void; 22 26 23 27 try child.spawn(); 24 28 ··· 26 30 27 31 var buffer: [4096]u8 = undefined; 28 32 var w = stdin.writer(&buffer); 29 - _ = try w.interface.write(input); 33 + _ = try w.interface.writeAll(input); 30 34 try w.interface.flush(); 31 35 32 - child.stdin.?.close(); 36 + stdin.close(); 33 37 child.stdin = null; 34 38 35 39 var out: std.ArrayList(u8) = .empty; ··· 40 44 // FIXME: maxInt(usize) is probably excessive here? 41 45 try child.collectOutput(gpa, &out, &err, std.math.maxInt(usize)); 42 46 47 + log.debug("stdout:\n{s}", .{out.items}); 48 + log.debug("stderr:\n{s}", .{err.items}); 49 + 43 50 const result = try child.wait(); 51 + 52 + log.debug("result: {}", .{result}); 44 53 45 54 if (result.Exited == 0) return out.toOwnedSlice(gpa); 46 55