authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-02-23 19:11:50+01:00
committergravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-02-23 19:11:50+01:00
log5820bd0e64ce58cca045a5dfe5ba03d9979eece8
treef053785c53aa3757129f92ed71208914bce01744
parent5306b1a9ab5cb0730fcc21da095db39b0197e99b
signaturelock-open Commit is signed but in an unrecognized format.

zig fmt: insert trailing comma in fn params with comment


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

lib/std/zig/parser_test.zig+25-24
......@@ -3494,30 +3494,31 @@ test "zig fmt: file ends with struct field" {
34943494// );
34953495//}
34963496
3497//test "zig fmt: comment after params" {
3498// try testTransform(
3499// \\fn a(
3500// \\ b: u32
3501// \\ // c: u32,
3502// \\ // d: u32,
3503// \\) void {}
3504// \\
3505// ,
3506// \\fn a(
3507// \\ b: u32, // c: u32,
3508// \\ // d: u32,
3509// \\) void {}
3510// \\
3511// );
3512// try testCanonical(
3513// \\fn a(
3514// \\ b: u32,
3515// \\ // c: u32,
3516// \\ // d: u32,
3517// \\) void {}
3518// \\
3519// );
3520//}
3497test "zig fmt: comment after params" {
3498 try testTransform(
3499 \\fn a(
3500 \\ b: u32
3501 \\ // c: u32,
3502 \\ // d: u32,
3503 \\) void {}
3504 \\
3505 ,
3506 \\fn a(
3507 \\ b: u32,
3508 \\ // c: u32,
3509 \\ // d: u32,
3510 \\) void {}
3511 \\
3512 );
3513 try testCanonical(
3514 \\fn a(
3515 \\ b: u32,
3516 \\ // c: u32,
3517 \\ // d: u32,
3518 \\) void {}
3519 \\
3520 );
3521}
35213522
35223523//test "zig fmt: comment in array initializer/access" {
35233524// try testCanonical(
lib/std/zig/render.zig+20-3
......@@ -1337,7 +1337,8 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: ast.Tree, fn_proto: ast.full.
13371337
13381338 // The params list is a sparse set that does *not* include anytype or ... parameters.
13391339
1340 if (token_tags[rparen - 1] != .comma) {
1340 const trailing_comma = token_tags[rparen - 1] == .comma;
1341 if (!trailing_comma and !hasComment(tree, lparen, rparen)) {
13411342 // Render all on one line, no trailing comma.
13421343 try renderToken(ais, tree, lparen, .none); // (
13431344
......@@ -1415,7 +1416,9 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: ast.Tree, fn_proto: ast.full.
14151416 continue;
14161417 },
14171418 .r_paren => break,
1418 else => unreachable,
1419 else => {
1420 std.debug.print("\n{}\n", .{token_tags[last_param_token]});
1421 },
14191422 }
14201423 if (token_tags[last_param_token] == .identifier) {
14211424 try renderToken(ais, tree, last_param_token, .none); // name
......@@ -1430,7 +1433,8 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: ast.Tree, fn_proto: ast.full.
14301433 const param = fn_proto.ast.params[param_i];
14311434 param_i += 1;
14321435 try renderExpression(gpa, ais, tree, param, .comma);
1433 last_param_token = tree.lastToken(param) + 1;
1436 last_param_token = tree.lastToken(param);
1437 if (token_tags[last_param_token + 1] == .comma) last_param_token += 1;
14341438 }
14351439 ais.popIndent();
14361440 }
......@@ -2171,6 +2175,19 @@ fn renderToken(ais: *Ais, tree: ast.Tree, token_index: ast.TokenIndex, space: Sp
21712175 }
21722176}
21732177
2178/// Returns true if there exists a comment between the start of token
2179/// `start_token` and the start of token `end_token`. This is used to determine
2180/// if e.g. a fn_proto should be wrapped and have a trailing comma inserted
2181/// even if there is none in the source.
2182fn hasComment(tree: ast.Tree, start_token: ast.TokenIndex, end_token: ast.TokenIndex) bool {
2183 const token_starts = tree.tokens.items(.start);
2184
2185 const start = token_starts[start_token];
2186 const end = token_starts[end_token];
2187
2188 return mem.indexOf(u8, tree.source[start..end], "//") != null;
2189}
2190
21742191/// Assumes that start is the first byte past the previous token and
21752192/// that end is the last byte before the next token.
21762193fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize) Error!bool {