zig fmt: while

This commit is contained in:
Andrew Kelley 2021-02-22 17:55:19 -07:00
parent 20cfa0b5b6
commit fec51ad7c5
3 changed files with 121 additions and 97 deletions

View File

@ -587,11 +587,16 @@ pub const Tree = struct {
.for_simple,
.@"for",
=> {
// Look for a label and inline.
const main_token = main_tokens[n];
return switch (token_tags[main_token - 1]) {
.keyword_inline => main_token - 1,
else => main_token,
} - end_offset;
var result = main_token;
if (token_tags[result - 1] == .keyword_inline) {
result -= 1;
}
if (token_tags[result - 1] == .colon) {
result -= 2;
}
return result - end_offset;
},
};
}

View File

@ -1456,17 +1456,17 @@ test "zig fmt: if condition has line break but must not wrap (no fn call comma)"
);
}
//test "zig fmt: function call with multiline argument" {
// try testCanonical(
// \\comptime {
// \\ self.user_input_options.put(name, UserInputOption{
// \\ .name = name,
// \\ .used = false,
// \\ });
// \\}
// \\
// );
//}
test "zig fmt: function call with multiline argument" {
try testCanonical(
\\comptime {
\\ self.user_input_options.put(name, UserInputOption{
\\ .name = name,
\\ .used = false,
\\ });
\\}
\\
);
}
test "zig fmt: if-else with comment before else" {
try testCanonical(
@ -1817,14 +1817,24 @@ test "zig fmt: empty block with only comment" {
);
}
//test "zig fmt: no trailing comma on struct decl" {
// try testCanonical(
// \\const RoundParam = struct {
// \\ k: usize, s: u32, t: u32
// \\};
// \\
// );
//}
test "zig fmt: trailing commas on struct decl" {
try testTransform(
\\const RoundParam = struct {
\\ k: usize, s: u32, t: u32
\\};
\\const RoundParam = struct {
\\ k: usize, s: u32, t: u32,
\\};
,
\\const RoundParam = struct { k: usize, s: u32, t: u32 };
\\const RoundParam = struct {
\\ k: usize,
\\ s: u32,
\\ t: u32,
\\};
\\
);
}
test "zig fmt: extra newlines at the end" {
try testTransform(
@ -2975,75 +2985,75 @@ test "zig fmt: switch" {
);
}
//test "zig fmt: while" {
// try testCanonical(
// \\test "while" {
// \\ while (10 < 1) unreachable;
// \\
// \\ while (10 < 1) unreachable else unreachable;
// \\
// \\ while (10 < 1) {
// \\ unreachable;
// \\ }
// \\
// \\ while (10 < 1)
// \\ unreachable;
// \\
// \\ var i: usize = 0;
// \\ while (i < 10) : (i += 1) {
// \\ continue;
// \\ }
// \\
// \\ i = 0;
// \\ while (i < 10) : (i += 1)
// \\ continue;
// \\
// \\ i = 0;
// \\ var j: usize = 0;
// \\ while (i < 10) : ({
// \\ i += 1;
// \\ j += 1;
// \\ }) {
// \\ continue;
// \\ }
// \\
// \\ var a: ?u8 = 2;
// \\ while (a) |v| : (a = null) {
// \\ continue;
// \\ }
// \\
// \\ while (a) |v| : (a = null)
// \\ unreachable;
// \\
// \\ label: while (10 < 0) {
// \\ unreachable;
// \\ }
// \\
// \\ const res = while (0 < 10) {
// \\ break 7;
// \\ } else {
// \\ unreachable;
// \\ };
// \\
// \\ const res = while (0 < 10)
// \\ break 7
// \\ else
// \\ unreachable;
// \\
// \\ var a: anyerror!u8 = 0;
// \\ while (a) |v| {
// \\ a = error.Err;
// \\ } else |err| {
// \\ i = 1;
// \\ }
// \\
// \\ comptime var k: usize = 0;
// \\ inline while (i < 10) : (i += 1)
// \\ j += 2;
// \\}
// \\
// );
//}
test "zig fmt: while" {
try testCanonical(
\\test "while" {
\\ while (10 < 1) unreachable;
\\
\\ while (10 < 1) unreachable else unreachable;
\\
\\ while (10 < 1) {
\\ unreachable;
\\ }
\\
\\ while (10 < 1)
\\ unreachable;
\\
\\ var i: usize = 0;
\\ while (i < 10) : (i += 1) {
\\ continue;
\\ }
\\
\\ i = 0;
\\ while (i < 10) : (i += 1)
\\ continue;
\\
\\ i = 0;
\\ var j: usize = 0;
\\ while (i < 10) : ({
\\ i += 1;
\\ j += 1;
\\ }) {
\\ continue;
\\ }
\\
\\ var a: ?u8 = 2;
\\ while (a) |v| : (a = null) {
\\ continue;
\\ }
\\
\\ while (a) |v| : (a = null)
\\ unreachable;
\\
\\ label: while (10 < 0) {
\\ unreachable;
\\ }
\\
\\ const res = while (0 < 10) {
\\ break 7;
\\ } else {
\\ unreachable;
\\ };
\\
\\ const res = while (0 < 10)
\\ break 7
\\ else
\\ unreachable;
\\
\\ var a: anyerror!u8 = 0;
\\ while (a) |v| {
\\ a = error.Err;
\\ } else |err| {
\\ i = 1;
\\ }
\\
\\ comptime var k: usize = 0;
\\ inline while (i < 10) : (i += 1)
\\ j += 2;
\\}
\\
);
}
test "zig fmt: for" {
try testCanonical(

View File

@ -1006,11 +1006,17 @@ fn renderWhile(ais: *Ais, tree: ast.Tree, while_node: ast.full.While, space: Spa
break :blk ident + 1;
}
};
const brace_space: Space = if (ais.isLineOverIndented()) .newline else .space;
const brace_space = if (while_node.ast.cont_expr == 0 and ais.isLineOverIndented())
Space.newline
else
Space.space;
try renderToken(ais, tree, pipe, brace_space); // |
} else {
const rparen = tree.lastToken(while_node.ast.cond_expr) + 1;
const brace_space: Space = if (ais.isLineOverIndented()) .newline else .space;
const brace_space = if (while_node.ast.cont_expr == 0 and ais.isLineOverIndented())
Space.newline
else
Space.space;
try renderToken(ais, tree, rparen, brace_space); // rparen
}
if (while_node.ast.cont_expr != 0) {
@ -1019,7 +1025,8 @@ fn renderWhile(ais: *Ais, tree: ast.Tree, while_node: ast.full.While, space: Spa
try renderToken(ais, tree, lparen - 1, .space); // :
try renderToken(ais, tree, lparen, .none); // lparen
try renderExpression(ais, tree, while_node.ast.cont_expr, .none);
try renderToken(ais, tree, rparen, .space); // rparen
const brace_space: Space = if (ais.isLineOverIndented()) .newline else .space;
try renderToken(ais, tree, rparen, brace_space); // rparen
}
if (while_node.ast.else_expr != 0) {
try renderExpression(ais, tree, while_node.ast.then_expr, Space.space);
@ -1061,10 +1068,12 @@ fn renderWhile(ais: *Ais, tree: ast.Tree, while_node: ast.full.While, space: Spa
break :blk ident + 1;
}
};
try renderToken(ais, tree, pipe, .newline); // |
const after_space: Space = if (while_node.ast.cont_expr != 0) .space else .newline;
try renderToken(ais, tree, pipe, after_space); // |
} else {
ais.pushIndent();
try renderToken(ais, tree, rparen, .newline); // rparen
const after_space: Space = if (while_node.ast.cont_expr != 0) .space else .newline;
try renderToken(ais, tree, rparen, after_space); // rparen
ais.popIndent();
}
if (while_node.ast.cont_expr != 0) {