| ... | ... | @@ -26,16 +26,13 @@ const Fmt = struct { |
| 26 | 26 | color: Color, |
| 27 | 27 | gpa: Allocator, |
| 28 | 28 | arena: Allocator, |
| 29 | | out_buffer: std.ArrayList(u8), |
| 29 | out_buffer: std.ArrayListUnmanaged(u8), |
| 30 | stdout: *std.io.BufferedWriter, |
| 30 | 31 | |
| 31 | 32 | const SeenMap = std.AutoHashMap(fs.File.INode, void); |
| 32 | 33 | }; |
| 33 | 34 | |
| 34 | | pub fn run( |
| 35 | | gpa: Allocator, |
| 36 | | arena: Allocator, |
| 37 | | args: []const []const u8, |
| 38 | | ) !void { |
| 35 | pub fn run(gpa: Allocator, arena: Allocator, args: []const []const u8) !void { |
| 39 | 36 | var color: Color = .auto; |
| 40 | 37 | var stdin_flag = false; |
| 41 | 38 | var check_flag = false; |
| ... | ... | @@ -52,8 +49,7 @@ pub fn run( |
| 52 | 49 | const arg = args[i]; |
| 53 | 50 | if (mem.startsWith(u8, arg, "-")) { |
| 54 | 51 | if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) { |
| 55 | | const stdout = std.io.getStdOut().writer(); |
| 56 | | try stdout.writeAll(usage_fmt); |
| 52 | try std.io.getStdOut().writeAll(usage_fmt); |
| 57 | 53 | return process.cleanExit(); |
| 58 | 54 | } else if (mem.eql(u8, arg, "--color")) { |
| 59 | 55 | if (i + 1 >= args.len) { |
| ... | ... | @@ -138,8 +134,11 @@ pub fn run( |
| 138 | 134 | try std.zig.printAstErrorsToStderr(gpa, tree, "<stdin>", color); |
| 139 | 135 | process.exit(2); |
| 140 | 136 | } |
| 141 | | const formatted = try tree.render(gpa); |
| 142 | | defer gpa.free(formatted); |
| 137 | var aw: std.io.AllocatingWriter = undefined; |
| 138 | const bw = aw.init(gpa); |
| 139 | defer aw.deinit(); |
| 140 | try tree.render(gpa, bw, .{}); |
| 141 | const formatted = aw.getWritten(); |
| 143 | 142 | |
| 144 | 143 | if (check_flag) { |
| 145 | 144 | const code: u8 = @intFromBool(mem.eql(u8, formatted, source_code)); |
| ... | ... | @@ -153,6 +152,12 @@ pub fn run( |
| 153 | 152 | fatal("expected at least one source file argument", .{}); |
| 154 | 153 | } |
| 155 | 154 | |
| 155 | var stdout_buffer: [4096]u8 = undefined; |
| 156 | var stdout: std.io.BufferedWriter = .{ |
| 157 | .buffer = &stdout_buffer, |
| 158 | .unbuffered_writer = std.io.getStdOut().writer(), |
| 159 | }; |
| 160 | |
| 156 | 161 | var fmt: Fmt = .{ |
| 157 | 162 | .gpa = gpa, |
| 158 | 163 | .arena = arena, |
| ... | ... | @@ -161,10 +166,11 @@ pub fn run( |
| 161 | 166 | .check_ast = check_ast_flag, |
| 162 | 167 | .force_zon = force_zon, |
| 163 | 168 | .color = color, |
| 164 | | .out_buffer = std.ArrayList(u8).init(gpa), |
| 169 | .out_buffer = .empty, |
| 170 | .stdout = &stdout, |
| 165 | 171 | }; |
| 166 | 172 | defer fmt.seen.deinit(); |
| 167 | | defer fmt.out_buffer.deinit(); |
| 173 | defer fmt.out_buffer.deinit(gpa); |
| 168 | 174 | |
| 169 | 175 | // Mark any excluded files/directories as already seen, |
| 170 | 176 | // so that they are skipped later during actual processing |
| ... | ... | @@ -322,15 +328,19 @@ fn fmtPathFile( |
| 322 | 328 | |
| 323 | 329 | // As a heuristic, we make enough capacity for the same as the input source. |
| 324 | 330 | fmt.out_buffer.shrinkRetainingCapacity(0); |
| 325 | | try fmt.out_buffer.ensureTotalCapacity(source_code.len); |
| 331 | try fmt.out_buffer.ensureTotalCapacity(gpa, source_code.len); |
| 326 | 332 | |
| 327 | | try tree.renderToArrayList(&fmt.out_buffer, .{}); |
| 333 | { |
| 334 | var aw: std.io.AllocatingWriter = undefined; |
| 335 | const bw = aw.fromArrayList(gpa, &fmt.out_buffer); |
| 336 | defer fmt.out_buffer = aw.toArrayList(); |
| 337 | try tree.render(gpa, bw, .{}); |
| 338 | } |
| 328 | 339 | if (mem.eql(u8, fmt.out_buffer.items, source_code)) |
| 329 | 340 | return; |
| 330 | 341 | |
| 331 | 342 | if (check_mode) { |
| 332 | | const stdout = std.io.getStdOut().writer(); |
| 333 | | try stdout.print("{s}\n", .{file_path}); |
| 343 | try fmt.stdout.print("{s}\n", .{file_path}); |
| 334 | 344 | fmt.any_error = true; |
| 335 | 345 | } else { |
| 336 | 346 | var af = try dir.atomicFile(sub_path, .{ .mode = stat.mode }); |
| ... | ... | @@ -338,8 +348,7 @@ fn fmtPathFile( |
| 338 | 348 | |
| 339 | 349 | try af.file.writeAll(fmt.out_buffer.items); |
| 340 | 350 | try af.finish(); |
| 341 | | const stdout = std.io.getStdOut().writer(); |
| 342 | | try stdout.print("{s}\n", .{file_path}); |
| 351 | try fmt.stdout.print("{s}\n", .{file_path}); |
| 343 | 352 | } |
| 344 | 353 | } |
| 345 | 354 | |
| ... | ... | @@ -350,3 +359,12 @@ const process = std.process; |
| 350 | 359 | const Allocator = std.mem.Allocator; |
| 351 | 360 | const Color = std.zig.Color; |
| 352 | 361 | const fatal = std.process.fatal; |
| 362 | |
| 363 | /// Provided for debugging/testing purposes; unused by the compiler. |
| 364 | pub fn main() !void { |
| 365 | const gpa = std.heap.smp_allocator; |
| 366 | var arena_instance = std.heap.ArenaAllocator.init(gpa); |
| 367 | const arena = arena_instance.allocator(); |
| 368 | const args = try process.argsAlloc(arena); |
| 369 | return run(gpa, arena, args[1..]); |
| 370 | } |