authorgravatar for r00ster91@proton.meWooster <r00ster91@proton.me> 2023-04-18 00:57:13+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-04-18 18:33:05-07:00
log2b7334c3a9c873a637f8d601ad9077d783058863
treebd3b9029cf4b0d1d4aea05b84213913f70a0c770
parentd2a799c65a7a65df5a66771f41417b6df24834b8

zig fmt: omit extra whitespace after last comment before EOF


2 files changed, 20 insertions(+), 15 deletions(-)

lib/std/zig/parser_test.zig+13-11
......@@ -18,6 +18,19 @@ test "zig fmt: transform old for loop syntax to new" {
1818 );
1919}
2020
21test "zig fmt: remove extra whitespace at start and end of file with comment between" {
22 try testTransform(
23 \\
24 \\
25 \\// hello
26 \\
27 \\
28 ,
29 \\// hello
30 \\
31 );
32}
33
2134test "zig fmt: tuple struct" {
2235 try testCanonical(
2336 \\const T = struct {
......@@ -527,17 +540,6 @@ test "zig fmt: allow empty line before commment at start of block" {
527540 );
528541}
529542
530test "zig fmt: allow empty line before commment at start of block" {
531 try testCanonical(
532 \\test {
533 \\
534 \\ // foo
535 \\ const x = 42;
536 \\}
537 \\
538 );
539}
540
541543test "zig fmt: trailing comma in fn parameter list" {
542544 try testCanonical(
543545 \\pub fn f(
lib/std/zig/render.zig+7-4
......@@ -2512,7 +2512,7 @@ const Space = enum {
25122512 /// In either case, a newline will be inserted afterwards.
25132513 semicolon,
25142514 /// Skip rendering whitespace and comments. If this is used, the caller
2515 /// *must* handle handle whitespace and comments manually.
2515 /// *must* handle whitespace and comments manually.
25162516 skip,
25172517};
25182518
......@@ -2702,7 +2702,7 @@ fn renderIdentifierContents(writer: anytype, bytes: []const u8) !void {
27022702 }
27032703 },
27042704 0x00...('\\' - 1), ('\\' + 1)...0x7f => {
2705 const buf = [1]u8{@intCast(u8, byte)};
2705 const buf = [1]u8{byte};
27062706 try std.fmt.format(writer, "{}", .{std.zig.fmtEscapes(&buf)});
27072707 pos += 1;
27082708 },
......@@ -2782,7 +2782,7 @@ fn renderComments(ais: *Ais, tree: Ast, start: usize, end: usize) Error!bool {
27822782 const comment_content = mem.trimLeft(u8, trimmed_comment["//".len..], &std.ascii.whitespace);
27832783 if (ais.disabled_offset != null and mem.eql(u8, comment_content, "zig fmt: on")) {
27842784 // Write the source for which formatting was disabled directly
2785 // to the underlying writer, fixing up invaild whitespace.
2785 // to the underlying writer, fixing up invalid whitespace.
27862786 const disabled_source = tree.source[ais.disabled_offset.?..comment_start];
27872787 try writeFixingWhitespace(ais.underlying_writer, disabled_source);
27882788 // Write with the canonical single space.
......@@ -2799,7 +2799,10 @@ fn renderComments(ais: *Ais, tree: Ast, start: usize, end: usize) Error!bool {
27992799 }
28002800
28012801 if (index != start and mem.containsAtLeast(u8, tree.source[index - 1 .. end], 2, "\n")) {
2802 try ais.insertNewline();
2802 // Don't leave any whitespace at the end of the file
2803 if (end != tree.source.len) {
2804 try ais.insertNewline();
2805 }
28032806 }
28042807
28052808 return index != start;