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