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" {
33213321 );
33223322}
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
33243377const std = @import("std");
33253378const mem = std.mem;
33263379const warn = std.debug.warn;
lib/std/zig/render.zig+12-3
......@@ -522,8 +522,12 @@ fn renderExpression(
522522 break :blk if (loc.line == 0) op_space else Space.Newline;
523523 };
524524
525 try renderToken(tree, ais, infix_op_node.op_token, after_op_space);
526 ais.pushIndentOneShot();
525 {
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();
527531 return renderExpression(allocator, ais, tree, infix_op_node.rhs, space);
528532 },
529533
......@@ -1873,7 +1877,12 @@ fn renderExpression(
18731877
18741878 if (src_has_newline) {
18751879 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
18781887 if (if_node.payload) |payload| {
18791888 try renderExpression(allocator, ais, tree, payload, Space.Newline);