Skip to content

Commit

Permalink
Veritcally align array literal columns
Browse files Browse the repository at this point in the history
  • Loading branch information
hryx authored and andrewrk committed Mar 31, 2019
1 parent 25ac2fe commit a4afacd
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
28 changes: 28 additions & 0 deletions std/zig/parser_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,34 @@ test "zig fmt: array literal with hint" {
);
}

test "zig fmt: array literal veritical column alignment" {
try testTransform(
\\const a = []u8{
\\ 1000, 200,
\\ 30, 4,
\\ 50000, 60
\\};
\\const a = []u8{0, 1, 2, 3, 40,
\\ 4,5,600,7,
\\ 80,
\\ 9, 10, 11, 0, 13, 14, 15};
\\
,
\\const a = []u8{
\\ 1000, 200,
\\ 30, 4,
\\ 50000, 60,
\\};
\\const a = []u8{
\\ 0, 1, 2, 3, 40,
\\ 4, 5, 600, 7, 80,
\\ 9, 10, 11, 0, 13,
\\ 14, 15,
\\};
\\
);
}

test "zig fmt: multiline string with backslash at end of line" {
try testCanonical(
\\comptime {
Expand Down
42 changes: 36 additions & 6 deletions std/zig/render.zig
Original file line number Diff line number Diff line change
Expand Up @@ -717,24 +717,54 @@ fn renderExpression(
};

if (maybe_row_size) |row_size| {
// A place to store the width of each expression and its column's maximum
var widths = try allocator.alloc(usize, exprs.len + row_size);
defer allocator.free(widths);
mem.set(usize, widths, 0);

var expr_widths = widths[0 .. widths.len - row_size];
var column_widths = widths[widths.len - row_size ..];

// Null stream for counting the printed length of each expression
var null_stream = std.io.NullOutStream.init();
var counting_stream = std.io.CountingOutStream(std.io.NullOutStream.Error).init(&null_stream.stream);

var it = exprs.iterator(0);
var i: usize = 0;

while (it.next()) |expr| : (i += 1) {
counting_stream.bytes_written = 0;
var dummy_col: usize = 0;
try renderExpression(allocator, &counting_stream.stream, tree, 0, &dummy_col, expr.*, Space.None);
const width = counting_stream.bytes_written;
const col = i % row_size;
column_widths[col] = std.math.max(column_widths[col], width);
expr_widths[i] = width;
}

const new_indent = indent + indent_delta;
try renderToken(tree, stream, lbrace, new_indent, start_col, Space.Newline);
try stream.writeByteNTimes(' ', new_indent);

var it = exprs.iterator(0);
var i: usize = 1;
while (it.next()) |expr| {
it.set(0);
i = 0;
var col: usize = 1;
while (it.next()) |expr| : (i += 1) {
if (it.peek()) |next_expr| {
try renderExpression(allocator, stream, tree, new_indent, start_col, expr.*, Space.None);

const comma = tree.nextToken(expr.*.lastToken());

if (i != row_size) {
if (col != row_size) {
try renderToken(tree, stream, comma, new_indent, start_col, Space.Space); // ,
i += 1;

const padding = column_widths[i % row_size] - expr_widths[i];
try stream.writeByteNTimes(' ', padding);

col += 1;
continue;
}
i = 1;
col = 1;

try renderToken(tree, stream, comma, new_indent, start_col, Space.Newline); // ,

Expand Down

0 comments on commit a4afacd

Please sign in to comment.