zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 1de63ad79331aec9f53a7a9d95069fd0ca5f66f4 (tree)
parent 7f64f7c9259ba5f67adb386ba55473d4b2b74607
Author: Cody Tapscott <topolarity@tapscott.me>
Date:   Wed, 23 Mar 2022 23:05:33 -0700

zig fmt: Add `--exclude` argument to skip dir/file

This change adds a "--exclude" parameter to zig format, which can be
used to make sure that it does not process certain files or folders
when recursively walking a directory.

To do this, we simply piggy-back on the existing "seen" logic in zig
fmt and mark these files/folders as seen before processing begins.

Diffstat:
Mci/zinc/linux_test.sh | 2+-
Msrc/main.zig | 20++++++++++++++++++++
2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/ci/zinc/linux_test.sh b/ci/zinc/linux_test.sh @@ -45,7 +45,7 @@ cd $WORKSPACE # Look for non-conforming code formatting. # Formatting errors can be fixed by running `zig fmt` on the files printed here. -$ZIG fmt --check . +$ZIG fmt --check . --exclude test/compile_errors/ # Build stage2 standalone so that we can test stage2 against stage2 compiler-rt. $ZIG build -p stage2 -Denable-llvm -Duse-zig-libcxx diff --git a/src/main.zig b/src/main.zig @@ -3792,6 +3792,7 @@ pub const usage_fmt = \\ --check List non-conforming files and exit with an error \\ if the list is non-empty \\ --ast-check Run zig ast-check on every file + \\ --exclude [file] Exclude file or directory from formatting \\ \\ ; @@ -3815,6 +3816,8 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void var check_ast_flag: bool = false; var input_files = ArrayList([]const u8).init(gpa); defer input_files.deinit(); + var excluded_files = ArrayList([]const u8).init(gpa); + defer excluded_files.deinit(); { var i: usize = 0; @@ -3840,6 +3843,13 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void check_flag = true; } else if (mem.eql(u8, arg, "--ast-check")) { check_ast_flag = true; + } else if (mem.eql(u8, arg, "--exclude")) { + if (i + 1 >= args.len) { + fatal("expected parameter after --exclude", .{}); + } + i += 1; + const next_arg = args[i]; + try excluded_files.append(next_arg); } else { fatal("unrecognized parameter: '{s}'", .{arg}); } @@ -3940,6 +3950,16 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void defer fmt.seen.deinit(); defer fmt.out_buffer.deinit(); + // Mark any excluded files/directories as already seen, + // so that they are skipped later during actual processing + for (excluded_files.items) |file_path| { + var dir = try fs.cwd().openDir(file_path, .{}); + defer dir.close(); + + const stat = try dir.stat(); + try fmt.seen.put(stat.inode, {}); + } + for (input_files.items) |file_path| { try fmtPath(&fmt, file_path, check_flag, fs.cwd(), file_path); }