aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorLemonBoy <thatlemon@gmail.com>2021-04-09 10:29:39 +0200
committerAndrew Kelley <andrew@ziglang.org>2021-04-09 10:03:45 -0700
commit5ebcd8ccafeab0d8ddbdb9723f7e7a50ecc60a5f (patch)
tree68e60d45952d665878c169ef84d46a01e021be99 /lib/std
parentcc525bc002e456cc735f66bc4d2c5267a10b6016 (diff)
downloadzig-5ebcd8ccafeab0d8ddbdb9723f7e7a50ecc60a5f.tar.gz
zig-5ebcd8ccafeab0d8ddbdb9723f7e7a50ecc60a5f.zip
zig fmt: Fix rendering of arrays with single row
rowSize used to return null if all the elements were placed on the same line as the right brace, making the rendering logic skip the whole set of elements. Given the usage of rowSize let's just drop the null and always return the number of elements. Fixes #8423
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/zig/parser_test.zig5
-rw-r--r--lib/std/zig/render.zig12
2 files changed, 11 insertions, 6 deletions
diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig
index 2a343a6edc..321f37c422 100644
--- a/lib/std/zig/parser_test.zig
+++ b/lib/std/zig/parser_test.zig
@@ -1812,6 +1812,8 @@ test "zig fmt: array literal veritical column alignment" {
\\ 4,5,600,7,
\\ 80,
\\ 9, 10, 11, 0, 13, 14, 15};
+ \\const a = [12]u8{
+ \\ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
\\
,
\\const a = []u8{
@@ -1825,6 +1827,9 @@ test "zig fmt: array literal veritical column alignment" {
\\ 9, 10, 11, 0, 13,
\\ 14, 15,
\\};
+ \\const a = [12]u8{
+ \\ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
+ \\};
\\
);
}
diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig
index 7add8383f3..1aa2fe8af6 100644
--- a/lib/std/zig/render.zig
+++ b/lib/std/zig/render.zig
@@ -1653,7 +1653,8 @@ fn renderArrayInit(
try renderToken(ais, tree, array_init.ast.lbrace, .newline);
var expr_index: usize = 0;
- while (rowSize(tree, array_init.ast.elements[expr_index..], rbrace)) |row_size| {
+ while (true) {
+ const row_size = rowSize(tree, array_init.ast.elements[expr_index..], rbrace);
const row_exprs = array_init.ast.elements[expr_index..];
// A place to store the width of each expression and its column's maximum
const widths = try gpa.alloc(usize, row_exprs.len + row_size);
@@ -1686,7 +1687,7 @@ fn renderArrayInit(
const maybe_comma = expr_last_token + 1;
if (token_tags[maybe_comma] == .comma) {
if (hasSameLineComment(tree, maybe_comma))
- break :sec_end i - this_line_size.? + 1;
+ break :sec_end i - this_line_size + 1;
}
}
break :sec_end row_exprs.len;
@@ -2500,9 +2501,8 @@ fn nodeCausesSliceOpSpace(tag: ast.Node.Tag) bool {
};
}
-// Returns the number of nodes in `expr` that are on the same line as `rtoken`,
-// or null if they all are on the same line.
-fn rowSize(tree: ast.Tree, exprs: []const ast.Node.Index, rtoken: ast.TokenIndex) ?usize {
+// Returns the number of nodes in `expr` that are on the same line as `rtoken`.
+fn rowSize(tree: ast.Tree, exprs: []const ast.Node.Index, rtoken: ast.TokenIndex) usize {
const token_tags = tree.tokens.items(.tag);
const first_token = tree.firstToken(exprs[0]);
@@ -2510,7 +2510,7 @@ fn rowSize(tree: ast.Tree, exprs: []const ast.Node.Index, rtoken: ast.TokenIndex
const maybe_comma = rtoken - 1;
if (token_tags[maybe_comma] == .comma)
return 1;
- return null; // no newlines
+ return exprs.len; // no newlines
}
var count: usize = 1;