Commit a6038f0375

Andrew Kelley <andrew@ziglang.org>
2021-02-23 02:17:26
zig fmt: function params should align nicely
1 parent fec51ad
Changed files (2)
lib/std/zig/parser_test.zig
@@ -3726,22 +3726,22 @@ test "zig fmt: C var args" {
     );
 }
 
-//test "zig fmt: Only indent multiline string literals in function calls" {
-//    try testCanonical(
-//        \\test "zig fmt:" {
-//        \\    try testTransform(
-//        \\        \\const X = struct {
-//        \\        \\    foo: i32, bar: i8 };
-//        \\    ,
-//        \\        \\const X = struct {
-//        \\        \\    foo: i32, bar: i8
-//        \\        \\};
-//        \\        \\
-//        \\    );
-//        \\}
-//        \\
-//    );
-//}
+test "zig fmt: Only indent multiline string literals in function calls" {
+    try testCanonical(
+        \\test "zig fmt:" {
+        \\    try testTransform(
+        \\        \\const X = struct {
+        \\        \\    foo: i32, bar: i8 };
+        \\    ,
+        \\        \\const X = struct {
+        \\        \\    foo: i32, bar: i8
+        \\        \\};
+        \\        \\
+        \\    );
+        \\}
+        \\
+    );
+}
 
 test "zig fmt: Don't add extra newline after if" {
     try testCanonical(
@@ -3806,68 +3806,66 @@ test "zig fmt: comments in ternary ifs" {
 //        \\
 //    );
 //}
-//
-//test "zig fmt: Indent comma correctly after multiline string literals in arg list (trailing comma)" {
-//    try testCanonical(
-//        \\fn foo() void {
-//        \\    z.display_message_dialog(
-//        \\        *const [323:0]u8,
-//        \\        \\Message Text
-//        \\        \\------------
-//        \\        \\xxxxxxxxxxxx
-//        \\        \\xxxxxxxxxxxx
-//        \\    ,
-//        \\        g.GtkMessageType.GTK_MESSAGE_WARNING,
-//        \\        null,
-//        \\    );
-//        \\
-//        \\    z.display_message_dialog(*const [323:0]u8,
-//        \\        \\Message Text
-//        \\        \\------------
-//        \\        \\xxxxxxxxxxxx
-//        \\        \\xxxxxxxxxxxx
-//        \\    , g.GtkMessageType.GTK_MESSAGE_WARNING, null);
-//        \\}
-//        \\
-//    );
-//}
 
-//test "zig fmt: Control flow statement as body of blockless if" {
-//    try testCanonical(
-//        \\pub fn main() void {
-//        \\    const zoom_node = if (focused_node == layout_first)
-//        \\        if (it.next()) {
-//        \\            if (!node.view.pending.float and !node.view.pending.fullscreen) break node;
-//        \\        } else null
-//        \\    else
-//        \\        focused_node;
-//        \\
-//        \\    const zoom_node = if (focused_node == layout_first) while (it.next()) |node| {
-//        \\        if (!node.view.pending.float and !node.view.pending.fullscreen) break node;
-//        \\    } else null else
-//        \\        focused_node;
-//        \\
-//        \\    const zoom_node = if (focused_node == layout_first)
-//        \\        if (it.next()) {
-//        \\            if (!node.view.pending.float and !node.view.pending.fullscreen) break node;
-//        \\        } else null;
-//        \\
-//        \\    const zoom_node = if (focused_node == layout_first) while (it.next()) |node| {
-//        \\        if (!node.view.pending.float and !node.view.pending.fullscreen) break node;
-//        \\    };
-//        \\
-//        \\    const zoom_node = if (focused_node == layout_first) for (nodes) |node| {
-//        \\        break node;
-//        \\    };
-//        \\
-//        \\    const zoom_node = if (focused_node == layout_first) switch (nodes) {
-//        \\        0 => 0,
-//        \\    } else
-//        \\        focused_node;
-//        \\}
-//        \\
-//    );
-//}
+test "zig fmt: Indent comma correctly after multiline string literals in arg list (trailing comma)" {
+    try testCanonical(
+        \\fn foo() void {
+        \\    z.display_message_dialog(
+        \\        *const [323:0]u8,
+        \\        \\Message Text
+        \\        \\------------
+        \\        \\xxxxxxxxxxxx
+        \\        \\xxxxxxxxxxxx
+        \\    ,
+        \\        g.GtkMessageType.GTK_MESSAGE_WARNING,
+        \\        null,
+        \\    );
+        \\
+        \\    z.display_message_dialog(*const [323:0]u8,
+        \\        \\Message Text
+        \\        \\------------
+        \\        \\xxxxxxxxxxxx
+        \\        \\xxxxxxxxxxxx
+        \\    , g.GtkMessageType.GTK_MESSAGE_WARNING, null);
+        \\}
+        \\
+    );
+}
+
+test "zig fmt: Control flow statement as body of blockless if" {
+    try testCanonical(
+        \\pub fn main() void {
+        \\    const zoom_node = if (focused_node == layout_first)
+        \\        if (it.next()) {
+        \\            if (!node.view.pending.float and !node.view.pending.fullscreen) break node;
+        \\        } else null
+        \\    else
+        \\        focused_node;
+        \\
+        \\    const zoom_node = if (focused_node == layout_first) while (it.next()) |node| {
+        \\        if (!node.view.pending.float and !node.view.pending.fullscreen) break node;
+        \\    } else null else focused_node;
+        \\
+        \\    const zoom_node = if (focused_node == layout_first)
+        \\        if (it.next()) {
+        \\            if (!node.view.pending.float and !node.view.pending.fullscreen) break node;
+        \\        } else null;
+        \\
+        \\    const zoom_node = if (focused_node == layout_first) while (it.next()) |node| {
+        \\        if (!node.view.pending.float and !node.view.pending.fullscreen) break node;
+        \\    };
+        \\
+        \\    const zoom_node = if (focused_node == layout_first) for (nodes) |node| {
+        \\        break node;
+        \\    };
+        \\
+        \\    const zoom_node = if (focused_node == layout_first) switch (nodes) {
+        \\        0 => 0,
+        \\    } else focused_node;
+        \\}
+        \\
+    );
+}
 
 test "zig fmt: regression test for #5722" {
     try testCanonical(
@@ -4058,21 +4056,21 @@ test "zig fmt: trailing comma should force multiline 1 column" {
     );
 }
 
-//test "zig fmt: function params should align nicely" {
-//    try testCanonical(
-//        \\pub fn foo() void {
-//        \\    cases.addRuntimeSafety("slicing operator with sentinel",
-//        \\        \\const std = @import("std");
-//        \\        ++ check_panic_msg ++
-//        \\        \\pub fn main() void {
-//        \\        \\    var buf = [4]u8{'a','b','c',0};
-//        \\        \\    const slice = buf[0..:0];
-//        \\        \\}
-//        \\    );
-//        \\}
-//        \\
-//    );
-//}
+test "zig fmt: function params should align nicely" {
+    try testCanonical(
+        \\pub fn foo() void {
+        \\    cases.addRuntimeSafety("slicing operator with sentinel",
+        \\        \\const std = @import("std");
+        \\    ++ check_panic_msg ++
+        \\        \\pub fn main() void {
+        \\        \\    var buf = [4]u8{'a','b','c',0};
+        \\        \\    const slice = buf[0..:0];
+        \\        \\}
+        \\    );
+        \\}
+        \\
+    );
+}
 
 test "zig fmt: error for invalid bit range" {
     try testError(
lib/std/zig/render.zig
@@ -197,11 +197,10 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac
             while (locked_indents > 0) : (locked_indents -= 1) ais.popIndent();
 
             switch (space) {
-                .none => {},
+                .none, .space, .newline => {},
                 .semicolon => if (token_tags[i] == .semicolon) try renderToken(ais, tree, i, .newline),
                 .comma => if (token_tags[i] == .comma) try renderToken(ais, tree, i, .newline),
                 .comma_space => if (token_tags[i] == .comma) try renderToken(ais, tree, i, .space),
-                else => unreachable,
             }
         },
 
@@ -358,8 +357,8 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac
                 ais.pushIndent();
                 try renderToken(ais, tree, op_token, .newline);
                 ais.popIndent();
-                ais.pushIndentOneShot();
             }
+            ais.pushIndentOneShot();
             return renderExpression(ais, tree, infix.rhs, space);
         },
 
@@ -1868,7 +1867,6 @@ fn renderCall(
     space: Space,
 ) Error!void {
     const token_tags = tree.tokens.items(.tag);
-    const node_tags = tree.nodes.items(.tag);
     const main_tokens = tree.nodes.items(.main_token);
 
     if (call.async_token) |async_token| {
@@ -1895,7 +1893,8 @@ fn renderCall(
                 try renderExpression(ais, tree, param_node, .none);
 
                 // Unindent the comma for multiline string literals.
-                const is_multiline_string = node_tags[param_node] == .multiline_string_literal;
+                const is_multiline_string =
+                    token_tags[tree.firstToken(param_node)] == .multiline_string_literal_line;
                 if (is_multiline_string) ais.popIndent();
 
                 const comma = tree.lastToken(param_node) + 1;
@@ -1915,7 +1914,8 @@ fn renderCall(
     try renderToken(ais, tree, lparen, .none); // (
 
     for (params) |param_node, i| {
-        const this_multiline_string = node_tags[param_node] == .multiline_string_literal;
+        const this_multiline_string =
+            token_tags[tree.firstToken(param_node)] == .multiline_string_literal_line;
         if (this_multiline_string) {
             ais.pushIndentOneShot();
         }
@@ -1923,7 +1923,8 @@ fn renderCall(
 
         if (i + 1 < params.len) {
             const comma = tree.lastToken(param_node) + 1;
-            const next_multiline_string = node_tags[params[i + 1]] == .multiline_string_literal;
+            const next_multiline_string =
+                token_tags[tree.firstToken(params[i + 1])] == .multiline_string_literal_line;
             const comma_space: Space = if (next_multiline_string) .none else .space;
             try renderToken(ais, tree, comma, comma_space);
         }