aboutsummaryrefslogtreecommitdiff
path: root/src/main.zig
diff options
context:
space:
mode:
authorCody Tapscott <topolarity@tapscott.me>2022-03-23 23:05:33 -0700
committerCody Tapscott <topolarity@tapscott.me>2022-03-25 12:27:46 -0700
commit1de63ad79331aec9f53a7a9d95069fd0ca5f66f4 (patch)
tree5644ee454239ef76cb51fea58d8678673f1a6ead /src/main.zig
parent7f64f7c9259ba5f67adb386ba55473d4b2b74607 (diff)
downloadzig-1de63ad79331aec9f53a7a9d95069fd0ca5f66f4.tar.gz
zig-1de63ad79331aec9f53a7a9d95069fd0ca5f66f4.zip
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 (limited to 'src/main.zig')
-rw-r--r--src/main.zig20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/main.zig b/src/main.zig
index 115f3748b6..a647216b05 100644
--- 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);
}