Commit ec987a7a46

Andrew Kelley <andrew@ziglang.org>
2021-02-23 00:39:50
zig fmt: if condition has line break but must not wrap
1 parent b301999
Changed files (2)
lib/std/zig/parser_test.zig
@@ -1410,31 +1410,31 @@ test "zig fmt: if condition wraps" {
         \\
     );
 }
-//
-//test "zig fmt: if condition has line break but must not wrap" {
-//    try testCanonical(
-//        \\comptime {
-//        \\    if (self.user_input_options.put(
-//        \\        name,
-//        \\        UserInputOption{
-//        \\            .name = name,
-//        \\            .used = false,
-//        \\        },
-//        \\    ) catch unreachable) |*prev_value| {
-//        \\        foo();
-//        \\        bar();
-//        \\    }
-//        \\    if (put(
-//        \\        a,
-//        \\        b,
-//        \\    )) {
-//        \\        foo();
-//        \\    }
-//        \\}
-//        \\
-//    );
-//}
-//
+
+test "zig fmt: if condition has line break but must not wrap" {
+    try testCanonical(
+        \\comptime {
+        \\    if (self.user_input_options.put(
+        \\        name,
+        \\        UserInputOption{
+        \\            .name = name,
+        \\            .used = false,
+        \\        },
+        \\    ) catch unreachable) |*prev_value| {
+        \\        foo();
+        \\        bar();
+        \\    }
+        \\    if (put(
+        \\        a,
+        \\        b,
+        \\    )) {
+        \\        foo();
+        \\    }
+        \\}
+        \\
+    );
+}
+
 //test "zig fmt: if condition has line break but must not wrap" {
 //    try testCanonical(
 //        \\comptime {
lib/std/zig/render.zig
@@ -1006,13 +1006,11 @@ fn renderWhile(ais: *Ais, tree: ast.Tree, while_node: ast.full.While, space: Spa
                     break :blk ident + 1;
                 }
             };
-            const cond_has_newline = !tree.tokensOnSameLine(while_node.ast.while_token, pipe);
-            const brace_space: Space = if (cond_has_newline) .newline else .space;
+            const brace_space: Space = if (ais.isLineOverIndented()) .newline else .space;
             try renderToken(ais, tree, pipe, brace_space); // |
         } else {
             const rparen = tree.lastToken(while_node.ast.cond_expr) + 1;
-            const cond_has_newline = !tree.tokensOnSameLine(while_node.ast.while_token, rparen);
-            const brace_space: Space = if (cond_has_newline) .newline else .space;
+            const brace_space: Space = if (ais.isLineOverIndented()) .newline else .space;
             try renderToken(ais, tree, rparen, brace_space); // rparen
         }
         if (while_node.ast.cont_expr != 0) {