diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig index 109d9e9620..ee18fc6c9a 100644 --- a/lib/std/zig/parser_test.zig +++ b/lib/std/zig/parser_test.zig @@ -1308,108 +1308,108 @@ test "zig fmt: 2nd arg multiline string" { ); } -//test "zig fmt: 2nd arg multiline string many args" { -// try testCanonical( -// \\comptime { -// \\ cases.addAsm("hello world linux x86_64", -// \\ \\.text -// \\ , "Hello, world!\n", "Hello, world!\n"); -// \\} -// \\ -// ); -//} -// -//test "zig fmt: final arg multiline string" { -// try testCanonical( -// \\comptime { -// \\ cases.addAsm("hello world linux x86_64", "Hello, world!\n", -// \\ \\.text -// \\ ); -// \\} -// \\ -// ); -//} -// -//test "zig fmt: if condition wraps" { -// try testTransform( -// \\comptime { -// \\ if (cond and -// \\ cond) { -// \\ return x; -// \\ } -// \\ while (cond and -// \\ cond) { -// \\ return x; -// \\ } -// \\ if (a == b and -// \\ c) { -// \\ a = b; -// \\ } -// \\ while (a == b and -// \\ c) { -// \\ a = b; -// \\ } -// \\ if ((cond and -// \\ cond)) { -// \\ return x; -// \\ } -// \\ while ((cond and -// \\ cond)) { -// \\ return x; -// \\ } -// \\ var a = if (a) |*f| x: { -// \\ break :x &a.b; -// \\ } else |err| err; -// \\ var a = if (cond and -// \\ cond) |*f| -// \\ x: { -// \\ break :x &a.b; -// \\ } else |err| err; -// \\} -// , -// \\comptime { -// \\ if (cond and -// \\ cond) -// \\ { -// \\ return x; -// \\ } -// \\ while (cond and -// \\ cond) -// \\ { -// \\ return x; -// \\ } -// \\ if (a == b and -// \\ c) -// \\ { -// \\ a = b; -// \\ } -// \\ while (a == b and -// \\ c) -// \\ { -// \\ a = b; -// \\ } -// \\ if ((cond and -// \\ cond)) -// \\ { -// \\ return x; -// \\ } -// \\ while ((cond and -// \\ cond)) -// \\ { -// \\ return x; -// \\ } -// \\ var a = if (a) |*f| x: { -// \\ break :x &a.b; -// \\ } else |err| err; -// \\ var a = if (cond and -// \\ cond) |*f| -// \\ x: { -// \\ break :x &a.b; -// \\ } else |err| err; -// \\} -// \\ -// ); -//} +test "zig fmt: 2nd arg multiline string many args" { + try testCanonical( + \\comptime { + \\ cases.addAsm("hello world linux x86_64", + \\ \\.text + \\ , "Hello, world!\n", "Hello, world!\n"); + \\} + \\ + ); +} + +test "zig fmt: final arg multiline string" { + try testCanonical( + \\comptime { + \\ cases.addAsm("hello world linux x86_64", "Hello, world!\n", + \\ \\.text + \\ ); + \\} + \\ + ); +} + +test "zig fmt: if condition wraps" { + try testTransform( + \\comptime { + \\ if (cond and + \\ cond) { + \\ return x; + \\ } + \\ while (cond and + \\ cond) { + \\ return x; + \\ } + \\ if (a == b and + \\ c) { + \\ a = b; + \\ } + \\ while (a == b and + \\ c) { + \\ a = b; + \\ } + \\ if ((cond and + \\ cond)) { + \\ return x; + \\ } + \\ while ((cond and + \\ cond)) { + \\ return x; + \\ } + \\ var a = if (a) |*f| x: { + \\ break :x &a.b; + \\ } else |err| err; + \\ var a = if (cond and + \\ cond) |*f| + \\ x: { + \\ break :x &a.b; + \\ } else |err| err; + \\} + , + \\comptime { + \\ if (cond and + \\ cond) + \\ { + \\ return x; + \\ } + \\ while (cond and + \\ cond) + \\ { + \\ return x; + \\ } + \\ if (a == b and + \\ c) + \\ { + \\ a = b; + \\ } + \\ while (a == b and + \\ c) + \\ { + \\ a = b; + \\ } + \\ if ((cond and + \\ cond)) + \\ { + \\ return x; + \\ } + \\ while ((cond and + \\ cond)) + \\ { + \\ return x; + \\ } + \\ var a = if (a) |*f| x: { + \\ break :x &a.b; + \\ } else |err| err; + \\ var a = if (cond and + \\ cond) |*f| + \\ x: { + \\ break :x &a.b; + \\ } else |err| err; + \\} + \\ + ); +} // //test "zig fmt: if condition has line break but must not wrap" { // try testCanonical( diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig index 105be0c9c4..06b5052fd5 100644 --- a/lib/std/zig/render.zig +++ b/lib/std/zig/render.zig @@ -499,10 +499,9 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac }, .grouped_expression => { - ais.pushIndentNextLine(); try renderToken(ais, tree, main_tokens[node], .none); // lparen + ais.pushIndentOneShot(); try renderExpression(ais, tree, datas[node].lhs, .none); - ais.popIndent(); return renderToken(ais, tree, datas[node].rhs, space); // rparen }, @@ -981,13 +980,13 @@ fn renderWhile(ais: *Ais, tree: ast.Tree, while_node: ast.full.While, space: Spa } try renderToken(ais, tree, while_node.ast.while_token, .space); // if - try renderToken(ais, tree, while_node.ast.while_token + 1, .none); // ( + try renderToken(ais, tree, while_node.ast.while_token + 1, .none); // lparen try renderExpression(ais, tree, while_node.ast.cond_expr, .none); // condition const then_tag = node_tags[while_node.ast.then_expr]; if (nodeIsBlock(then_tag) and !nodeIsIf(then_tag)) { if (while_node.payload_token) |payload_token| { - try renderToken(ais, tree, payload_token - 2, .space); // ) + try renderToken(ais, tree, payload_token - 2, .space); // rparen try renderToken(ais, tree, payload_token - 1, .none); // | const ident = blk: { if (token_tags[payload_token] == .asterisk) { @@ -1007,10 +1006,14 @@ fn renderWhile(ais: *Ais, tree: ast.Tree, while_node: ast.full.While, space: Spa break :blk ident + 1; } }; - try renderToken(ais, tree, pipe, .space); // | + const cond_has_newline = !tree.tokensOnSameLine(while_node.ast.while_token, pipe); + const brace_space: Space = if (cond_has_newline) .newline else .space; + try renderToken(ais, tree, pipe, brace_space); // | } else { const rparen = tree.lastToken(while_node.ast.cond_expr) + 1; - try renderToken(ais, tree, rparen, .space); // ) + const cond_has_newline = !tree.tokensOnSameLine(while_node.ast.while_token, rparen); + const brace_space: Space = if (cond_has_newline) .newline else .space; + try renderToken(ais, tree, rparen, brace_space); // rparen } if (while_node.ast.cont_expr != 0) { const rparen = tree.lastToken(while_node.ast.cont_expr) + 1; @@ -1040,7 +1043,7 @@ fn renderWhile(ais: *Ais, tree: ast.Tree, while_node: ast.full.While, space: Spa if (src_has_newline) { if (while_node.payload_token) |payload_token| { - try renderToken(ais, tree, payload_token - 2, .space); // ) + try renderToken(ais, tree, payload_token - 2, .space); // rparen try renderToken(ais, tree, payload_token - 1, .none); // | const ident = blk: { if (token_tags[payload_token] == .asterisk) { @@ -1063,7 +1066,7 @@ fn renderWhile(ais: *Ais, tree: ast.Tree, while_node: ast.full.While, space: Spa try renderToken(ais, tree, pipe, .newline); // | } else { ais.pushIndent(); - try renderToken(ais, tree, rparen, .newline); // ) + try renderToken(ais, tree, rparen, .newline); // rparen ais.popIndent(); } if (while_node.ast.cont_expr != 0) {