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" {...@@ -3494,30 +3494,31 @@ test "zig fmt: file ends with struct field" {
3494// );3494// );
3495//}3495//}
34963496
3497//test "zig fmt: comment after params" {3497test "zig fmt: comment after params" {
3498// try testTransform(3498 try testTransform(
3499// \\fn a(3499 \\fn a(
3500// \\ b: u323500 \\ b: u32
3501// \\ // c: u32,3501 \\ // c: u32,
3502// \\ // d: u32,3502 \\ // d: u32,
3503// \\) void {}3503 \\) void {}
3504// \\3504 \\
3505// ,3505 ,
3506// \\fn a(3506 \\fn a(
3507// \\ b: u32, // c: u32,3507 \\ b: u32,
3508// \\ // d: u32,3508 \\ // c: u32,
3509// \\) void {}3509 \\ // d: u32,
3510// \\3510 \\) void {}
3511// );3511 \\
3512// try testCanonical(3512 );
3513// \\fn a(3513 try testCanonical(
3514// \\ b: u32,3514 \\fn a(
3515// \\ // c: u32,3515 \\ b: u32,
3516// \\ // d: u32,3516 \\ // c: u32,
3517// \\) void {}3517 \\ // d: u32,
3518// \\3518 \\) void {}
3519// );3519 \\
3520//}3520 );
3521}
35213522
3522//test "zig fmt: comment in array initializer/access" {3523//test "zig fmt: comment in array initializer/access" {
3523// try testCanonical(3524// 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....@@ -1337,7 +1337,8 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: ast.Tree, fn_proto: ast.full.
13371337
1338 // The params list is a sparse set that does *not* include anytype or ... parameters.1338 // 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)) {
1341 // Render all on one line, no trailing comma.1342 // Render all on one line, no trailing comma.
1342 try renderToken(ais, tree, lparen, .none); // (1343 try renderToken(ais, tree, lparen, .none); // (
13431344
...@@ -1415,7 +1416,9 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: ast.Tree, fn_proto: ast.full....@@ -1415,7 +1416,9 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: ast.Tree, fn_proto: ast.full.
1415 continue;1416 continue;
1416 },1417 },
1417 .r_paren => break,1418 .r_paren => break,
1418 else => unreachable,1419 else => {
1420 std.debug.print("\n{}\n", .{token_tags[last_param_token]});
1421 },
1419 }1422 }
1420 if (token_tags[last_param_token] == .identifier) {1423 if (token_tags[last_param_token] == .identifier) {
1421 try renderToken(ais, tree, last_param_token, .none); // name1424 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....@@ -1430,7 +1433,8 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: ast.Tree, fn_proto: ast.full.
1430 const param = fn_proto.ast.params[param_i];1433 const param = fn_proto.ast.params[param_i];
1431 param_i += 1;1434 param_i += 1;
1432 try renderExpression(gpa, ais, tree, param, .comma);1435 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;
1434 }1438 }
1435 ais.popIndent();1439 ais.popIndent();
1436 }1440 }
...@@ -2171,6 +2175,19 @@ fn renderToken(ais: *Ais, tree: ast.Tree, token_index: ast.TokenIndex, space: Sp...@@ -2171,6 +2175,19 @@ fn renderToken(ais: *Ais, tree: ast.Tree, token_index: ast.TokenIndex, space: Sp
2171 }2175 }
2172}2176}
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
2174/// Assumes that start is the first byte past the previous token and2191/// Assumes that start is the first byte past the previous token and
2175/// that end is the last byte before the next token.2192/// that end is the last byte before the next token.
2176fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize) Error!bool {2193fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize) Error!bool {