authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-11-14 19:50:55-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-11-15 00:26:43-05:00
log0c3bd0c3d16eb8c38593254ae54ef7fd22d8fa41
tree7609fdfb03f2380692258697fafc1b4e99d33132
parent65cddc5a1955303481252768b72c2f68200be359
signature Commit is signed but in an unrecognized format.

zig fmt: add --check flag

closes #1558 closes #1555

4 files changed, 49 insertions(+), 22 deletions(-)

src-self-hosted/arg.zig+1-1
......@@ -149,7 +149,7 @@ pub const Args = struct {
149149 }
150150
151151 // e.g. --help
152 pub fn present(self: *Args, name: []const u8) bool {
152 pub fn present(self: *const Args, name: []const u8) bool {
153153 return self.flags.contains(name);
154154 }
155155
src-self-hosted/main.zig+32-12
......@@ -514,17 +514,22 @@ const usage_fmt =
514514 \\usage: zig fmt [file]...
515515 \\
516516 \\ Formats the input files and modifies them in-place.
517 \\ Arguments can be files or directories, which are searched
518 \\ recursively.
517519 \\
518520 \\Options:
519521 \\ --help Print this help and exit
520522 \\ --color [auto|off|on] Enable or disable colored error messages
521 \\ --stdin Format code from stdin
523 \\ --stdin Format code from stdin; output to stdout
524 \\ --check List non-conforming files and exit with an error
525 \\ if the list is non-empty
522526 \\
523527 \\
524528;
525529
526530const args_fmt_spec = []Flag{
527531 Flag.Bool("--help"),
532 Flag.Bool("--check"),
528533 Flag.Option("--color", []const []const u8{
529534 "auto",
530535 "off",
......@@ -640,6 +645,11 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
640645 if (tree.errors.len != 0) {
641646 os.exit(1);
642647 }
648 if (flags.present("check")) {
649 const anything_changed = try std.zig.render(allocator, io.noop_out_stream, &tree);
650 const code = if (anything_changed) u8(1) else u8(0);
651 os.exit(code);
652 }
643653
644654 _ = try std.zig.render(allocator, stdout, &tree);
645655 return;
......@@ -711,9 +721,11 @@ async fn asyncFmtMain(
711721 .loop = loop,
712722 };
713723
724 const check_mode = flags.present("check");
725
714726 var group = event.Group(FmtError!void).init(loop);
715727 for (flags.positionals.toSliceConst()) |file_path| {
716 try group.call(fmtPath, &fmt, file_path);
728 try group.call(fmtPath, &fmt, file_path, check_mode);
717729 }
718730 try await (async group.wait() catch unreachable);
719731 if (fmt.any_error) {
......@@ -721,7 +733,7 @@ async fn asyncFmtMain(
721733 }
722734}
723735
724async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8) FmtError!void {
736async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtError!void {
725737 const file_path = try std.mem.dupe(fmt.loop.allocator, u8, file_path_ref);
726738 defer fmt.loop.allocator.free(file_path);
727739
......@@ -746,7 +758,7 @@ async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8) FmtError!void {
746758 while (try dir.next()) |entry| {
747759 if (entry.kind == std.os.Dir.Entry.Kind.Directory or mem.endsWith(u8, entry.name, ".zig")) {
748760 const full_path = try os.path.join(fmt.loop.allocator, file_path, entry.name);
749 try group.call(fmtPath, fmt, full_path);
761 try group.call(fmtPath, fmt, full_path, check_mode);
750762 }
751763 }
752764 return await (async group.wait() catch unreachable);
......@@ -779,14 +791,22 @@ async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8) FmtError!void {
779791 return;
780792 }
781793
782 // TODO make this evented
783 const baf = try io.BufferedAtomicFile.create(fmt.loop.allocator, file_path);
784 defer baf.destroy();
785
786 const anything_changed = try std.zig.render(fmt.loop.allocator, baf.stream(), &tree);
787 if (anything_changed) {
788 try stderr.print("{}\n", file_path);
789 try baf.finish();
794 if (check_mode) {
795 const anything_changed = try std.zig.render(fmt.loop.allocator, io.noop_out_stream, &tree);
796 if (anything_changed) {
797 try stderr.print("{}\n", file_path);
798 fmt.any_error = true;
799 }
800 } else {
801 // TODO make this evented
802 const baf = try io.BufferedAtomicFile.create(fmt.loop.allocator, file_path);
803 defer baf.destroy();
804
805 const anything_changed = try std.zig.render(fmt.loop.allocator, baf.stream(), &tree);
806 if (anything_changed) {
807 try stderr.print("{}\n", file_path);
808 try baf.finish();
809 }
790810 }
791811}
792812
std/io.zig+15-8
......@@ -203,20 +203,20 @@ pub fn OutStream(comptime WriteError: type) type {
203203
204204 writeFn: fn (self: *Self, bytes: []const u8) Error!void,
205205
206 pub fn print(self: *Self, comptime format: []const u8, args: ...) !void {
206 pub fn print(self: *Self, comptime format: []const u8, args: ...) Error!void {
207207 return std.fmt.format(self, Error, self.writeFn, format, args);
208208 }
209209
210 pub fn write(self: *Self, bytes: []const u8) !void {
210 pub fn write(self: *Self, bytes: []const u8) Error!void {
211211 return self.writeFn(self, bytes);
212212 }
213213
214 pub fn writeByte(self: *Self, byte: u8) !void {
214 pub fn writeByte(self: *Self, byte: u8) Error!void {
215215 const slice = (*[1]u8)(&byte)[0..];
216216 return self.writeFn(self, slice);
217217 }
218218
219 pub fn writeByteNTimes(self: *Self, byte: u8, n: usize) !void {
219 pub fn writeByteNTimes(self: *Self, byte: u8, n: usize) Error!void {
220220 const slice = (*[1]u8)(&byte)[0..];
221221 var i: usize = 0;
222222 while (i < n) : (i += 1) {
......@@ -225,23 +225,23 @@ pub fn OutStream(comptime WriteError: type) type {
225225 }
226226
227227 /// Write a native-endian integer.
228 pub fn writeIntNe(self: *Self, comptime T: type, value: T) !void {
228 pub fn writeIntNe(self: *Self, comptime T: type, value: T) Error!void {
229229 return self.writeInt(builtin.endian, T, value);
230230 }
231231
232 pub fn writeIntLe(self: *Self, comptime T: type, value: T) !void {
232 pub fn writeIntLe(self: *Self, comptime T: type, value: T) Error!void {
233233 var bytes: [@sizeOf(T)]u8 = undefined;
234234 mem.writeIntLE(T, &bytes, value);
235235 return self.writeFn(self, bytes);
236236 }
237237
238 pub fn writeIntBe(self: *Self, comptime T: type, value: T) !void {
238 pub fn writeIntBe(self: *Self, comptime T: type, value: T) Error!void {
239239 var bytes: [@sizeOf(T)]u8 = undefined;
240240 mem.writeIntBE(T, &bytes, value);
241241 return self.writeFn(self, bytes);
242242 }
243243
244 pub fn writeInt(self: *Self, endian: builtin.Endian, comptime T: type, value: T) !void {
244 pub fn writeInt(self: *Self, endian: builtin.Endian, comptime T: type, value: T) Error!void {
245245 var bytes: [@sizeOf(T)]u8 = undefined;
246246 mem.writeInt(bytes[0..], value, endian);
247247 return self.writeFn(self, bytes);
......@@ -249,6 +249,13 @@ pub fn OutStream(comptime WriteError: type) type {
249249 };
250250}
251251
252pub const noop_out_stream = &noop_out_stream_state;
253const NoopOutStreamError = error{};
254var noop_out_stream_state = OutStream(NoopOutStreamError){
255 .writeFn = noop_out_stream_write,
256};
257fn noop_out_stream_write(self: *OutStream(NoopOutStreamError), bytes: []const u8) error{}!void {}
258
252259pub fn writeFile(path: []const u8, data: []const u8) !void {
253260 var file = try File.openWrite(path);
254261 defer file.close();
std/zig/render.zig+1-1
......@@ -133,7 +133,7 @@ fn renderRoot(
133133 }
134134}
135135
136fn renderExtraNewline(tree: *ast.Tree, stream: var, start_col: *usize, node: *ast.Node) !void {
136fn renderExtraNewline(tree: *ast.Tree, stream: var, start_col: *usize, node: *ast.Node) @typeOf(stream).Child.Error!void {
137137 const first_token = node.firstToken();
138138 var prev_token = first_token;
139139 while (tree.tokens.at(prev_token - 1).id == Token.Id.DocComment) {