mirror of
https://github.com/ziglang/zig.git
synced 2025-12-16 11:13:08 +00:00
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.
This commit is contained in:
parent
84f3b3dff2
commit
3ad9cb8b47
@ -1108,6 +1108,25 @@ test "zig fmt: comment to disable/enable zig fmt first" {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "zig fmt: 'zig fmt: (off|on)' can be surrounded by arbitrary whitespace" {
|
||||||
|
try testTransform(
|
||||||
|
\\// Test trailing comma syntax
|
||||||
|
\\// zig fmt: off
|
||||||
|
\\
|
||||||
|
\\const struct_trailing_comma = struct { x: i32, y: i32, };
|
||||||
|
\\
|
||||||
|
\\// zig fmt: on
|
||||||
|
,
|
||||||
|
\\// Test trailing comma syntax
|
||||||
|
\\// zig fmt: off
|
||||||
|
\\
|
||||||
|
\\const struct_trailing_comma = struct { x: i32, y: i32, };
|
||||||
|
\\
|
||||||
|
\\// zig fmt: on
|
||||||
|
\\
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
test "zig fmt: comment to disable/enable zig fmt" {
|
test "zig fmt: comment to disable/enable zig fmt" {
|
||||||
try testTransform(
|
try testTransform(
|
||||||
\\const a = b;
|
\\const a = b;
|
||||||
|
|||||||
@ -2352,18 +2352,24 @@ fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize) Error!boo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try ais.writer().print("{s}\n", .{trimmed_comment});
|
index = 1 + (newline orelse end - 1);
|
||||||
index = 1 + (newline orelse return true);
|
|
||||||
|
|
||||||
if (ais.disabled_offset) |disabled_offset| {
|
const comment_content = mem.trimLeft(u8, trimmed_comment["//".len..], &std.ascii.spaces);
|
||||||
if (mem.eql(u8, trimmed_comment, "// zig fmt: on")) {
|
if (ais.disabled_offset != null and mem.eql(u8, comment_content, "zig fmt: on")) {
|
||||||
// write the source for which formatting was disabled directly
|
// Write the source for which formatting was disabled directly
|
||||||
// to the underlying writer, fixing up invaild whitespace
|
// to the underlying writer, fixing up invaild whitespace.
|
||||||
try writeFixingWhitespace(ais.underlying_writer, tree.source[disabled_offset..index]);
|
const disabled_source = tree.source[ais.disabled_offset.?..comment_start];
|
||||||
ais.disabled_offset = null;
|
try writeFixingWhitespace(ais.underlying_writer, disabled_source);
|
||||||
}
|
ais.disabled_offset = null;
|
||||||
} else if (mem.eql(u8, trimmed_comment, "// zig fmt: off")) {
|
// Write with the canonical single space.
|
||||||
|
try ais.writer().writeAll("// zig fmt: on\n");
|
||||||
|
} else if (ais.disabled_offset == null and mem.eql(u8, comment_content, "zig fmt: off")) {
|
||||||
|
// Write with the canonical single space.
|
||||||
|
try ais.writer().writeAll("// zig fmt: off\n");
|
||||||
ais.disabled_offset = index;
|
ais.disabled_offset = index;
|
||||||
|
} else {
|
||||||
|
// Write the comment minus trailing whitespace.
|
||||||
|
try ais.writer().print("{s}\n", .{trimmed_comment});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user