authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-23 16:09:51-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-23 16:09:51-07:00
log6f4a1bafcf9cc1120881dabc462b46696481720e
tree93bac849859c3c45046e18c6778da50673f96074
parent4ee368c4b33f5cf286c575e94535deaf59659895

zig fmt: fn call with comments and multiline strings

forcing the parameters over multiple lines

2 files changed, 62 insertions(+), 48 deletions(-)

lib/std/zig/parser_test.zig+39-39
......@@ -4049,45 +4049,45 @@ test "zig fmt: allow trailing line comments to do manual array formatting" {
40494049// );
40504050//}
40514051
4052//test "zig fmt: use of comments and Multiline string literals may force the parameters over multiple lines" {
4053// try testCanonical(
4054// \\pub fn makeMemUndefined(qzz: []u8) i1 {
4055// \\ cases.add( // fixed bug #2032
4056// \\ "compile diagnostic string for top level decl type",
4057// \\ \\export fn entry() void {
4058// \\ \\ var foo: u32 = @This(){};
4059// \\ \\}
4060// \\ , &[_][]const u8{
4061// \\ "tmp.zig:2:27: error: type 'u32' does not support array initialization",
4062// \\ });
4063// \\ @compileError(
4064// \\ \\ unknown-length pointers and C pointers cannot be hashed deeply.
4065// \\ \\ Consider providing your own hash function.
4066// \\ \\ unknown-length pointers and C pointers cannot be hashed deeply.
4067// \\ \\ Consider providing your own hash function.
4068// \\ );
4069// \\ return @intCast(i1, doMemCheckClientRequestExpr(0, // default return
4070// \\ .MakeMemUndefined, @ptrToInt(qzz.ptr), qzz.len, 0, 0, 0));
4071// \\}
4072// \\
4073// \\// This looks like garbage don't do this
4074// \\const rparen = tree.prevToken(
4075// \\// the first token for the annotation expressions is the left
4076// \\// parenthesis, hence the need for two prevToken
4077// \\ if (fn_proto.getAlignExpr()) |align_expr|
4078// \\ tree.prevToken(tree.prevToken(align_expr.firstToken()))
4079// \\else if (fn_proto.getSectionExpr()) |section_expr|
4080// \\ tree.prevToken(tree.prevToken(section_expr.firstToken()))
4081// \\else if (fn_proto.getCallconvExpr()) |callconv_expr|
4082// \\ tree.prevToken(tree.prevToken(callconv_expr.firstToken()))
4083// \\else switch (fn_proto.return_type) {
4084// \\ .Explicit => |node| node.firstToken(),
4085// \\ .InferErrorSet => |node| tree.prevToken(node.firstToken()),
4086// \\ .Invalid => unreachable,
4087// \\});
4088// \\
4089// );
4090//}
4052test "zig fmt: use of comments and multiline string literals may force the parameters over multiple lines" {
4053 try testCanonical(
4054 \\pub fn makeMemUndefined(qzz: []u8) i1 {
4055 \\ cases.add( // fixed bug foo
4056 \\ "compile diagnostic string for top level decl type",
4057 \\ \\export fn entry() void {
4058 \\ \\ var foo: u32 = @This(){};
4059 \\ \\}
4060 \\ , &[_][]const u8{
4061 \\ "tmp.zig:2:27: error: type 'u32' does not support array initialization",
4062 \\ });
4063 \\ @compileError(
4064 \\ \\ unknown-length pointers and C pointers cannot be hashed deeply.
4065 \\ \\ Consider providing your own hash function.
4066 \\ \\ unknown-length pointers and C pointers cannot be hashed deeply.
4067 \\ \\ Consider providing your own hash function.
4068 \\ );
4069 \\ return @intCast(i1, doMemCheckClientRequestExpr(0, // default return
4070 \\ .MakeMemUndefined, @ptrToInt(qzz.ptr), qzz.len, 0, 0, 0));
4071 \\}
4072 \\
4073 \\// This looks like garbage don't do this
4074 \\const rparen = tree.prevToken(
4075 \\// the first token for the annotation expressions is the left
4076 \\// parenthesis, hence the need for two prevToken
4077 \\if (fn_proto.getAlignExpr()) |align_expr|
4078 \\ tree.prevToken(tree.prevToken(align_expr.firstToken()))
4079 \\else if (fn_proto.getSectionExpr()) |section_expr|
4080 \\ tree.prevToken(tree.prevToken(section_expr.firstToken()))
4081 \\else if (fn_proto.getCallconvExpr()) |callconv_expr|
4082 \\ tree.prevToken(tree.prevToken(callconv_expr.firstToken()))
4083 \\else switch (fn_proto.return_type) {
4084 \\ .Explicit => |node| node.firstToken(),
4085 \\ .InferErrorSet => |node| tree.prevToken(node.firstToken()),
4086 \\ .Invalid => unreachable,
4087 \\});
4088 \\
4089 );
4090}
40914091
40924092test "zig fmt: single argument trailing commas in @builtins()" {
40934093 try testCanonical(
lib/std/zig/render.zig+23-9
......@@ -1291,6 +1291,12 @@ fn renderBuiltinCall(
12911291 try renderToken(ais, tree, builtin_token + 1, .none); // (
12921292
12931293 for (params) |param_node, i| {
1294 const first_param_token = tree.firstToken(param_node);
1295 if (token_tags[first_param_token] == .multiline_string_literal_line or
1296 hasSameLineComment(tree, first_param_token - 1))
1297 {
1298 ais.pushIndentOneShot();
1299 }
12941300 try renderExpression(gpa, ais, tree, param_node, .none);
12951301
12961302 if (i + 1 < params.len) {
......@@ -1733,12 +1739,8 @@ fn renderArrayInit(
17331739
17341740 const maybe_comma = expr_last_token + 1;
17351741 if (token_tags[maybe_comma] == .comma) {
1736 const after_comma_src = tree.source[token_starts[maybe_comma]..token_starts[maybe_comma + 1]];
1737 for (after_comma_src) |byte| switch (byte) {
1738 '\n' => break,
1739 '/' => break :sec_end i - this_line_size.? + 1,
1740 else => continue,
1741 };
1742 if (hasSameLineComment(tree, maybe_comma))
1743 break :sec_end i - this_line_size.? + 1;
17421744 }
17431745 }
17441746 break :sec_end row_exprs.len;
......@@ -2106,9 +2108,10 @@ fn renderCall(
21062108 try renderToken(ais, tree, lparen, .none); // (
21072109
21082110 for (params) |param_node, i| {
2109 const this_multiline_string =
2110 token_tags[tree.firstToken(param_node)] == .multiline_string_literal_line;
2111 if (this_multiline_string) {
2111 const first_param_token = tree.firstToken(param_node);
2112 if (token_tags[first_param_token] == .multiline_string_literal_line or
2113 hasSameLineComment(tree, first_param_token - 1))
2114 {
21122115 ais.pushIndentOneShot();
21132116 }
21142117 try renderExpression(gpa, ais, tree, param_node, .none);
......@@ -2339,6 +2342,17 @@ fn tokenSliceForRender(tree: ast.Tree, token_index: ast.TokenIndex) []const u8 {
23392342 return ret;
23402343}
23412344
2345fn hasSameLineComment(tree: ast.Tree, token_index: ast.TokenIndex) bool {
2346 const token_starts = tree.tokens.items(.start);
2347 const between_source = tree.source[token_starts[token_index]..token_starts[token_index + 1]];
2348 for (between_source) |byte| switch (byte) {
2349 '\n' => return false,
2350 '/' => return true,
2351 else => continue,
2352 };
2353 return false;
2354}
2355
23422356fn writeFixingWhitespace(writer: std.ArrayList(u8).Writer, slice: []const u8) Error!void {
23432357 for (slice) |byte| switch (byte) {
23442358 '\t' => try writer.writeAll(" " ** 4),