authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-22 16:21:19-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-22 16:21:19-07:00
logb301999cd3e3d070f5d62a9d3da2a736b7026b30
treee21065a35d3805e405c7a9e2d6b023ccf5d33f44
parent253906fb93f25481ae80af9043b58342858e156c

zig fmt: if condition wraps


2 files changed, 113 insertions(+), 110 deletions(-)

lib/std/zig/parser_test.zig+102-102
......@@ -1308,108 +1308,108 @@ test "zig fmt: 2nd arg multiline string" {
13081308 );
13091309}
13101310
1311//test "zig fmt: 2nd arg multiline string many args" {
1312// try testCanonical(
1313// \\comptime {
1314// \\ cases.addAsm("hello world linux x86_64",
1315// \\ \\.text
1316// \\ , "Hello, world!\n", "Hello, world!\n");
1317// \\}
1318// \\
1319// );
1320//}
1321//
1322//test "zig fmt: final arg multiline string" {
1323// try testCanonical(
1324// \\comptime {
1325// \\ cases.addAsm("hello world linux x86_64", "Hello, world!\n",
1326// \\ \\.text
1327// \\ );
1328// \\}
1329// \\
1330// );
1331//}
1332//
1333//test "zig fmt: if condition wraps" {
1334// try testTransform(
1335// \\comptime {
1336// \\ if (cond and
1337// \\ cond) {
1338// \\ return x;
1339// \\ }
1340// \\ while (cond and
1341// \\ cond) {
1342// \\ return x;
1343// \\ }
1344// \\ if (a == b and
1345// \\ c) {
1346// \\ a = b;
1347// \\ }
1348// \\ while (a == b and
1349// \\ c) {
1350// \\ a = b;
1351// \\ }
1352// \\ if ((cond and
1353// \\ cond)) {
1354// \\ return x;
1355// \\ }
1356// \\ while ((cond and
1357// \\ cond)) {
1358// \\ return x;
1359// \\ }
1360// \\ var a = if (a) |*f| x: {
1361// \\ break :x &a.b;
1362// \\ } else |err| err;
1363// \\ var a = if (cond and
1364// \\ cond) |*f|
1365// \\ x: {
1366// \\ break :x &a.b;
1367// \\ } else |err| err;
1368// \\}
1369// ,
1370// \\comptime {
1371// \\ if (cond and
1372// \\ cond)
1373// \\ {
1374// \\ return x;
1375// \\ }
1376// \\ while (cond and
1377// \\ cond)
1378// \\ {
1379// \\ return x;
1380// \\ }
1381// \\ if (a == b and
1382// \\ c)
1383// \\ {
1384// \\ a = b;
1385// \\ }
1386// \\ while (a == b and
1387// \\ c)
1388// \\ {
1389// \\ a = b;
1390// \\ }
1391// \\ if ((cond and
1392// \\ cond))
1393// \\ {
1394// \\ return x;
1395// \\ }
1396// \\ while ((cond and
1397// \\ cond))
1398// \\ {
1399// \\ return x;
1400// \\ }
1401// \\ var a = if (a) |*f| x: {
1402// \\ break :x &a.b;
1403// \\ } else |err| err;
1404// \\ var a = if (cond and
1405// \\ cond) |*f|
1406// \\ x: {
1407// \\ break :x &a.b;
1408// \\ } else |err| err;
1409// \\}
1410// \\
1411// );
1412//}
1311test "zig fmt: 2nd arg multiline string many args" {
1312 try testCanonical(
1313 \\comptime {
1314 \\ cases.addAsm("hello world linux x86_64",
1315 \\ \\.text
1316 \\ , "Hello, world!\n", "Hello, world!\n");
1317 \\}
1318 \\
1319 );
1320}
1321
1322test "zig fmt: final arg multiline string" {
1323 try testCanonical(
1324 \\comptime {
1325 \\ cases.addAsm("hello world linux x86_64", "Hello, world!\n",
1326 \\ \\.text
1327 \\ );
1328 \\}
1329 \\
1330 );
1331}
1332
1333test "zig fmt: if condition wraps" {
1334 try testTransform(
1335 \\comptime {
1336 \\ if (cond and
1337 \\ cond) {
1338 \\ return x;
1339 \\ }
1340 \\ while (cond and
1341 \\ cond) {
1342 \\ return x;
1343 \\ }
1344 \\ if (a == b and
1345 \\ c) {
1346 \\ a = b;
1347 \\ }
1348 \\ while (a == b and
1349 \\ c) {
1350 \\ a = b;
1351 \\ }
1352 \\ if ((cond and
1353 \\ cond)) {
1354 \\ return x;
1355 \\ }
1356 \\ while ((cond and
1357 \\ cond)) {
1358 \\ return x;
1359 \\ }
1360 \\ var a = if (a) |*f| x: {
1361 \\ break :x &a.b;
1362 \\ } else |err| err;
1363 \\ var a = if (cond and
1364 \\ cond) |*f|
1365 \\ x: {
1366 \\ break :x &a.b;
1367 \\ } else |err| err;
1368 \\}
1369 ,
1370 \\comptime {
1371 \\ if (cond and
1372 \\ cond)
1373 \\ {
1374 \\ return x;
1375 \\ }
1376 \\ while (cond and
1377 \\ cond)
1378 \\ {
1379 \\ return x;
1380 \\ }
1381 \\ if (a == b and
1382 \\ c)
1383 \\ {
1384 \\ a = b;
1385 \\ }
1386 \\ while (a == b and
1387 \\ c)
1388 \\ {
1389 \\ a = b;
1390 \\ }
1391 \\ if ((cond and
1392 \\ cond))
1393 \\ {
1394 \\ return x;
1395 \\ }
1396 \\ while ((cond and
1397 \\ cond))
1398 \\ {
1399 \\ return x;
1400 \\ }
1401 \\ var a = if (a) |*f| x: {
1402 \\ break :x &a.b;
1403 \\ } else |err| err;
1404 \\ var a = if (cond and
1405 \\ cond) |*f|
1406 \\ x: {
1407 \\ break :x &a.b;
1408 \\ } else |err| err;
1409 \\}
1410 \\
1411 );
1412}
14131413//
14141414//test "zig fmt: if condition has line break but must not wrap" {
14151415// try testCanonical(
lib/std/zig/render.zig+11-8
......@@ -499,10 +499,9 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac
499499 },
500500
501501 .grouped_expression => {
502 ais.pushIndentNextLine();
503502 try renderToken(ais, tree, main_tokens[node], .none); // lparen
503 ais.pushIndentOneShot();
504504 try renderExpression(ais, tree, datas[node].lhs, .none);
505 ais.popIndent();
506505 return renderToken(ais, tree, datas[node].rhs, space); // rparen
507506 },
508507
......@@ -981,13 +980,13 @@ fn renderWhile(ais: *Ais, tree: ast.Tree, while_node: ast.full.While, space: Spa
981980 }
982981
983982 try renderToken(ais, tree, while_node.ast.while_token, .space); // if
984 try renderToken(ais, tree, while_node.ast.while_token + 1, .none); // (
983 try renderToken(ais, tree, while_node.ast.while_token + 1, .none); // lparen
985984 try renderExpression(ais, tree, while_node.ast.cond_expr, .none); // condition
986985
987986 const then_tag = node_tags[while_node.ast.then_expr];
988987 if (nodeIsBlock(then_tag) and !nodeIsIf(then_tag)) {
989988 if (while_node.payload_token) |payload_token| {
990 try renderToken(ais, tree, payload_token - 2, .space); // )
989 try renderToken(ais, tree, payload_token - 2, .space); // rparen
991990 try renderToken(ais, tree, payload_token - 1, .none); // |
992991 const ident = blk: {
993992 if (token_tags[payload_token] == .asterisk) {
......@@ -1007,10 +1006,14 @@ fn renderWhile(ais: *Ais, tree: ast.Tree, while_node: ast.full.While, space: Spa
10071006 break :blk ident + 1;
10081007 }
10091008 };
1010 try renderToken(ais, tree, pipe, .space); // |
1009 const cond_has_newline = !tree.tokensOnSameLine(while_node.ast.while_token, pipe);
1010 const brace_space: Space = if (cond_has_newline) .newline else .space;
1011 try renderToken(ais, tree, pipe, brace_space); // |
10111012 } else {
10121013 const rparen = tree.lastToken(while_node.ast.cond_expr) + 1;
1013 try renderToken(ais, tree, rparen, .space); // )
1014 const cond_has_newline = !tree.tokensOnSameLine(while_node.ast.while_token, rparen);
1015 const brace_space: Space = if (cond_has_newline) .newline else .space;
1016 try renderToken(ais, tree, rparen, brace_space); // rparen
10141017 }
10151018 if (while_node.ast.cont_expr != 0) {
10161019 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
10401043
10411044 if (src_has_newline) {
10421045 if (while_node.payload_token) |payload_token| {
1043 try renderToken(ais, tree, payload_token - 2, .space); // )
1046 try renderToken(ais, tree, payload_token - 2, .space); // rparen
10441047 try renderToken(ais, tree, payload_token - 1, .none); // |
10451048 const ident = blk: {
10461049 if (token_tags[payload_token] == .asterisk) {
......@@ -1063,7 +1066,7 @@ fn renderWhile(ais: *Ais, tree: ast.Tree, while_node: ast.full.While, space: Spa
10631066 try renderToken(ais, tree, pipe, .newline); // |
10641067 } else {
10651068 ais.pushIndent();
1066 try renderToken(ais, tree, rparen, .newline); // )
1069 try renderToken(ais, tree, rparen, .newline); // rparen
10671070 ais.popIndent();
10681071 }
10691072 if (while_node.ast.cont_expr != 0) {