authorgravatar for codroid@gmail.comhryx <codroid@gmail.com> 2019-03-31 00:47:42-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-31 14:27:10-04:00
loga4afacd1822cea75196b27a8a5fe30bd252eccae
treeb2a7c82e015e6e2e3274b942b219cfed906eb812
parent25ac2fe8cd817977f3d5a8677cbe067ad2684d66

Veritcally align array literal columns


2 files changed, 64 insertions(+), 6 deletions(-)

std/zig/parser_test.zig+28
...@@ -470,6 +470,34 @@ test "zig fmt: array literal with hint" {...@@ -470,6 +470,34 @@ test "zig fmt: array literal with hint" {
470 );470 );
471}471}
472472
473test "zig fmt: array literal veritical column alignment" {
474 try testTransform(
475 \\const a = []u8{
476 \\ 1000, 200,
477 \\ 30, 4,
478 \\ 50000, 60
479 \\};
480 \\const a = []u8{0, 1, 2, 3, 40,
481 \\ 4,5,600,7,
482 \\ 80,
483 \\ 9, 10, 11, 0, 13, 14, 15};
484 \\
485 ,
486 \\const a = []u8{
487 \\ 1000, 200,
488 \\ 30, 4,
489 \\ 50000, 60,
490 \\};
491 \\const a = []u8{
492 \\ 0, 1, 2, 3, 40,
493 \\ 4, 5, 600, 7, 80,
494 \\ 9, 10, 11, 0, 13,
495 \\ 14, 15,
496 \\};
497 \\
498 );
499}
500
473test "zig fmt: multiline string with backslash at end of line" {501test "zig fmt: multiline string with backslash at end of line" {
474 try testCanonical(502 try testCanonical(
475 \\comptime {503 \\comptime {
std/zig/render.zig+36-6
...@@ -717,24 +717,54 @@ fn renderExpression(...@@ -717,24 +717,54 @@ fn renderExpression(
717 };717 };
718718
719 if (maybe_row_size) |row_size| {719 if (maybe_row_size) |row_size| {
720 // A place to store the width of each expression and its column's maximum
721 var widths = try allocator.alloc(usize, exprs.len + row_size);
722 defer allocator.free(widths);
723 mem.set(usize, widths, 0);
724
725 var expr_widths = widths[0 .. widths.len - row_size];
726 var column_widths = widths[widths.len - row_size ..];
727
728 // Null stream for counting the printed length of each expression
729 var null_stream = std.io.NullOutStream.init();
730 var counting_stream = std.io.CountingOutStream(std.io.NullOutStream.Error).init(&null_stream.stream);
731
732 var it = exprs.iterator(0);
733 var i: usize = 0;
734
735 while (it.next()) |expr| : (i += 1) {
736 counting_stream.bytes_written = 0;
737 var dummy_col: usize = 0;
738 try renderExpression(allocator, &counting_stream.stream, tree, 0, &dummy_col, expr.*, Space.None);
739 const width = counting_stream.bytes_written;
740 const col = i % row_size;
741 column_widths[col] = std.math.max(column_widths[col], width);
742 expr_widths[i] = width;
743 }
744
720 const new_indent = indent + indent_delta;745 const new_indent = indent + indent_delta;
721 try renderToken(tree, stream, lbrace, new_indent, start_col, Space.Newline);746 try renderToken(tree, stream, lbrace, new_indent, start_col, Space.Newline);
722 try stream.writeByteNTimes(' ', new_indent);747 try stream.writeByteNTimes(' ', new_indent);
723748
724 var it = exprs.iterator(0);749 it.set(0);
725 var i: usize = 1;750 i = 0;
726 while (it.next()) |expr| {751 var col: usize = 1;
752 while (it.next()) |expr| : (i += 1) {
727 if (it.peek()) |next_expr| {753 if (it.peek()) |next_expr| {
728 try renderExpression(allocator, stream, tree, new_indent, start_col, expr.*, Space.None);754 try renderExpression(allocator, stream, tree, new_indent, start_col, expr.*, Space.None);
729755
730 const comma = tree.nextToken(expr.*.lastToken());756 const comma = tree.nextToken(expr.*.lastToken());
731757
732 if (i != row_size) {758 if (col != row_size) {
733 try renderToken(tree, stream, comma, new_indent, start_col, Space.Space); // ,759 try renderToken(tree, stream, comma, new_indent, start_col, Space.Space); // ,
734 i += 1;760
761 const padding = column_widths[i % row_size] - expr_widths[i];
762 try stream.writeByteNTimes(' ', padding);
763
764 col += 1;
735 continue;765 continue;
736 }766 }
737 i = 1;767 col = 1;
738768
739 try renderToken(tree, stream, comma, new_indent, start_col, Space.Newline); // ,769 try renderToken(tree, stream, comma, new_indent, start_col, Space.Newline); // ,
740770