zig fmt ignores "zig fmt: off" directive for whitespace fixes

This commit is contained in:
Andrew Kelley 2019-07-05 15:09:22 -04:00
parent 4f43a4b30f
commit 9471f16c79
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
2 changed files with 15 additions and 3 deletions

View File

@ -9,10 +9,13 @@ test "zig fmt: change use to usingnamespace" {
}
test "zig fmt: whitespace fixes" {
try testTransform("test \"\" {\r\n\tconst hi = x;\r\n}",
try testTransform("test \"\" {\r\n\tconst hi = x;\r\n}\n// zig fmt: off\ntest \"\"{\r\n\tconst a = b;}\r\n",
\\test "" {
\\ const hi = x;
\\}
\\// zig fmt: off
\\test ""{
\\ const a = b;}
\\
);
}

View File

@ -108,14 +108,15 @@ fn renderRoot(
Token.Id.LineComment => {},
Token.Id.Eof => {
const start = tree.tokens.at(start_token_index + 1).start;
try stream.write(tree.source[start..]);
try copyFixingWhitespace(stream, 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]);
try copyFixingWhitespace(stream, tree.source[start..end_token.end]);
try stream.writeByte('\n');
while (tree.tokens.at(decl.firstToken()).start < end_token.end) {
decl = (it.next() orelse return).*;
}
@ -2121,3 +2122,11 @@ const FindByteOutStream = struct {
};
}
};
fn copyFixingWhitespace(stream: var, slice: []const u8) @typeOf(stream).Child.Error!void {
for (slice) |byte| switch (byte) {
'\t' => try stream.write(" "),
'\r' => {},
else => try stream.writeByte(byte),
};
}