| ... | ... | @@ -546,6 +546,7 @@ const Fmt = struct { |
| 546 | 546 | any_error: bool, |
| 547 | 547 | color: Color, |
| 548 | 548 | gpa: *Allocator, |
| 549 | out_buffer: std.ArrayList(u8), |
| 549 | 550 | |
| 550 | 551 | const SeenMap = std.AutoHashMap(fs.File.INode, void); |
| 551 | 552 | }; |
| ... | ... | @@ -641,7 +642,10 @@ pub fn cmdFmt(gpa: *Allocator, args: []const []const u8) !void { |
| 641 | 642 | .seen = Fmt.SeenMap.init(gpa), |
| 642 | 643 | .any_error = false, |
| 643 | 644 | .color = color, |
| 645 | .out_buffer = std.ArrayList(u8).init(gpa), |
| 644 | 646 | }; |
| 647 | defer fmt.seen.deinit(); |
| 648 | defer fmt.out_buffer.deinit(); |
| 645 | 649 | |
| 646 | 650 | for (input_files.span()) |file_path| { |
| 647 | 651 | // Get the real path here to avoid Windows failing on relative file paths with . or .. in them. |
| ... | ... | @@ -767,14 +771,19 @@ fn fmtPathFile( |
| 767 | 771 | fmt.any_error = true; |
| 768 | 772 | } |
| 769 | 773 | } else { |
| 770 | | const baf = try io.BufferedAtomicFile.create(fmt.gpa, dir, sub_path, .{ .mode = stat.mode }); |
| 771 | | defer baf.destroy(); |
| 772 | | |
| 773 | | const anything_changed = try std.zig.render(fmt.gpa, baf.stream(), tree); |
| 774 | | if (anything_changed) { |
| 775 | | std.debug.warn("{}\n", .{file_path}); |
| 776 | | try baf.finish(); |
| 777 | | } |
| 774 | // As a heuristic, we make enough capacity for the same as the input source. |
| 775 | try fmt.out_buffer.ensureCapacity(source_code.len); |
| 776 | fmt.out_buffer.items.len = 0; |
| 777 | const anything_changed = try std.zig.render(fmt.gpa, fmt.out_buffer.writer(), tree); |
| 778 | if (!anything_changed) |
| 779 | return; // Good thing we didn't waste any file system access on this. |
| 780 | |
| 781 | var af = try dir.atomicFile(sub_path, .{ .mode = stat.mode }); |
| 782 | defer af.deinit(); |
| 783 | |
| 784 | try af.file.writeAll(fmt.out_buffer.items); |
| 785 | try af.finish(); |
| 786 | std.debug.warn("{}\n", .{file_path}); |
| 778 | 787 | } |
| 779 | 788 | } |
| 780 | 789 | |