Skip to content

Commit

Permalink
support zig fmt: off and zig fmt: on between top level decls
Browse files Browse the repository at this point in the history
closes #1030
closes #1033
  • Loading branch information
andrewrk committed Jun 4, 2018
1 parent 8dfa66f commit d21a192
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
2 changes: 2 additions & 0 deletions std/special/compiler_rt/udivmoddi4_test.zig
@@ -1,3 +1,5 @@
// Disable formatting to avoid unnecessary source repository bloat.
// zig fmt: off
const __udivmoddi4 = @import("udivmoddi4.zig").__udivmoddi4;
const assert = @import("std").debug.assert;

Expand Down
2 changes: 2 additions & 0 deletions std/special/compiler_rt/udivmodti4_test.zig
@@ -1,3 +1,5 @@
// Disable formatting to avoid unnecessary source repository bloat.
// zig fmt: off
const __udivmodti4 = @import("udivmodti4.zig").__udivmodti4;
const assert = @import("std").debug.assert;

Expand Down
26 changes: 26 additions & 0 deletions std/zig/parser_test.zig
@@ -1,3 +1,29 @@
test "zig fmt: comment to disable/enable zig fmt first" {
try testCanonical(
\\// Test trailing comma syntax
\\// zig fmt: off
\\
\\const struct_trailing_comma = struct { x: i32, y: i32, };
);
}

test "zig fmt: comment to disable/enable zig fmt" {
try testTransform(
\\const a = b;
\\// zig fmt: off
\\const c = d;
\\// zig fmt: on
\\const e = f;
,
\\const a = b;
\\// zig fmt: off
\\const c = d;
\\// zig fmt: on
\\const e = f;
\\
);
}

test "zig fmt: pointer of unknown length" {
try testCanonical(
\\fn foo(ptr: [*]u8) void {}
Expand Down
41 changes: 39 additions & 2 deletions std/zig/render.zig
Expand Up @@ -82,8 +82,45 @@ fn renderRoot(

var start_col: usize = 0;
var it = tree.root_node.decls.iterator(0);
while (it.next()) |decl| {
try renderTopLevelDecl(allocator, stream, tree, 0, &start_col, decl.*);
while (true) {
var decl = (it.next() ?? return).*;
// look for zig fmt: off comment
var start_token_index = decl.firstToken();
zig_fmt_loop: while (start_token_index != 0) {
start_token_index -= 1;
const start_token = tree.tokens.at(start_token_index);
switch (start_token.id) {
Token.Id.LineComment => {},
Token.Id.DocComment => continue,
else => break,
}
if (mem.eql(u8, mem.trim(u8, tree.tokenSlicePtr(start_token)[2..], " "), "zig fmt: off")) {
var end_token_index = start_token_index;
while (true) {
end_token_index += 1;
const end_token = tree.tokens.at(end_token_index);
switch (end_token.id) {
Token.Id.LineComment => {},
Token.Id.Eof => {
const start = tree.tokens.at(start_token_index + 1).start;
try stream.write(tree.source[start..]);
return;
},
else => continue,
}
if (mem.eql(u8, mem.trim(u8, tree.tokenSlicePtr(end_token)[2..], " "), "zig fmt: on")) {
const start = tree.tokens.at(start_token_index + 1).start;
try stream.print("{}\n", tree.source[start..end_token.end]);
while (tree.tokens.at(decl.firstToken()).start < end_token.end) {
decl = (it.next() ?? return).*;
}
break :zig_fmt_loop;
}
}
}
}

try renderTopLevelDecl(allocator, stream, tree, 0, &start_col, decl);
if (it.peek()) |next_decl| {
try renderExtraNewline(tree, stream, &start_col, next_decl.*);
}
Expand Down
1 change: 1 addition & 0 deletions test/cases/syntax.zig
@@ -1,4 +1,5 @@
// Test trailing comma syntax
// zig fmt: off

const struct_trailing_comma = struct { x: i32, y: i32, };
const struct_no_comma = struct { x: i32, y: i32 };
Expand Down

0 comments on commit d21a192

Please sign in to comment.