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" {...@@ -18,6 +18,19 @@ test "zig fmt: transform old for loop syntax to new" {
18 );18 );
19}19}
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
21test "zig fmt: tuple struct" {34test "zig fmt: tuple struct" {
22 try testCanonical(35 try testCanonical(
23 \\const T = struct {36 \\const T = struct {
...@@ -527,17 +540,6 @@ test "zig fmt: allow empty line before commment at start of block" {...@@ -527,17 +540,6 @@ test "zig fmt: allow empty line before commment at start of block" {
527 );540 );
528}541}
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
541test "zig fmt: trailing comma in fn parameter list" {543test "zig fmt: trailing comma in fn parameter list" {
542 try testCanonical(544 try testCanonical(
543 \\pub fn f(545 \\pub fn f(
lib/std/zig/render.zig+7-4
...@@ -2512,7 +2512,7 @@ const Space = enum {...@@ -2512,7 +2512,7 @@ const Space = enum {
2512 /// In either case, a newline will be inserted afterwards.2512 /// In either case, a newline will be inserted afterwards.
2513 semicolon,2513 semicolon,
2514 /// Skip rendering whitespace and comments. If this is used, the caller2514 /// 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.
2516 skip,2516 skip,
2517};2517};
25182518
...@@ -2702,7 +2702,7 @@ fn renderIdentifierContents(writer: anytype, bytes: []const u8) !void {...@@ -2702,7 +2702,7 @@ fn renderIdentifierContents(writer: anytype, bytes: []const u8) !void {
2702 }2702 }
2703 },2703 },
2704 0x00...('\\' - 1), ('\\' + 1)...0x7f => {2704 0x00...('\\' - 1), ('\\' + 1)...0x7f => {
2705 const buf = [1]u8{@intCast(u8, byte)};2705 const buf = [1]u8{byte};
2706 try std.fmt.format(writer, "{}", .{std.zig.fmtEscapes(&buf)});2706 try std.fmt.format(writer, "{}", .{std.zig.fmtEscapes(&buf)});
2707 pos += 1;2707 pos += 1;
2708 },2708 },
...@@ -2782,7 +2782,7 @@ fn renderComments(ais: *Ais, tree: Ast, start: usize, end: usize) Error!bool {...@@ -2782,7 +2782,7 @@ fn renderComments(ais: *Ais, tree: Ast, start: usize, end: usize) Error!bool {
2782 const comment_content = mem.trimLeft(u8, trimmed_comment["//".len..], &std.ascii.whitespace);2782 const comment_content = mem.trimLeft(u8, trimmed_comment["//".len..], &std.ascii.whitespace);
2783 if (ais.disabled_offset != null and mem.eql(u8, comment_content, "zig fmt: on")) {2783 if (ais.disabled_offset != null and mem.eql(u8, comment_content, "zig fmt: on")) {
2784 // Write the source for which formatting was disabled directly2784 // 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.
2786 const disabled_source = tree.source[ais.disabled_offset.?..comment_start];2786 const disabled_source = tree.source[ais.disabled_offset.?..comment_start];
2787 try writeFixingWhitespace(ais.underlying_writer, disabled_source);2787 try writeFixingWhitespace(ais.underlying_writer, disabled_source);
2788 // Write with the canonical single space.2788 // Write with the canonical single space.
...@@ -2799,7 +2799,10 @@ fn renderComments(ais: *Ais, tree: Ast, start: usize, end: usize) Error!bool {...@@ -2799,7 +2799,10 @@ fn renderComments(ais: *Ais, tree: Ast, start: usize, end: usize) Error!bool {
2799 }2799 }
28002800
2801 if (index != start and mem.containsAtLeast(u8, tree.source[index - 1 .. end], 2, "\n")) {2801 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 }
2803 }2806 }
28042807
2805 return index != start;2808 return index != start;