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" {...@@ -4049,45 +4049,45 @@ test "zig fmt: allow trailing line comments to do manual array formatting" {
4049// );4049// );
4050//}4050//}
40514051
4052//test "zig fmt: use of comments and Multiline string literals may force the parameters over multiple lines" {4052test "zig fmt: use of comments and multiline string literals may force the parameters over multiple lines" {
4053// try testCanonical(4053 try testCanonical(
4054// \\pub fn makeMemUndefined(qzz: []u8) i1 {4054 \\pub fn makeMemUndefined(qzz: []u8) i1 {
4055// \\ cases.add( // fixed bug #20324055 \\ cases.add( // fixed bug foo
4056// \\ "compile diagnostic string for top level decl type",4056 \\ "compile diagnostic string for top level decl type",
4057// \\ \\export fn entry() void {4057 \\ \\export fn entry() void {
4058// \\ \\ var foo: u32 = @This(){};4058 \\ \\ var foo: u32 = @This(){};
4059// \\ \\}4059 \\ \\}
4060// \\ , &[_][]const u8{4060 \\ , &[_][]const u8{
4061// \\ "tmp.zig:2:27: error: type 'u32' does not support array initialization",4061 \\ "tmp.zig:2:27: error: type 'u32' does not support array initialization",
4062// \\ });4062 \\ });
4063// \\ @compileError(4063 \\ @compileError(
4064// \\ \\ unknown-length pointers and C pointers cannot be hashed deeply.4064 \\ \\ unknown-length pointers and C pointers cannot be hashed deeply.
4065// \\ \\ Consider providing your own hash function.4065 \\ \\ Consider providing your own hash function.
4066// \\ \\ unknown-length pointers and C pointers cannot be hashed deeply.4066 \\ \\ unknown-length pointers and C pointers cannot be hashed deeply.
4067// \\ \\ Consider providing your own hash function.4067 \\ \\ Consider providing your own hash function.
4068// \\ );4068 \\ );
4069// \\ return @intCast(i1, doMemCheckClientRequestExpr(0, // default return4069 \\ return @intCast(i1, doMemCheckClientRequestExpr(0, // default return
4070// \\ .MakeMemUndefined, @ptrToInt(qzz.ptr), qzz.len, 0, 0, 0));4070 \\ .MakeMemUndefined, @ptrToInt(qzz.ptr), qzz.len, 0, 0, 0));
4071// \\}4071 \\}
4072// \\4072 \\
4073// \\// This looks like garbage don't do this4073 \\// This looks like garbage don't do this
4074// \\const rparen = tree.prevToken(4074 \\const rparen = tree.prevToken(
4075// \\// the first token for the annotation expressions is the left4075 \\// the first token for the annotation expressions is the left
4076// \\// parenthesis, hence the need for two prevToken4076 \\// parenthesis, hence the need for two prevToken
4077// \\ if (fn_proto.getAlignExpr()) |align_expr|4077 \\if (fn_proto.getAlignExpr()) |align_expr|
4078// \\ tree.prevToken(tree.prevToken(align_expr.firstToken()))4078 \\ tree.prevToken(tree.prevToken(align_expr.firstToken()))
4079// \\else if (fn_proto.getSectionExpr()) |section_expr|4079 \\else if (fn_proto.getSectionExpr()) |section_expr|
4080// \\ tree.prevToken(tree.prevToken(section_expr.firstToken()))4080 \\ tree.prevToken(tree.prevToken(section_expr.firstToken()))
4081// \\else if (fn_proto.getCallconvExpr()) |callconv_expr|4081 \\else if (fn_proto.getCallconvExpr()) |callconv_expr|
4082// \\ tree.prevToken(tree.prevToken(callconv_expr.firstToken()))4082 \\ tree.prevToken(tree.prevToken(callconv_expr.firstToken()))
4083// \\else switch (fn_proto.return_type) {4083 \\else switch (fn_proto.return_type) {
4084// \\ .Explicit => |node| node.firstToken(),4084 \\ .Explicit => |node| node.firstToken(),
4085// \\ .InferErrorSet => |node| tree.prevToken(node.firstToken()),4085 \\ .InferErrorSet => |node| tree.prevToken(node.firstToken()),
4086// \\ .Invalid => unreachable,4086 \\ .Invalid => unreachable,
4087// \\});4087 \\});
4088// \\4088 \\
4089// );4089 );
4090//}4090}
40914091
4092test "zig fmt: single argument trailing commas in @builtins()" {4092test "zig fmt: single argument trailing commas in @builtins()" {
4093 try testCanonical(4093 try testCanonical(
lib/std/zig/render.zig+23-9
...@@ -1291,6 +1291,12 @@ fn renderBuiltinCall(...@@ -1291,6 +1291,12 @@ fn renderBuiltinCall(
1291 try renderToken(ais, tree, builtin_token + 1, .none); // (1291 try renderToken(ais, tree, builtin_token + 1, .none); // (
12921292
1293 for (params) |param_node, i| {1293 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 }
1294 try renderExpression(gpa, ais, tree, param_node, .none);1300 try renderExpression(gpa, ais, tree, param_node, .none);
12951301
1296 if (i + 1 < params.len) {1302 if (i + 1 < params.len) {
...@@ -1733,12 +1739,8 @@ fn renderArrayInit(...@@ -1733,12 +1739,8 @@ fn renderArrayInit(
17331739
1734 const maybe_comma = expr_last_token + 1;1740 const maybe_comma = expr_last_token + 1;
1735 if (token_tags[maybe_comma] == .comma) {1741 if (token_tags[maybe_comma] == .comma) {
1736 const after_comma_src = tree.source[token_starts[maybe_comma]..token_starts[maybe_comma + 1]];1742 if (hasSameLineComment(tree, maybe_comma))
1737 for (after_comma_src) |byte| switch (byte) {1743 break :sec_end i - this_line_size.? + 1;
1738 '\n' => break,
1739 '/' => break :sec_end i - this_line_size.? + 1,
1740 else => continue,
1741 };
1742 }1744 }
1743 }1745 }
1744 break :sec_end row_exprs.len;1746 break :sec_end row_exprs.len;
...@@ -2106,9 +2108,10 @@ fn renderCall(...@@ -2106,9 +2108,10 @@ fn renderCall(
2106 try renderToken(ais, tree, lparen, .none); // (2108 try renderToken(ais, tree, lparen, .none); // (
21072109
2108 for (params) |param_node, i| {2110 for (params) |param_node, i| {
2109 const this_multiline_string =2111 const first_param_token = tree.firstToken(param_node);
2110 token_tags[tree.firstToken(param_node)] == .multiline_string_literal_line;2112 if (token_tags[first_param_token] == .multiline_string_literal_line or
2111 if (this_multiline_string) {2113 hasSameLineComment(tree, first_param_token - 1))
2114 {
2112 ais.pushIndentOneShot();2115 ais.pushIndentOneShot();
2113 }2116 }
2114 try renderExpression(gpa, ais, tree, param_node, .none);2117 try renderExpression(gpa, ais, tree, param_node, .none);
...@@ -2339,6 +2342,17 @@ fn tokenSliceForRender(tree: ast.Tree, token_index: ast.TokenIndex) []const u8 {...@@ -2339,6 +2342,17 @@ fn tokenSliceForRender(tree: ast.Tree, token_index: ast.TokenIndex) []const u8 {
2339 return ret;2342 return ret;
2340}2343}
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
2342fn writeFixingWhitespace(writer: std.ArrayList(u8).Writer, slice: []const u8) Error!void {2356fn writeFixingWhitespace(writer: std.ArrayList(u8).Writer, slice: []const u8) Error!void {
2343 for (slice) |byte| switch (byte) {2357 for (slice) |byte| switch (byte) {
2344 '\t' => try writer.writeAll(" " ** 4),2358 '\t' => try writer.writeAll(" " ** 4),