authorgravatar for lachlan@lakebythewoods.xyzLachlan Easton <lachlan@lakebythewoods.xyz> 2020-08-30 10:35:18+10:00
committergravatar for lachlan@lakebythewoods.xyzLachlan Easton <lachlan@lakebythewoods.xyz> 2020-09-09 21:54:42+10:00
log283d441c19d5bafa01a7df24db277a6b08a86c00
tree56830b0ac654af46a161323ff180648dbaaf12b0
parent7d487a41627658bfda37f1f06f8e453c2b576b9c

zig fmt: fix #3978, fix #2748


2 files changed, 65 insertions(+), 3 deletions(-)

lib/std/zig/parser_test.zig+53
...@@ -3321,6 +3321,59 @@ test "zig fmt: Don't add extra newline after if" {...@@ -3321,6 +3321,59 @@ test "zig fmt: Don't add extra newline after if" {
3321 );3321 );
3322}3322}
33233323
3324test "zig fmt: comments in ternary ifs" {
3325 try testCanonical(
3326 \\const x = if (true) {
3327 \\ 1;
3328 \\} else if (false)
3329 \\ // Comment
3330 \\ 0;
3331 \\const y = if (true)
3332 \\ // Comment
3333 \\ 1
3334 \\else
3335 \\ 0;
3336 \\
3337 \\pub extern "c" fn printf(format: [*:0]const u8, ...) c_int;
3338 \\
3339 );
3340}
3341
3342test "zig fmt: test comments in field access chain" {
3343 try testCanonical(
3344 \\pub const str = struct {
3345 \\ pub const Thing = more.more //
3346 \\ .more() //
3347 \\ .more().more() //
3348 \\ .more() //
3349 \\ // .more() //
3350 \\ .more() //
3351 \\ .more();
3352 \\ data: Data,
3353 \\};
3354 \\
3355 \\pub const str = struct {
3356 \\ pub const Thing = more.more //
3357 \\ .more() //
3358 \\ // .more() //
3359 \\ // .more() //
3360 \\ // .more() //
3361 \\ .more() //
3362 \\ .more();
3363 \\ data: Data,
3364 \\};
3365 \\
3366 \\pub const str = struct {
3367 \\ pub const Thing = more //
3368 \\ .more //
3369 \\ .more() //
3370 \\ .more();
3371 \\ data: Data,
3372 \\};
3373 \\
3374 );
3375}
3376
3324const std = @import("std");3377const std = @import("std");
3325const mem = std.mem;3378const mem = std.mem;
3326const warn = std.debug.warn;3379const warn = std.debug.warn;
lib/std/zig/render.zig+12-3
...@@ -522,8 +522,12 @@ fn renderExpression(...@@ -522,8 +522,12 @@ fn renderExpression(
522 break :blk if (loc.line == 0) op_space else Space.Newline;522 break :blk if (loc.line == 0) op_space else Space.Newline;
523 };523 };
524524
525 try renderToken(tree, ais, infix_op_node.op_token, after_op_space);525 {
526 ais.pushIndentOneShot();526 try ais.pushIndent();
527 defer ais.popIndent();
528 try renderToken(tree, ais, infix_op_node.op_token, after_op_space);
529 }
530 try ais.pushIndentOneShot();
527 return renderExpression(allocator, ais, tree, infix_op_node.rhs, space);531 return renderExpression(allocator, ais, tree, infix_op_node.rhs, space);
528 },532 },
529533
...@@ -1873,7 +1877,12 @@ fn renderExpression(...@@ -1873,7 +1877,12 @@ fn renderExpression(
18731877
1874 if (src_has_newline) {1878 if (src_has_newline) {
1875 const after_rparen_space = if (if_node.payload == null) Space.Newline else Space.Space;1879 const after_rparen_space = if (if_node.payload == null) Space.Newline else Space.Space;
1876 try renderToken(tree, ais, rparen, after_rparen_space); // )1880
1881 {
1882 try ais.pushIndent();
1883 defer ais.popIndent();
1884 try renderToken(tree, ais, rparen, after_rparen_space); // )
1885 }
18771886
1878 if (if_node.payload) |payload| {1887 if (if_node.payload) |payload| {
1879 try renderExpression(allocator, ais, tree, payload, Space.Newline);1888 try renderExpression(allocator, ais, tree, payload, Space.Newline);