authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-02-12 17:41:12+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-13 16:57:34-08:00
log75ba8d8db6d3c067fd8f9c8d32e6fea7c3b9343b
tree7ba200ee782ebfb7103ddd3a09cce1a705e96f8c
parent7630a5c566b106b6325a55f29eb1ed9e584d0949

zig fmt: remove empty lines at start/end of block


2 files changed, 217 insertions(+), 121 deletions(-)

lib/std/zig/parser_test.zig+62
......@@ -239,6 +239,68 @@ test "zig fmt: container declaration, transform trailing comma" {
239239 );
240240}
241241
242test "zig fmt: remove empty lines at start/end of container decl" {
243 try testTransform(
244 \\const X = struct {
245 \\
246 \\ foo: i32,
247 \\
248 \\ bar: i8,
249 \\
250 \\};
251 \\
252 ,
253 \\const X = struct {
254 \\ foo: i32,
255 \\
256 \\ bar: i8,
257 \\};
258 \\
259 );
260}
261
262test "zig fmt: remove empty lines at start/end of block" {
263 try testTransform(
264 \\test {
265 \\
266 \\ if (foo) {
267 \\ foo();
268 \\ }
269 \\
270 \\}
271 \\
272 ,
273 \\test {
274 \\ if (foo) {
275 \\ foo();
276 \\ }
277 \\}
278 \\
279 );
280}
281
282test "zig fmt: allow empty line before commment at start of block" {
283 try testCanonical(
284 \\test {
285 \\
286 \\ // foo
287 \\ const x = 42;
288 \\}
289 \\
290 );
291}
292
293test "zig fmt: allow empty line before commment at start of block" {
294 try testCanonical(
295 \\test {
296 \\
297 \\ // foo
298 \\ const x = 42;
299 \\}
300 \\
301 );
302}
303
242304test "zig fmt: trailing comma in fn parameter list" {
243305 try testCanonical(
244306 \\pub fn f(
lib/std/zig/render.zig+155-121
......@@ -24,51 +24,21 @@ pub fn renderTree(buffer: *std.ArrayList(u8), tree: ast.Tree) Error!void {
2424 const ais = &auto_indenting_stream;
2525
2626 // Render all the line comments at the beginning of the file.
27 const src_start: usize = if (mem.startsWith(u8, tree.source, "\xEF\xBB\xBF")) 3 else 0;
2827 const comment_end_loc: usize = tree.tokens.items(.start)[0];
29 _ = try renderCommentsAndNewlines(ais, tree, src_start, comment_end_loc);
28 _ = try renderComments(ais, tree, 0, comment_end_loc);
3029
31 for (tree.rootDecls()) |decl| {
32 try renderMember(ais, tree, decl, .newline);
33 }
30 try renderMembers(ais, tree, tree.rootDecls());
3431}
3532
36/// Assumes that start is the first byte past the previous token and
37/// that end is the last byte before the next token.
38fn renderCommentsAndNewlines(ais: *Ais, tree: ast.Tree, start: usize, end: usize) Error!bool {
39 var index: usize = start;
40 while (mem.indexOf(u8, tree.source[index..end], "//")) |offset| {
41 const comment_start = index + offset;
42 const newline = comment_start +
43 mem.indexOfScalar(u8, tree.source[comment_start..end], '\n').?;
44 const untrimmed_comment = tree.source[comment_start..newline];
45 const trimmed_comment = mem.trimRight(u8, untrimmed_comment, " \r\t");
46
47 // Leave up to one empty line before the comment
48 if (index == start and mem.containsAtLeast(u8, tree.source[index..comment_start], 2, "\n")) {
49 try ais.insertNewline();
50 try ais.insertNewline();
51 } else if (mem.indexOfScalar(u8, tree.source[index..comment_start], '\n') != null) {
52 // Respect the newline directly before the comment. This allows an
53 // empty line between comments
54 try ais.insertNewline();
55 } else if (index == start and start != 0) {
56 // If the comment is on the same line as the token before it,
57 // prefix it with a single space
58 try ais.writer().writeByte(' ');
59 }
60
61 try ais.writer().print("{s}\n", .{trimmed_comment});
62 index = newline + 1;
63 }
64
65 // Leave up to one empty line if present in the source
66 if (index > start) index -= 1;
67 if (end != tree.source.len and mem.containsAtLeast(u8, tree.source[index..end], 2, "\n")) {
68 try ais.insertNewline();
33/// Render all members in the given slice, keeping empty lines where appropriate
34fn renderMembers(ais: *Ais, tree: ast.Tree, members: []const ast.Node.Index) Error!void {
35 if (members.len == 0) return;
36 //try renderExtraNewline(ais, tree, members[0]);
37 try renderMember(ais, tree, members[0], .newline);
38 for (members[1..]) |member| {
39 try renderExtraNewline(ais, tree, member);
40 try renderMember(ais, tree, member, .newline);
6941 }
70
71 return index != start;
7242}
7343
7444fn renderMember(ais: *Ais, tree: ast.Tree, decl: ast.Node.Index, space: Space) Error!void {
......@@ -157,6 +127,16 @@ fn renderMember(ais: *Ais, tree: ast.Tree, decl: ast.Node.Index, space: Space) E
157127 }
158128}
159129
130/// Render all expressions in the slice, keeping empty lines where appropriate
131fn renderExpressions(ais: *Ais, tree: ast.Tree, expressions: []const ast.Node.Index, space: Space) Error!void {
132 if (expressions.len == 0) return;
133 try renderExpression(ais, tree, expressions[0], space);
134 for (expressions[1..]) |expression| {
135 try renderExtraNewline(ais, tree, expression);
136 try renderExpression(ais, tree, expression, space);
137 }
138}
139
160140fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Space) Error!void {
161141 const token_tags = tree.tokens.items(.tag);
162142 const main_tokens = tree.nodes.items(.main_token);
......@@ -501,7 +481,6 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac
501481 .tagged_union_enum_tag_comma,
502482 => return renderContainerDecl(ais, tree, tree.taggedUnionEnumTag(node), space),
503483
504 // TODO: handle comments properly
505484 .error_set_decl => {
506485 const error_token = main_tokens[node];
507486 const lbrace = error_token + 1;
......@@ -521,10 +500,11 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac
521500 return renderToken(ais, tree, rbrace, space);
522501 } else if (token_tags[rbrace - 1] == .comma) {
523502 // There is a trailing comma so render each member on a new line.
503 ais.pushIndentNextLine();
524504 try renderToken(ais, tree, lbrace, .newline);
525 ais.pushIndent();
526505 var i = lbrace + 1;
527506 while (i < rbrace) : (i += 1) {
507 if (i > lbrace + 1) try renderExtraNewlineToken(ais, tree, i);
528508 switch (token_tags[i]) {
529509 .doc_comment => try renderToken(ais, tree, i, .newline),
530510 .identifier => try renderToken(ais, tree, i, .comma),
......@@ -603,14 +583,12 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac
603583 try renderExpression(ais, tree, condition, .none); // condtion expression
604584 try renderToken(ais, tree, rparen, .space); // rparen
605585
586 ais.pushIndentNextLine();
606587 if (cases.len == 0) {
607588 try renderToken(ais, tree, rparen + 1, .none); // lbrace
608 return renderToken(ais, tree, rparen + 2, space); // rbrace
609 }
610 ais.pushIndentNextLine();
611 try renderToken(ais, tree, rparen + 1, .newline); // lbrace
612 for (cases) |case| {
613 try renderExpression(ais, tree, case, .comma);
589 } else {
590 try renderToken(ais, tree, rparen + 1, .newline); // lbrace
591 try renderExpressions(ais, tree, cases, .comma);
614592 }
615593 ais.popIndent();
616594 return renderToken(ais, tree, tree.lastToken(node), space); // rbrace
......@@ -1439,9 +1417,7 @@ fn renderSwitchCase(
14391417 try renderExpression(ais, tree, switch_case.ast.values[0], .space);
14401418 } else if (trailing_comma) {
14411419 // Render each value on a new line
1442 for (switch_case.ast.values) |value_expr| {
1443 try renderExpression(ais, tree, value_expr, .comma);
1444 }
1420 try renderExpressions(ais, tree, switch_case.ast.values, .comma);
14451421 } else {
14461422 // Render on one line
14471423 for (switch_case.ast.values) |value_expr| {
......@@ -1486,22 +1462,20 @@ fn renderBlock(
14861462 try renderToken(ais, tree, lbrace - 1, .space);
14871463 }
14881464
1465 ais.pushIndentNextLine();
14891466 if (statements.len == 0) {
1490 ais.pushIndentNextLine();
14911467 try renderToken(ais, tree, lbrace, .none);
1492 ais.popIndent();
1493 return renderToken(ais, tree, lbrace + 1, space); // rbrace
1494 }
1495
1496 ais.pushIndentNextLine();
1497 try renderToken(ais, tree, lbrace, .newline);
1498 for (statements) |stmt, i| {
1499 switch (node_tags[stmt]) {
1500 .global_var_decl => try renderVarDecl(ais, tree, tree.globalVarDecl(stmt)),
1501 .local_var_decl => try renderVarDecl(ais, tree, tree.localVarDecl(stmt)),
1502 .simple_var_decl => try renderVarDecl(ais, tree, tree.simpleVarDecl(stmt)),
1503 .aligned_var_decl => try renderVarDecl(ais, tree, tree.alignedVarDecl(stmt)),
1504 else => try renderExpression(ais, tree, stmt, .semicolon),
1468 } else {
1469 try renderToken(ais, tree, lbrace, .newline);
1470 for (statements) |stmt, i| {
1471 if (i != 0) try renderExtraNewline(ais, tree, stmt);
1472 switch (node_tags[stmt]) {
1473 .global_var_decl => try renderVarDecl(ais, tree, tree.globalVarDecl(stmt)),
1474 .local_var_decl => try renderVarDecl(ais, tree, tree.localVarDecl(stmt)),
1475 .simple_var_decl => try renderVarDecl(ais, tree, tree.simpleVarDecl(stmt)),
1476 .aligned_var_decl => try renderVarDecl(ais, tree, tree.alignedVarDecl(stmt)),
1477 else => try renderExpression(ais, tree, stmt, .semicolon),
1478 }
15051479 }
15061480 }
15071481 ais.popIndent();
......@@ -1530,11 +1504,17 @@ fn renderStructInit(
15301504 const last_field_token = tree.lastToken(last_field);
15311505 if (token_tags[last_field_token + 1] == .comma) {
15321506 // Render one field init per line.
1533 ais.pushIndent();
1507 ais.pushIndentNextLine();
15341508 try renderToken(ais, tree, struct_init.ast.lbrace, .newline);
15351509
1536 for (struct_init.ast.fields) |field_init| {
1510 try renderToken(ais, tree, struct_init.ast.lbrace + 1, .none); // .
1511 try renderToken(ais, tree, struct_init.ast.lbrace + 2, .space); // name
1512 try renderToken(ais, tree, struct_init.ast.lbrace + 3, .space); // =
1513 try renderExpression(ais, tree, struct_init.ast.fields[0], .comma);
1514
1515 for (struct_init.ast.fields[1..]) |field_init| {
15371516 const init_token = tree.firstToken(field_init);
1517 try renderExtraNewlineToken(ais, tree, init_token - 3);
15381518 try renderToken(ais, tree, init_token - 3, .none); // .
15391519 try renderToken(ais, tree, init_token - 2, .space); // name
15401520 try renderToken(ais, tree, init_token - 1, .space); // =
......@@ -1573,20 +1553,18 @@ fn renderArrayInit(
15731553 try renderExpression(ais, tree, array_init.ast.type_expr, .none); // T
15741554 }
15751555 if (array_init.ast.elements.len == 0) {
1556 ais.pushIndentNextLine();
15761557 try renderToken(ais, tree, array_init.ast.lbrace, .none); // lbrace
1558 ais.popIndent();
15771559 return renderToken(ais, tree, array_init.ast.lbrace + 1, space); // rbrace
15781560 }
15791561 const last_elem = array_init.ast.elements[array_init.ast.elements.len - 1];
15801562 const last_elem_token = tree.lastToken(last_elem);
15811563 if (token_tags[last_elem_token + 1] == .comma) {
15821564 // Render one element per line.
1583 ais.pushIndent();
1565 ais.pushIndentNextLine();
15841566 try renderToken(ais, tree, array_init.ast.lbrace, .newline);
1585
1586 for (array_init.ast.elements) |elem| {
1587 try renderExpression(ais, tree, elem, .comma);
1588 }
1589
1567 try renderExpressions(ais, tree, array_init.ast.elements, .comma);
15901568 ais.popIndent();
15911569 return renderToken(ais, tree, last_elem_token + 2, space); // rbrace
15921570 } else {
......@@ -1679,11 +1657,9 @@ fn renderContainerDecl(
16791657 }
16801658
16811659 // One member per line.
1682 ais.pushIndent();
1660 ais.pushIndentNextLine();
16831661 try renderToken(ais, tree, lbrace, .newline); // lbrace
1684 for (container_decl.ast.members) |member| {
1685 try renderMember(ais, tree, member, .newline);
1686 }
1662 try renderMembers(ais, tree, container_decl.ast.members);
16871663 ais.popIndent();
16881664
16891665 return renderToken(ais, tree, rbrace, space); // rbrace
......@@ -1745,6 +1721,7 @@ fn renderAsm(
17451721
17461722 const comma = tree.firstToken(next_asm_output) - 1;
17471723 try renderToken(ais, tree, comma, .newline); // ,
1724 try renderExtraNewlineToken(ais, tree, tree.firstToken(next_asm_output));
17481725 } else if (asm_node.inputs.len == 0 and asm_node.first_clobber == null) {
17491726 try renderAsmOutput(ais, tree, asm_output, .newline);
17501727 ais.popIndent();
......@@ -1776,6 +1753,7 @@ fn renderAsm(
17761753
17771754 const first_token = tree.firstToken(next_asm_input);
17781755 try renderToken(ais, tree, first_token - 1, .newline); // ,
1756 try renderExtraNewlineToken(ais, tree, first_token);
17791757 } else if (asm_node.first_clobber == null) {
17801758 try renderAsmInput(ais, tree, asm_input, .newline);
17811759 ais.popIndent();
......@@ -1834,7 +1812,9 @@ fn renderCall(
18341812 const lparen = call.ast.lparen;
18351813 const params = call.ast.params;
18361814 if (params.len == 0) {
1815 ais.pushIndentNextLine();
18371816 try renderToken(ais, tree, lparen, .none);
1817 ais.popIndent();
18381818 return renderToken(ais, tree, lparen + 1, space); // )
18391819 }
18401820
......@@ -1856,6 +1836,8 @@ fn renderCall(
18561836 try renderToken(ais, tree, comma, Space.newline); // ,
18571837
18581838 if (is_multiline_string) ais.pushIndent();
1839
1840 try renderExtraNewline(ais, tree, params[i + 1]);
18591841 } else {
18601842 try renderExpression(ais, tree, param_node, Space.comma);
18611843 }
......@@ -1928,44 +1910,100 @@ fn renderToken(ais: *Ais, tree: ast.Tree, token_index: ast.TokenIndex, space: Sp
19281910 const lexeme = tree.tokenSlice(token_index);
19291911 try ais.writer().writeAll(lexeme);
19301912
1913 if (space == .no_comment) return;
1914
1915 const comment = try renderComments(ais, tree, token_start + lexeme.len, token_starts[token_index + 1]);
19311916 switch (space) {
1932 .no_comment => {},
1933 .none => _ = try renderCommentsAndNewlines(ais, tree, token_start + lexeme.len, token_starts[token_index + 1]),
1934 .comma => {
1935 const comment = try renderCommentsAndNewlines(ais, tree, token_start + lexeme.len, token_starts[token_index + 1]);
1936 if (token_tags[token_index + 1] == .comma) {
1937 return renderToken(ais, tree, token_index + 1, .newline);
1938 } else if (!comment) {
1939 return ais.insertNewline();
1940 }
1941 },
1942 .comma_space => {
1943 const comment = try renderCommentsAndNewlines(ais, tree, token_start + lexeme.len, token_starts[token_index + 1]);
1944 if (token_tags[token_index + 1] == .comma) {
1945 return renderToken(ais, tree, token_index + 1, .space);
1946 } else if (!comment) {
1947 return ais.writer().writeByte(' ');
1948 }
1917 .none => {},
1918 .space => if (!comment) try ais.writer().writeByte(' '),
1919 .newline => if (!comment) try ais.insertNewline(),
1920
1921 .comma => if (token_tags[token_index + 1] == .comma) {
1922 try renderToken(ais, tree, token_index + 1, .newline);
1923 } else if (!comment) {
1924 try ais.insertNewline();
19491925 },
1950 .semicolon => {
1951 const comment = try renderCommentsAndNewlines(ais, tree, token_start + lexeme.len, token_starts[token_index + 1]);
1952 if (token_tags[token_index + 1] == .semicolon) {
1953 return renderToken(ais, tree, token_index + 1, .newline);
1954 } else if (!comment) {
1955 return ais.insertNewline();
1956 }
1926
1927 .comma_space => if (token_tags[token_index + 1] == .comma) {
1928 try renderToken(ais, tree, token_index + 1, .space);
1929 } else if (!comment) {
1930 try ais.writer().writeByte(' ');
19571931 },
1958 .space => {
1959 const comment = try renderCommentsAndNewlines(ais, tree, token_start + lexeme.len, token_starts[token_index + 1]);
1960 if (!comment) {
1961 return ais.writer().writeByte(' ');
1962 }
1932
1933 .semicolon => if (token_tags[token_index + 1] == .semicolon) {
1934 try renderToken(ais, tree, token_index + 1, .newline);
1935 } else if (!comment) {
1936 try ais.insertNewline();
19631937 },
1964 .newline => {
1965 if (!try renderCommentsAndNewlines(ais, tree, token_start + lexeme.len, token_starts[token_index + 1])) {
1938
1939 .no_comment => unreachable,
1940 }
1941}
1942
1943/// Assumes that start is the first byte past the previous token and
1944/// that end is the last byte before the next token.
1945fn renderComments(ais: *Ais, tree: ast.Tree, start: usize, end: usize) Error!bool {
1946 var index: usize = start;
1947 while (mem.indexOf(u8, tree.source[index..end], "//")) |offset| {
1948 const comment_start = index + offset;
1949 const newline = comment_start +
1950 mem.indexOfScalar(u8, tree.source[comment_start..end], '\n').?;
1951 const untrimmed_comment = tree.source[comment_start..newline];
1952 const trimmed_comment = mem.trimRight(u8, untrimmed_comment, &std.ascii.spaces);
1953
1954 // Don't leave any whitespace at the start of the file
1955 if (index != 0) {
1956 if (index == start and mem.containsAtLeast(u8, tree.source[index..comment_start], 2, "\n")) {
1957 // Leave up to one empty line before the first comment
1958 try ais.insertNewline();
19661959 try ais.insertNewline();
1960 } else if (mem.indexOfScalar(u8, tree.source[index..comment_start], '\n') != null) {
1961 // Respect the newline directly before the comment.
1962 // Note: This allows an empty line between comments
1963 try ais.insertNewline();
1964 } else if (index == start) {
1965 // Otherwise if the first comment is on the same line as
1966 // the token before it, prefix it with a single space.
1967 try ais.writer().writeByte(' ');
19671968 }
1968 },
1969 }
1970
1971 try ais.writer().print("{s}\n", .{trimmed_comment});
1972 index = newline + 1;
1973 }
1974
1975 if (index != start and mem.containsAtLeast(u8, tree.source[index - 1 .. end], 2, "\n")) {
1976 try ais.insertNewline();
1977 }
1978
1979 return index != start;
1980}
1981
1982fn renderExtraNewline(ais: *Ais, tree: ast.Tree, node: ast.Node.Index) Error!void {
1983 return renderExtraNewlineToken(ais, tree, tree.firstToken(node));
1984}
1985
1986/// Check if there is an empty line immediately before the given token. If so, render it.
1987fn renderExtraNewlineToken(ais: *Ais, tree: ast.Tree, token_index: ast.TokenIndex) Error!void {
1988 const token_starts = tree.tokens.items(.start);
1989 const token_start = token_starts[token_index];
1990 if (token_start == 0) return;
1991 const prev_token_end = if (token_index == 0)
1992 0
1993 else
1994 token_starts[token_index - 1] + tree.tokenSlice(token_index - 1).len;
1995
1996 // If there is a comment present, it will handle the empty line
1997 if (mem.indexOf(u8, tree.source[prev_token_end..token_start], "//") != null) return;
1998
1999 // Iterate backwards to the end of the previous token, stopping if a
2000 // non-whitespace character is encountered or two newlines have been found.
2001 var i = token_start - 1;
2002 var newlines: u2 = 0;
2003 while (std.ascii.isSpace(tree.source[i])) : (i -= 1) {
2004 if (tree.source[i] == '\n') newlines += 1;
2005 if (newlines == 2) return ais.insertNewline();
2006 if (i == prev_token_end) break;
19692007 }
19702008}
19712009
......@@ -1983,19 +2021,15 @@ fn renderDocComments(ais: *Ais, tree: ast.Tree, end_token: ast.TokenIndex) Error
19832021 tok += 1;
19842022 }
19852023 const first_tok = tok;
1986 if (tok == end_token) return;
2024 if (first_tok == end_token) return;
2025 try renderExtraNewlineToken(ais, tree, first_tok);
19872026
1988 while (true) : (tok += 1) {
1989 switch (token_tags[tok]) {
1990 .doc_comment => {
1991 if (first_tok < end_token) {
1992 try renderToken(ais, tree, tok, .newline);
1993 } else {
1994 try renderToken(ais, tree, tok, .no_comment);
1995 try ais.insertNewline();
1996 }
1997 },
1998 else => break,
2027 while (token_tags[tok] == .doc_comment) : (tok += 1) {
2028 if (first_tok < end_token) {
2029 try renderToken(ais, tree, tok, .newline);
2030 } else {
2031 try renderToken(ais, tree, tok, .no_comment);
2032 try ais.insertNewline();
19992033 }
20002034 }
20012035}