Skip to content

Commit

Permalink
zig fmt: fix #3978, fix #2748
Browse files Browse the repository at this point in the history
  • Loading branch information
LakeByTheWoods committed Sep 9, 2020
1 parent 7d487a4 commit 283d441
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
53 changes: 53 additions & 0 deletions lib/std/zig/parser_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3321,6 +3321,59 @@ test "zig fmt: Don't add extra newline after if" {
);
}

test "zig fmt: comments in ternary ifs" {
try testCanonical(
\\const x = if (true) {
\\ 1;
\\} else if (false)
\\ // Comment
\\ 0;
\\const y = if (true)
\\ // Comment
\\ 1
\\else
\\ 0;
\\
\\pub extern "c" fn printf(format: [*:0]const u8, ...) c_int;
\\
);
}

test "zig fmt: test comments in field access chain" {
try testCanonical(
\\pub const str = struct {
\\ pub const Thing = more.more //
\\ .more() //
\\ .more().more() //
\\ .more() //
\\ // .more() //
\\ .more() //
\\ .more();
\\ data: Data,
\\};
\\
\\pub const str = struct {
\\ pub const Thing = more.more //
\\ .more() //
\\ // .more() //
\\ // .more() //
\\ // .more() //
\\ .more() //
\\ .more();
\\ data: Data,
\\};
\\
\\pub const str = struct {
\\ pub const Thing = more //
\\ .more //
\\ .more() //
\\ .more();
\\ data: Data,
\\};
\\
);
}

const std = @import("std");
const mem = std.mem;
const warn = std.debug.warn;
Expand Down
15 changes: 12 additions & 3 deletions lib/std/zig/render.zig
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,12 @@ fn renderExpression(
break :blk if (loc.line == 0) op_space else Space.Newline;
};

try renderToken(tree, ais, infix_op_node.op_token, after_op_space);
ais.pushIndentOneShot();
{
try ais.pushIndent();
defer ais.popIndent();
try renderToken(tree, ais, infix_op_node.op_token, after_op_space);
}
try ais.pushIndentOneShot();
return renderExpression(allocator, ais, tree, infix_op_node.rhs, space);
},

Expand Down Expand Up @@ -1873,7 +1877,12 @@ fn renderExpression(

if (src_has_newline) {
const after_rparen_space = if (if_node.payload == null) Space.Newline else Space.Space;
try renderToken(tree, ais, rparen, after_rparen_space); // )

{
try ais.pushIndent();
defer ais.popIndent();
try renderToken(tree, ais, rparen, after_rparen_space); // )
}

if (if_node.payload) |payload| {
try renderExpression(allocator, ais, tree, payload, Space.Newline);
Expand Down

0 comments on commit 283d441

Please sign in to comment.