From 7495fd8cb9eea3d571ec62eff069b443ac0e3a6a Mon Sep 17 00:00:00 2001 From: Vexu <15308111+Vexu@users.noreply.github.com> Date: Tue, 15 Oct 2019 14:06:08 +0300 Subject: [PATCH] fix struct align fmt --- lib/std/zig/parser_test.zig | 6 ++++++ lib/std/zig/render.zig | 18 +++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig index 2c7978ba83..78f80786c5 100644 --- a/lib/std/zig/parser_test.zig +++ b/lib/std/zig/parser_test.zig @@ -173,6 +173,12 @@ test "zig fmt: aligned struct field" { \\}; \\ ); + try testCanonical( + \\pub const S = struct { + \\ f: i32 align(32) = 1, + \\}; + \\ + ); } test "zig fmt: preserve space between async fn definitions" { diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig index 6268a056f5..1be4410f38 100644 --- a/lib/std/zig/render.zig +++ b/lib/std/zig/render.zig @@ -272,7 +272,7 @@ fn renderTopLevelDecl(allocator: *mem.Allocator, stream: var, tree: *ast.Tree, i try renderToken(tree, stream, align_kw, indent, start_col, Space.None); // align try renderToken(tree, stream, lparen_token, indent, start_col, Space.None); // ( try renderExpression(allocator, stream, tree, indent, start_col, align_value_expr, Space.None); // alignment - try renderToken(tree, stream, rparen_token, indent, start_col, Space.Comma); // ) + try renderToken(tree, stream, rparen_token, indent, start_col, Space.Comma); // ), } else { try renderExpression(allocator, stream, tree, indent, start_col, field.type_expr.?, Space.Comma); // type, } @@ -283,8 +283,20 @@ fn renderTopLevelDecl(allocator: *mem.Allocator, stream: var, tree: *ast.Tree, i } else { try renderToken(tree, stream, field.name_token, indent, start_col, Space.None); // name try renderToken(tree, stream, tree.nextToken(field.name_token), indent, start_col, Space.Space); // : - try renderExpression(allocator, stream, tree, indent, start_col, field.type_expr.?, Space.Space); // type - try renderToken(tree, stream, tree.nextToken(field.type_expr.?.lastToken()), indent, start_col, Space.Space); // = + + if (field.align_expr) |align_value_expr| { + try renderExpression(allocator, stream, tree, indent, start_col, field.type_expr.?, Space.Space); // type + const lparen_token = tree.prevToken(align_value_expr.firstToken()); + const align_kw = tree.prevToken(lparen_token); + const rparen_token = tree.nextToken(align_value_expr.lastToken()); + try renderToken(tree, stream, align_kw, indent, start_col, Space.None); // align + try renderToken(tree, stream, lparen_token, indent, start_col, Space.None); // ( + try renderExpression(allocator, stream, tree, indent, start_col, align_value_expr, Space.None); // alignment + try renderToken(tree, stream, rparen_token, indent, start_col, Space.Space); // ) + } else { + try renderExpression(allocator, stream, tree, indent, start_col, field.type_expr.?, Space.Space); // type + } + try renderToken(tree, stream, tree.prevToken(field.value_expr.?.firstToken()), indent, start_col, Space.Space); // = return renderExpression(allocator, stream, tree, indent, start_col, field.value_expr.?, Space.Comma); // value, } },