authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-03-02 21:36:25+01:00
committergravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-03-03 01:06:09+01:00
log3ad9cb8b473820ec5ea11d85aa72e8ddc83cfa03
treec3400ebccf6fa1a3b8ccd34a0a98a0fb2338496e
parent84f3b3dff2cc8f380a60e920cb1ca76e779571c6

zig fmt: allow and trim whitespace around zig fmt: (off|on)

Currently `// zig fmt: off` does not work as there are two spaces after the `//` instead of one. This can cause confusion, so allow arbitrary whitespace before the `zig fmt: (off|on)` in the comment but trim this whitespace to the canonical single space in the output.

2 files changed, 36 insertions(+), 11 deletions(-)

lib/std/zig/parser_test.zig+19
......@@ -1108,6 +1108,25 @@ test "zig fmt: comment to disable/enable zig fmt first" {
11081108 );
11091109}
11101110
1111test "zig fmt: 'zig fmt: (off|on)' can be surrounded by arbitrary whitespace" {
1112 try testTransform(
1113 \\// Test trailing comma syntax
1114 \\// zig fmt: off
1115 \\
1116 \\const struct_trailing_comma = struct { x: i32, y: i32, };
1117 \\
1118 \\// zig fmt: on
1119 ,
1120 \\// Test trailing comma syntax
1121 \\// zig fmt: off
1122 \\
1123 \\const struct_trailing_comma = struct { x: i32, y: i32, };
1124 \\
1125 \\// zig fmt: on
1126 \\
1127 );
1128}
1129
11111130test "zig fmt: comment to disable/enable zig fmt" {
11121131 try testTransform(
11131132 \\const a = b;
lib/std/zig/render.zig+17-11
......@@ -2352,18 +2352,24 @@ fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize) Error!boo
23522352 }
23532353 }
23542354
2355 try ais.writer().print("{s}\n", .{trimmed_comment});
2356 index = 1 + (newline orelse return true);
2357
2358 if (ais.disabled_offset) |disabled_offset| {
2359 if (mem.eql(u8, trimmed_comment, "// zig fmt: on")) {
2360 // write the source for which formatting was disabled directly
2361 // to the underlying writer, fixing up invaild whitespace
2362 try writeFixingWhitespace(ais.underlying_writer, tree.source[disabled_offset..index]);
2363 ais.disabled_offset = null;
2364 }
2365 } else if (mem.eql(u8, trimmed_comment, "// zig fmt: off")) {
2355 index = 1 + (newline orelse end - 1);
2356
2357 const comment_content = mem.trimLeft(u8, trimmed_comment["//".len..], &std.ascii.spaces);
2358 if (ais.disabled_offset != null and mem.eql(u8, comment_content, "zig fmt: on")) {
2359 // Write the source for which formatting was disabled directly
2360 // to the underlying writer, fixing up invaild whitespace.
2361 const disabled_source = tree.source[ais.disabled_offset.?..comment_start];
2362 try writeFixingWhitespace(ais.underlying_writer, disabled_source);
2363 ais.disabled_offset = null;
2364 // Write with the canonical single space.
2365 try ais.writer().writeAll("// zig fmt: on\n");
2366 } else if (ais.disabled_offset == null and mem.eql(u8, comment_content, "zig fmt: off")) {
2367 // Write with the canonical single space.
2368 try ais.writer().writeAll("// zig fmt: off\n");
23662369 ais.disabled_offset = index;
2370 } else {
2371 // Write the comment minus trailing whitespace.
2372 try ais.writer().print("{s}\n", .{trimmed_comment});
23672373 }
23682374 }
23692375