authorgravatar for codroid@gmail.comhryx <codroid@gmail.com> 2019-03-31 21:31:36-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-01 11:31:00-04:00
logc76d51de975a4e2e8b549d37161f1e0056384e0b
tree6c1496532c591e93884ec0c6fc3c41f181f90ae2
parentd6455008830eff433a14c5eff6094f2f5ba9991d

zig fmt: Allow one-line for loops


2 files changed, 49 insertions(+), 18 deletions(-)

std/zig/parser_test.zig+32
...@@ -1737,6 +1737,10 @@ test "zig fmt: switch" {...@@ -1737,6 +1737,10 @@ test "zig fmt: switch" {
1737test "zig fmt: while" {1737test "zig fmt: while" {
1738 try testCanonical(1738 try testCanonical(
1739 \\test "while" {1739 \\test "while" {
1740 \\ while (10 < 1) unreachable;
1741 \\
1742 \\ while (10 < 1) unreachable else unreachable;
1743 \\
1740 \\ while (10 < 1) {1744 \\ while (10 < 1) {
1741 \\ unreachable;1745 \\ unreachable;
1742 \\ }1746 \\ }
...@@ -1803,10 +1807,27 @@ test "zig fmt: while" {...@@ -1803,10 +1807,27 @@ test "zig fmt: while" {
1803test "zig fmt: for" {1807test "zig fmt: for" {
1804 try testCanonical(1808 try testCanonical(
1805 \\test "for" {1809 \\test "for" {
1810 \\ for (a) continue;
1811 \\
1812 \\ for (a)
1813 \\ continue;
1814 \\
1815 \\ for (a) {
1816 \\ continue;
1817 \\ }
1818 \\
1806 \\ for (a) |v| {1819 \\ for (a) |v| {
1807 \\ continue;1820 \\ continue;
1808 \\ }1821 \\ }
1809 \\1822 \\
1823 \\ for (a) |v| continue;
1824 \\
1825 \\ for (a) |v| continue else return;
1826 \\
1827 \\ for (a) |v| {
1828 \\ continue;
1829 \\ } else return;
1830 \\
1810 \\ for (a) |v|1831 \\ for (a) |v|
1811 \\ continue;1832 \\ continue;
1812 \\1833 \\
...@@ -1820,6 +1841,17 @@ test "zig fmt: for" {...@@ -1820,6 +1841,17 @@ test "zig fmt: for" {
1820 \\ for (a) |v, i|1841 \\ for (a) |v, i|
1821 \\ continue;1842 \\ continue;
1822 \\1843 \\
1844 \\ for (a) |b| switch (b) {
1845 \\ c => {},
1846 \\ d => {},
1847 \\ };
1848 \\
1849 \\ for (a) |b|
1850 \\ switch (b) {
1851 \\ c => {},
1852 \\ d => {},
1853 \\ };
1854 \\
1823 \\ const res = for (a) |v, i| {1855 \\ const res = for (a) |v, i| {
1824 \\ break v;1856 \\ break v;
1825 \\ } else {1857 \\ } else {
std/zig/render.zig+17-18
...@@ -1444,18 +1444,24 @@ fn renderExpression(...@@ -1444,18 +1444,24 @@ fn renderExpression(
1444 try renderExpression(allocator, stream, tree, indent, start_col, for_node.array_expr, Space.None);1444 try renderExpression(allocator, stream, tree, indent, start_col, for_node.array_expr, Space.None);
14451445
1446 const rparen = tree.nextToken(for_node.array_expr.lastToken());1446 const rparen = tree.nextToken(for_node.array_expr.lastToken());
1447 const rparen_space = if (for_node.payload != null or1447
1448 for_node.body.id == ast.Node.Id.Block) Space.Space else Space.Newline;1448 const has_payload = for_node.payload != null;
1449 try renderToken(tree, stream, rparen, indent, start_col, rparen_space); // )1449 const body_is_block = for_node.body.id == ast.Node.Id.Block;
1450 const src_one_line_to_body = !body_is_block and tree.tokensOnSameLine(rparen, for_node.body.firstToken());
1451 const body_indent = if (!body_is_block and !src_one_line_to_body) indent + indent_delta else indent;
1452
1453 const rparen_space = if (has_payload or body_is_block or src_one_line_to_body) Space.Space else Space.Newline;
1454 try renderToken(tree, stream, rparen, body_indent, start_col, rparen_space); // )
14501455
1451 if (for_node.payload) |payload| {1456 if (for_node.payload) |payload| {
1452 const payload_space = if (for_node.body.id == ast.Node.Id.Block) Space.Space else Space.Newline;1457 const payload_space = if (body_is_block or src_one_line_to_body) Space.Space else Space.Newline;
1453 try renderExpression(allocator, stream, tree, indent, start_col, payload, payload_space);1458 try renderExpression(allocator, stream, tree, body_indent, start_col, payload, payload_space); // |x|
1454 }1459 }
14551460
1456 const body_space = blk: {1461 const body_space = blk: {
1457 if (for_node.@"else" != null) {1462 if (for_node.@"else") |@"else"| {
1458 if (for_node.body.id == ast.Node.Id.Block) {1463 const src_one_line_to_else = tree.tokensOnSameLine(for_node.body.lastToken(), @"else".firstToken());
1464 if (body_is_block or src_one_line_to_else) {
1459 break :blk Space.Space;1465 break :blk Space.Space;
1460 } else {1466 } else {
1461 break :blk Space.Newline;1467 break :blk Space.Newline;
...@@ -1464,19 +1470,12 @@ fn renderExpression(...@@ -1464,19 +1470,12 @@ fn renderExpression(
1464 break :blk space;1470 break :blk space;
1465 }1471 }
1466 };1472 };
1467 if (for_node.body.id == ast.Node.Id.Block) {
1468 try renderExpression(allocator, stream, tree, indent, start_col, for_node.body, body_space);
1469 } else {
1470 try stream.writeByteNTimes(' ', indent + indent_delta);
1471 try renderExpression(allocator, stream, tree, indent, start_col, for_node.body, body_space);
1472 }
14731473
1474 if (for_node.@"else") |@"else"| {1474 if (!body_is_block and !src_one_line_to_body) try stream.writeByteNTimes(' ', body_indent);
1475 if (for_node.body.id != ast.Node.Id.Block) {1475 try renderExpression(allocator, stream, tree, body_indent, start_col, for_node.body, body_space);
1476 try stream.writeByteNTimes(' ', indent);
1477 }
14781476
1479 return renderExpression(allocator, stream, tree, indent, start_col, &@"else".base, space);1477 if (for_node.@"else") |@"else"| {
1478 return renderExpression(allocator, stream, tree, body_indent, start_col, &@"else".base, space);
1480 }1479 }
1481 },1480 },
14821481