aboutsummaryrefslogtreecommitdiff
path: root/src-self-hosted
diff options
context:
space:
mode:
authorMarc Tiehuis <marctiehuis@gmail.com>2018-07-15 00:07:47 +1200
committerAndrew Kelley <superjoe30@gmail.com>2018-07-14 11:43:35 -0400
commitbf441ed2448fdf4c5f12282ca448a62c5b71d26e (patch)
tree37038662888b883a42e6ae36680cad866da2cbab /src-self-hosted
parented3181f029809f8cd608f2c20b60d1b7c6e353e3 (diff)
downloadzig-bf441ed2448fdf4c5f12282ca448a62c5b71d26e.tar.gz
zig-bf441ed2448fdf4c5f12282ca448a62c5b71d26e.zip
Add --stdin option to zig fmt
Diffstat (limited to 'src-self-hosted')
-rw-r--r--src-self-hosted/main.zig45
1 files changed, 40 insertions, 5 deletions
diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig
index 058459a2d8..972aaae9ac 100644
--- a/src-self-hosted/main.zig
+++ b/src-self-hosted/main.zig
@@ -527,6 +527,7 @@ const usage_fmt =
\\Options:
\\ --help Print this help and exit
\\ --color [auto|off|on] Enable or disable colored error messages
+ \\ --stdin Format code from stdin
\\
\\
;
@@ -538,6 +539,7 @@ const args_fmt_spec = []Flag{
"off",
"on",
}),
+ Flag.Bool("--stdin"),
};
const Fmt = struct {
@@ -579,11 +581,6 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
os.exit(0);
}
- if (flags.positionals.len == 0) {
- try stderr.write("expected at least one source file argument\n");
- os.exit(1);
- }
-
const color = blk: {
if (flags.single("color")) |color_flag| {
if (mem.eql(u8, color_flag, "auto")) {
@@ -598,6 +595,44 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
}
};
+ if (flags.present("stdin")) {
+ if (flags.positionals.len != 0) {
+ try stderr.write("cannot use --stdin with positional arguments\n");
+ os.exit(1);
+ }
+
+ var stdin_file = try io.getStdIn();
+ var stdin = io.FileInStream.init(&stdin_file);
+
+ const source_code = try stdin.stream.readAllAlloc(allocator, @maxValue(usize));
+ defer allocator.free(source_code);
+
+ var tree = std.zig.parse(allocator, source_code) catch |err| {
+ try stderr.print("error parsing stdin: {}\n", err);
+ os.exit(1);
+ };
+ defer tree.deinit();
+
+ var error_it = tree.errors.iterator(0);
+ while (error_it.next()) |parse_error| {
+ const msg = try errmsg.createFromParseError(allocator, parse_error, &tree, "<stdin>");
+ defer allocator.destroy(msg);
+
+ try errmsg.printToFile(&stderr_file, msg, color);
+ }
+ if (tree.errors.len != 0) {
+ os.exit(1);
+ }
+
+ _ = try std.zig.render(allocator, stdout, &tree);
+ return;
+ }
+
+ if (flags.positionals.len == 0) {
+ try stderr.write("expected at least one source file argument\n");
+ os.exit(1);
+ }
+
var fmt = Fmt{
.seen = std.HashMap([]const u8, void, mem.hash_slice_u8, mem.eql_slice_u8).init(allocator),
.queue = std.LinkedList([]const u8).init(),