authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-06-04 12:15:02-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-06-04 12:15:02-04:00
logd21a1922eb5d76b9b0d0611eaeb42c91f83234ab
tree497da8a656b975bf083a3d3acff63610e4a657ae
parent8dfa66fee30fa6fa9a7e22780d71be2253eabb21

support `zig fmt: off` and `zig fmt: on` between top level decls

closes #1030 closes #1033

5 files changed, 70 insertions(+), 2 deletions(-)

std/special/compiler_rt/udivmoddi4_test.zig+2
......@@ -1,3 +1,5 @@
1// Disable formatting to avoid unnecessary source repository bloat.
2// zig fmt: off
13const __udivmoddi4 = @import("udivmoddi4.zig").__udivmoddi4;
24const assert = @import("std").debug.assert;
35
std/special/compiler_rt/udivmodti4_test.zig+2
......@@ -1,3 +1,5 @@
1// Disable formatting to avoid unnecessary source repository bloat.
2// zig fmt: off
13const __udivmodti4 = @import("udivmodti4.zig").__udivmodti4;
24const assert = @import("std").debug.assert;
35
std/zig/parser_test.zig+26
......@@ -1,3 +1,29 @@
1test "zig fmt: comment to disable/enable zig fmt first" {
2 try testCanonical(
3 \\// Test trailing comma syntax
4 \\// zig fmt: off
5 \\
6 \\const struct_trailing_comma = struct { x: i32, y: i32, };
7 );
8}
9
10test "zig fmt: comment to disable/enable zig fmt" {
11 try testTransform(
12 \\const a = b;
13 \\// zig fmt: off
14 \\const c = d;
15 \\// zig fmt: on
16 \\const e = f;
17 ,
18 \\const a = b;
19 \\// zig fmt: off
20 \\const c = d;
21 \\// zig fmt: on
22 \\const e = f;
23 \\
24 );
25}
26
127test "zig fmt: pointer of unknown length" {
228 try testCanonical(
329 \\fn foo(ptr: [*]u8) void {}
std/zig/render.zig+39-2
......@@ -82,8 +82,45 @@ fn renderRoot(
8282
8383 var start_col: usize = 0;
8484 var it = tree.root_node.decls.iterator(0);
85 while (it.next()) |decl| {
86 try renderTopLevelDecl(allocator, stream, tree, 0, &start_col, decl.*);
85 while (true) {
86 var decl = (it.next() ?? return).*;
87 // look for zig fmt: off comment
88 var start_token_index = decl.firstToken();
89 zig_fmt_loop: while (start_token_index != 0) {
90 start_token_index -= 1;
91 const start_token = tree.tokens.at(start_token_index);
92 switch (start_token.id) {
93 Token.Id.LineComment => {},
94 Token.Id.DocComment => continue,
95 else => break,
96 }
97 if (mem.eql(u8, mem.trim(u8, tree.tokenSlicePtr(start_token)[2..], " "), "zig fmt: off")) {
98 var end_token_index = start_token_index;
99 while (true) {
100 end_token_index += 1;
101 const end_token = tree.tokens.at(end_token_index);
102 switch (end_token.id) {
103 Token.Id.LineComment => {},
104 Token.Id.Eof => {
105 const start = tree.tokens.at(start_token_index + 1).start;
106 try stream.write(tree.source[start..]);
107 return;
108 },
109 else => continue,
110 }
111 if (mem.eql(u8, mem.trim(u8, tree.tokenSlicePtr(end_token)[2..], " "), "zig fmt: on")) {
112 const start = tree.tokens.at(start_token_index + 1).start;
113 try stream.print("{}\n", tree.source[start..end_token.end]);
114 while (tree.tokens.at(decl.firstToken()).start < end_token.end) {
115 decl = (it.next() ?? return).*;
116 }
117 break :zig_fmt_loop;
118 }
119 }
120 }
121 }
122
123 try renderTopLevelDecl(allocator, stream, tree, 0, &start_col, decl);
87124 if (it.peek()) |next_decl| {
88125 try renderExtraNewline(tree, stream, &start_col, next_decl.*);
89126 }
test/cases/syntax.zig+1
......@@ -1,4 +1,5 @@
11// Test trailing comma syntax
2// zig fmt: off
23
34const struct_trailing_comma = struct { x: i32, y: i32, };
45const struct_no_comma = struct { x: i32, y: i32 };