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" {...@@ -1308,108 +1308,108 @@ test "zig fmt: 2nd arg multiline string" {
1308 );1308 );
1309}1309}
13101310
1311//test "zig fmt: 2nd arg multiline string many args" {1311test "zig fmt: 2nd arg multiline string many args" {
1312// try testCanonical(1312 try testCanonical(
1313// \\comptime {1313 \\comptime {
1314// \\ cases.addAsm("hello world linux x86_64",1314 \\ cases.addAsm("hello world linux x86_64",
1315// \\ \\.text1315 \\ \\.text
1316// \\ , "Hello, world!\n", "Hello, world!\n");1316 \\ , "Hello, world!\n", "Hello, world!\n");
1317// \\}1317 \\}
1318// \\1318 \\
1319// );1319 );
1320//}1320}
1321//1321
1322//test "zig fmt: final arg multiline string" {1322test "zig fmt: final arg multiline string" {
1323// try testCanonical(1323 try testCanonical(
1324// \\comptime {1324 \\comptime {
1325// \\ cases.addAsm("hello world linux x86_64", "Hello, world!\n",1325 \\ cases.addAsm("hello world linux x86_64", "Hello, world!\n",
1326// \\ \\.text1326 \\ \\.text
1327// \\ );1327 \\ );
1328// \\}1328 \\}
1329// \\1329 \\
1330// );1330 );
1331//}1331}
1332//1332
1333//test "zig fmt: if condition wraps" {1333test "zig fmt: if condition wraps" {
1334// try testTransform(1334 try testTransform(
1335// \\comptime {1335 \\comptime {
1336// \\ if (cond and1336 \\ if (cond and
1337// \\ cond) {1337 \\ cond) {
1338// \\ return x;1338 \\ return x;
1339// \\ }1339 \\ }
1340// \\ while (cond and1340 \\ while (cond and
1341// \\ cond) {1341 \\ cond) {
1342// \\ return x;1342 \\ return x;
1343// \\ }1343 \\ }
1344// \\ if (a == b and1344 \\ if (a == b and
1345// \\ c) {1345 \\ c) {
1346// \\ a = b;1346 \\ a = b;
1347// \\ }1347 \\ }
1348// \\ while (a == b and1348 \\ while (a == b and
1349// \\ c) {1349 \\ c) {
1350// \\ a = b;1350 \\ a = b;
1351// \\ }1351 \\ }
1352// \\ if ((cond and1352 \\ if ((cond and
1353// \\ cond)) {1353 \\ cond)) {
1354// \\ return x;1354 \\ return x;
1355// \\ }1355 \\ }
1356// \\ while ((cond and1356 \\ while ((cond and
1357// \\ cond)) {1357 \\ cond)) {
1358// \\ return x;1358 \\ return x;
1359// \\ }1359 \\ }
1360// \\ var a = if (a) |*f| x: {1360 \\ var a = if (a) |*f| x: {
1361// \\ break :x &a.b;1361 \\ break :x &a.b;
1362// \\ } else |err| err;1362 \\ } else |err| err;
1363// \\ var a = if (cond and1363 \\ var a = if (cond and
1364// \\ cond) |*f|1364 \\ cond) |*f|
1365// \\ x: {1365 \\ x: {
1366// \\ break :x &a.b;1366 \\ break :x &a.b;
1367// \\ } else |err| err;1367 \\ } else |err| err;
1368// \\}1368 \\}
1369// ,1369 ,
1370// \\comptime {1370 \\comptime {
1371// \\ if (cond and1371 \\ if (cond and
1372// \\ cond)1372 \\ cond)
1373// \\ {1373 \\ {
1374// \\ return x;1374 \\ return x;
1375// \\ }1375 \\ }
1376// \\ while (cond and1376 \\ while (cond and
1377// \\ cond)1377 \\ cond)
1378// \\ {1378 \\ {
1379// \\ return x;1379 \\ return x;
1380// \\ }1380 \\ }
1381// \\ if (a == b and1381 \\ if (a == b and
1382// \\ c)1382 \\ c)
1383// \\ {1383 \\ {
1384// \\ a = b;1384 \\ a = b;
1385// \\ }1385 \\ }
1386// \\ while (a == b and1386 \\ while (a == b and
1387// \\ c)1387 \\ c)
1388// \\ {1388 \\ {
1389// \\ a = b;1389 \\ a = b;
1390// \\ }1390 \\ }
1391// \\ if ((cond and1391 \\ if ((cond and
1392// \\ cond))1392 \\ cond))
1393// \\ {1393 \\ {
1394// \\ return x;1394 \\ return x;
1395// \\ }1395 \\ }
1396// \\ while ((cond and1396 \\ while ((cond and
1397// \\ cond))1397 \\ cond))
1398// \\ {1398 \\ {
1399// \\ return x;1399 \\ return x;
1400// \\ }1400 \\ }
1401// \\ var a = if (a) |*f| x: {1401 \\ var a = if (a) |*f| x: {
1402// \\ break :x &a.b;1402 \\ break :x &a.b;
1403// \\ } else |err| err;1403 \\ } else |err| err;
1404// \\ var a = if (cond and1404 \\ var a = if (cond and
1405// \\ cond) |*f|1405 \\ cond) |*f|
1406// \\ x: {1406 \\ x: {
1407// \\ break :x &a.b;1407 \\ break :x &a.b;
1408// \\ } else |err| err;1408 \\ } else |err| err;
1409// \\}1409 \\}
1410// \\1410 \\
1411// );1411 );
1412//}1412}
1413//1413//
1414//test "zig fmt: if condition has line break but must not wrap" {1414//test "zig fmt: if condition has line break but must not wrap" {
1415// try testCanonical(1415// 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...@@ -499,10 +499,9 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac
499 },499 },
500500
501 .grouped_expression => {501 .grouped_expression => {
502 ais.pushIndentNextLine();
503 try renderToken(ais, tree, main_tokens[node], .none); // lparen502 try renderToken(ais, tree, main_tokens[node], .none); // lparen
503 ais.pushIndentOneShot();
504 try renderExpression(ais, tree, datas[node].lhs, .none);504 try renderExpression(ais, tree, datas[node].lhs, .none);
505 ais.popIndent();
506 return renderToken(ais, tree, datas[node].rhs, space); // rparen505 return renderToken(ais, tree, datas[node].rhs, space); // rparen
507 },506 },
508507
...@@ -981,13 +980,13 @@ fn renderWhile(ais: *Ais, tree: ast.Tree, while_node: ast.full.While, space: Spa...@@ -981,13 +980,13 @@ fn renderWhile(ais: *Ais, tree: ast.Tree, while_node: ast.full.While, space: Spa
981 }980 }
982981
983 try renderToken(ais, tree, while_node.ast.while_token, .space); // if982 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
985 try renderExpression(ais, tree, while_node.ast.cond_expr, .none); // condition984 try renderExpression(ais, tree, while_node.ast.cond_expr, .none); // condition
986985
987 const then_tag = node_tags[while_node.ast.then_expr];986 const then_tag = node_tags[while_node.ast.then_expr];
988 if (nodeIsBlock(then_tag) and !nodeIsIf(then_tag)) {987 if (nodeIsBlock(then_tag) and !nodeIsIf(then_tag)) {
989 if (while_node.payload_token) |payload_token| {988 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
991 try renderToken(ais, tree, payload_token - 1, .none); // |990 try renderToken(ais, tree, payload_token - 1, .none); // |
992 const ident = blk: {991 const ident = blk: {
993 if (token_tags[payload_token] == .asterisk) {992 if (token_tags[payload_token] == .asterisk) {
...@@ -1007,10 +1006,14 @@ fn renderWhile(ais: *Ais, tree: ast.Tree, while_node: ast.full.While, space: Spa...@@ -1007,10 +1006,14 @@ fn renderWhile(ais: *Ais, tree: ast.Tree, while_node: ast.full.While, space: Spa
1007 break :blk ident + 1;1006 break :blk ident + 1;
1008 }1007 }
1009 };1008 };
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); // |
1011 } else {1012 } else {
1012 const rparen = tree.lastToken(while_node.ast.cond_expr) + 1;1013 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
1014 }1017 }
1015 if (while_node.ast.cont_expr != 0) {1018 if (while_node.ast.cont_expr != 0) {
1016 const rparen = tree.lastToken(while_node.ast.cont_expr) + 1;1019 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...@@ -1040,7 +1043,7 @@ fn renderWhile(ais: *Ais, tree: ast.Tree, while_node: ast.full.While, space: Spa
10401043
1041 if (src_has_newline) {1044 if (src_has_newline) {
1042 if (while_node.payload_token) |payload_token| {1045 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
1044 try renderToken(ais, tree, payload_token - 1, .none); // |1047 try renderToken(ais, tree, payload_token - 1, .none); // |
1045 const ident = blk: {1048 const ident = blk: {
1046 if (token_tags[payload_token] == .asterisk) {1049 if (token_tags[payload_token] == .asterisk) {
...@@ -1063,7 +1066,7 @@ fn renderWhile(ais: *Ais, tree: ast.Tree, while_node: ast.full.While, space: Spa...@@ -1063,7 +1066,7 @@ fn renderWhile(ais: *Ais, tree: ast.Tree, while_node: ast.full.While, space: Spa
1063 try renderToken(ais, tree, pipe, .newline); // |1066 try renderToken(ais, tree, pipe, .newline); // |
1064 } else {1067 } else {
1065 ais.pushIndent();1068 ais.pushIndent();
1066 try renderToken(ais, tree, rparen, .newline); // )1069 try renderToken(ais, tree, rparen, .newline); // rparen
1067 ais.popIndent();1070 ais.popIndent();
1068 }1071 }
1069 if (while_node.ast.cont_expr != 0) {1072 if (while_node.ast.cont_expr != 0) {