authorgravatar for lachlan@lakebythewoods.xyzLachlan Easton <lachlan@lakebythewoods.xyz> 2020-09-09 21:45:05+10:00
committergravatar for lachlan@lakebythewoods.xyzLachlan Easton <lachlan@lakebythewoods.xyz> 2020-09-18 20:34:00+10:00
log206a8cf6709df51213252b03bf3ba4a9b8b52b6f
tree7517f9f949441a703970d6692c5f5131f2fe15d5
parent291482a0310312fa3d84b3a967fa3f2d5b71b165

zig fmt: fix comments and multiline literals in function args


2 files changed, 76 insertions(+), 27 deletions(-)

lib/std/zig/parser_test.zig+40
...@@ -3543,6 +3543,46 @@ test "zig fmt: multiline string literals should play nice with array initializer...@@ -3543,6 +3543,46 @@ test "zig fmt: multiline string literals should play nice with array initializer
3543 );3543 );
3544}3544}
35453545
3546test "zig fmt: use of comments and Multiline string literals may force the parameters over multiple lines" {
3547 try testCanonical(
3548 \\pub fn makeMemUndefined(qzz: []u8) i1 {
3549 \\ cases.add( // fixed bug #2032
3550 \\ "compile diagnostic string for top level decl type",
3551 \\ \\export fn entry() void {
3552 \\ \\ var foo: u32 = @This(){};
3553 \\ \\}
3554 \\ , &[_][]const u8{
3555 \\ "tmp.zig:2:27: error: type 'u32' does not support array initialization",
3556 \\ });
3557 \\ @compileError(
3558 \\ \\ unknown-length pointers and C pointers cannot be hashed deeply.
3559 \\ \\ Consider providing your own hash function.
3560 \\ \\ unknown-length pointers and C pointers cannot be hashed deeply.
3561 \\ \\ Consider providing your own hash function.
3562 \\ );
3563 \\ return @intCast(i1, doMemCheckClientRequestExpr(0, // default return
3564 \\ .MakeMemUndefined, @ptrToInt(qzz.ptr), qzz.len, 0, 0, 0));
3565 \\}
3566 \\
3567 \\// This looks like garbage don't do this
3568 \\const rparen = tree.prevToken(
3569 \\// the first token for the annotation expressions is the left
3570 \\// parenthesis, hence the need for two prevToken
3571 \\ if (fn_proto.getAlignExpr()) |align_expr|
3572 \\ tree.prevToken(tree.prevToken(align_expr.firstToken()))
3573 \\else if (fn_proto.getSectionExpr()) |section_expr|
3574 \\ tree.prevToken(tree.prevToken(section_expr.firstToken()))
3575 \\else if (fn_proto.getCallconvExpr()) |callconv_expr|
3576 \\ tree.prevToken(tree.prevToken(callconv_expr.firstToken()))
3577 \\else switch (fn_proto.return_type) {
3578 \\ .Explicit => |node| node.firstToken(),
3579 \\ .InferErrorSet => |node| tree.prevToken(node.firstToken()),
3580 \\ .Invalid => unreachable,
3581 \\});
3582 \\
3583 );
3584}
3585
3546const std = @import("std");3586const std = @import("std");
3547const mem = std.mem;3587const mem = std.mem;
3548const warn = std.debug.warn;3588const warn = std.debug.warn;
lib/std/zig/render.zig+36-27
...@@ -1058,21 +1058,22 @@ fn renderExpression(...@@ -1058,21 +1058,22 @@ fn renderExpression(
1058 };1058 };
10591059
1060 if (src_has_trailing_comma) {1060 if (src_has_trailing_comma) {
1061 try renderToken(tree, ais, lparen, Space.Newline);1061 {
1062
1063 const params = call.params();
1064 for (params) |param_node, i| {
1065 ais.pushIndent();1062 ais.pushIndent();
1066 defer ais.popIndent();1063 defer ais.popIndent();
10671064
1068 if (i + 1 < params.len) {1065 try renderToken(tree, ais, lparen, Space.Newline); // (
1069 const next_node = params[i + 1];1066 const params = call.params();
1070 try renderExpression(allocator, ais, tree, param_node, Space.None);1067 for (params) |param_node, i| {
1071 const comma = tree.nextToken(param_node.lastToken());1068 if (i + 1 < params.len) {
1072 try renderToken(tree, ais, comma, Space.Newline); // ,1069 const next_node = params[i + 1];
1073 try renderExtraNewline(tree, ais, next_node);1070 try renderExpression(allocator, ais, tree, param_node, Space.None);
1074 } else {1071 const comma = tree.nextToken(param_node.lastToken());
1075 try renderExpression(allocator, ais, tree, param_node, Space.Comma);1072 try renderToken(tree, ais, comma, Space.Newline); // ,
1073 try renderExtraNewline(tree, ais, next_node);
1074 } else {
1075 try renderExpression(allocator, ais, tree, param_node, Space.Comma);
1076 }
1076 }1077 }
1077 }1078 }
1078 return renderToken(tree, ais, call.rtoken, space);1079 return renderToken(tree, ais, call.rtoken, space);
...@@ -1082,7 +1083,10 @@ fn renderExpression(...@@ -1082,7 +1083,10 @@ fn renderExpression(
10821083
1083 const params = call.params();1084 const params = call.params();
1084 for (params) |param_node, i| {1085 for (params) |param_node, i| {
1085 if (param_node.*.tag == .MultilineStringLiteral) ais.pushIndentOneShot();1086 const maybe_comment = param_node.firstToken() - 1;
1087 if (param_node.*.tag == .MultilineStringLiteral or tree.token_ids[maybe_comment] == .LineComment) {
1088 ais.pushIndentOneShot();
1089 }
10861090
1087 try renderExpression(allocator, ais, tree, param_node, Space.None);1091 try renderExpression(allocator, ais, tree, param_node, Space.None);
10881092
...@@ -1092,7 +1096,7 @@ fn renderExpression(...@@ -1092,7 +1096,7 @@ fn renderExpression(
1092 try renderToken(tree, ais, comma, Space.Space);1096 try renderToken(tree, ais, comma, Space.Space);
1093 }1097 }
1094 }1098 }
1095 return renderToken(tree, ais, call.rtoken, space);1099 return renderToken(tree, ais, call.rtoken, space); // )
1096 },1100 },
10971101
1098 .ArrayAccess => {1102 .ArrayAccess => {
...@@ -1497,6 +1501,10 @@ fn renderExpression(...@@ -1497,6 +1501,10 @@ fn renderExpression(
1497 // render all on one line, no trailing comma1501 // render all on one line, no trailing comma
1498 const params = builtin_call.params();1502 const params = builtin_call.params();
1499 for (params) |param_node, i| {1503 for (params) |param_node, i| {
1504 const maybe_comment = param_node.firstToken() - 1;
1505 if (param_node.*.tag == .MultilineStringLiteral or tree.token_ids[maybe_comment] == .LineComment) {
1506 ais.pushIndentOneShot();
1507 }
1500 try renderExpression(allocator, ais, tree, param_node, Space.None);1508 try renderExpression(allocator, ais, tree, param_node, Space.None);
15011509
1502 if (i + 1 < params.len) {1510 if (i + 1 < params.len) {
...@@ -1548,19 +1556,20 @@ fn renderExpression(...@@ -1548,19 +1556,20 @@ fn renderExpression(
1548 assert(tree.token_ids[lparen] == .LParen);1556 assert(tree.token_ids[lparen] == .LParen);
15491557
1550 const rparen = tree.prevToken(1558 const rparen = tree.prevToken(
1551 // the first token for the annotation expressions is the left1559 // the first token for the annotation expressions is the left
1552 // parenthesis, hence the need for two prevToken1560 // parenthesis, hence the need for two prevToken
1553 if (fn_proto.getAlignExpr()) |align_expr|1561 if (fn_proto.getAlignExpr()) |align_expr|
1554 tree.prevToken(tree.prevToken(align_expr.firstToken()))1562 tree.prevToken(tree.prevToken(align_expr.firstToken()))
1555 else if (fn_proto.getSectionExpr()) |section_expr|1563 else if (fn_proto.getSectionExpr()) |section_expr|
1556 tree.prevToken(tree.prevToken(section_expr.firstToken()))1564 tree.prevToken(tree.prevToken(section_expr.firstToken()))
1557 else if (fn_proto.getCallconvExpr()) |callconv_expr|1565 else if (fn_proto.getCallconvExpr()) |callconv_expr|
1558 tree.prevToken(tree.prevToken(callconv_expr.firstToken()))1566 tree.prevToken(tree.prevToken(callconv_expr.firstToken()))
1559 else switch (fn_proto.return_type) {1567 else switch (fn_proto.return_type) {
1560 .Explicit => |node| node.firstToken(),1568 .Explicit => |node| node.firstToken(),
1561 .InferErrorSet => |node| tree.prevToken(node.firstToken()),1569 .InferErrorSet => |node| tree.prevToken(node.firstToken()),
1562 .Invalid => unreachable,1570 .Invalid => unreachable,
1563 });1571 },
1572 );
1564 assert(tree.token_ids[rparen] == .RParen);1573 assert(tree.token_ids[rparen] == .RParen);
15651574
1566 const src_params_trailing_comma = blk: {1575 const src_params_trailing_comma = blk: {