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
35433543 );
35443544}
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
35463586const std = @import("std");
35473587const mem = std.mem;
35483588const warn = std.debug.warn;
lib/std/zig/render.zig+36-27
......@@ -1058,21 +1058,22 @@ fn renderExpression(
10581058 };
10591059
10601060 if (src_has_trailing_comma) {
1061 try renderToken(tree, ais, lparen, Space.Newline);
1062
1063 const params = call.params();
1064 for (params) |param_node, i| {
1061 {
10651062 ais.pushIndent();
10661063 defer ais.popIndent();
10671064
1068 if (i + 1 < params.len) {
1069 const next_node = params[i + 1];
1070 try renderExpression(allocator, ais, tree, param_node, Space.None);
1071 const comma = tree.nextToken(param_node.lastToken());
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);
1065 try renderToken(tree, ais, lparen, Space.Newline); // (
1066 const params = call.params();
1067 for (params) |param_node, i| {
1068 if (i + 1 < params.len) {
1069 const next_node = params[i + 1];
1070 try renderExpression(allocator, ais, tree, param_node, Space.None);
1071 const comma = tree.nextToken(param_node.lastToken());
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 }
10761077 }
10771078 }
10781079 return renderToken(tree, ais, call.rtoken, space);
......@@ -1082,7 +1083,10 @@ fn renderExpression(
10821083
10831084 const params = call.params();
10841085 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
10871091 try renderExpression(allocator, ais, tree, param_node, Space.None);
10881092
......@@ -1092,7 +1096,7 @@ fn renderExpression(
10921096 try renderToken(tree, ais, comma, Space.Space);
10931097 }
10941098 }
1095 return renderToken(tree, ais, call.rtoken, space);
1099 return renderToken(tree, ais, call.rtoken, space); // )
10961100 },
10971101
10981102 .ArrayAccess => {
......@@ -1497,6 +1501,10 @@ fn renderExpression(
14971501 // render all on one line, no trailing comma
14981502 const params = builtin_call.params();
14991503 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 }
15001508 try renderExpression(allocator, ais, tree, param_node, Space.None);
15011509
15021510 if (i + 1 < params.len) {
......@@ -1548,19 +1556,20 @@ fn renderExpression(
15481556 assert(tree.token_ids[lparen] == .LParen);
15491557
15501558 const rparen = tree.prevToken(
1551 // the first token for the annotation expressions is the left
1552 // parenthesis, hence the need for two prevToken
1553 if (fn_proto.getAlignExpr()) |align_expr|
1554 tree.prevToken(tree.prevToken(align_expr.firstToken()))
1555 else if (fn_proto.getSectionExpr()) |section_expr|
1556 tree.prevToken(tree.prevToken(section_expr.firstToken()))
1557 else if (fn_proto.getCallconvExpr()) |callconv_expr|
1558 tree.prevToken(tree.prevToken(callconv_expr.firstToken()))
1559 else switch (fn_proto.return_type) {
1560 .Explicit => |node| node.firstToken(),
1561 .InferErrorSet => |node| tree.prevToken(node.firstToken()),
1562 .Invalid => unreachable,
1563 });
1559 // the first token for the annotation expressions is the left
1560 // parenthesis, hence the need for two prevToken
1561 if (fn_proto.getAlignExpr()) |align_expr|
1562 tree.prevToken(tree.prevToken(align_expr.firstToken()))
1563 else if (fn_proto.getSectionExpr()) |section_expr|
1564 tree.prevToken(tree.prevToken(section_expr.firstToken()))
1565 else if (fn_proto.getCallconvExpr()) |callconv_expr|
1566 tree.prevToken(tree.prevToken(callconv_expr.firstToken()))
1567 else switch (fn_proto.return_type) {
1568 .Explicit => |node| node.firstToken(),
1569 .InferErrorSet => |node| tree.prevToken(node.firstToken()),
1570 .Invalid => unreachable,
1571 },
1572 );
15641573 assert(tree.token_ids[rparen] == .RParen);
15651574
15661575 const src_params_trailing_comma = blk: {