authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-16 00:27:18-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-16 00:27:18-04:00
logee5f9ffad02b3cf263c13ca58c4d2e4526e29a84
tree2054c61378a2c125f82527a994b564db029b05d8
parent492a214d4c4f4feb15620dfd05230de0086825e5

zig fmt: add comma on last switch prong


3 files changed, 46 insertions(+), 2 deletions(-)

std/zig/parse.zig+1-1
...@@ -1406,7 +1406,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {...@@ -1406,7 +1406,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
1406 },1406 },
14071407
1408 State.SwitchCaseCommaOrEnd => |list_state| {1408 State.SwitchCaseCommaOrEnd => |list_state| {
1409 switch (expectCommaOrEnd(&tok_it, &tree, Token.Id.RParen)) {1409 switch (expectCommaOrEnd(&tok_it, &tree, Token.Id.RBrace)) {
1410 ExpectCommaOrEndResult.end_token => |maybe_end| if (maybe_end) |end| {1410 ExpectCommaOrEndResult.end_token => |maybe_end| if (maybe_end) |end| {
1411 *list_state.ptr = end;1411 *list_state.ptr = end;
1412 continue;1412 continue;
std/zig/parser_test.zig+31
...@@ -1,3 +1,34 @@...@@ -1,3 +1,34 @@
1test "zig fmt: add comma on last switch prong" {
2 try testTransform(
3 \\test "aoeu" {
4 \\switch (self.init_arg_expr) {
5 \\ InitArg.Type => |t| { },
6 \\ InitArg.None,
7 \\ InitArg.Enum => { }
8 \\}
9 \\ switch (self.init_arg_expr) {
10 \\ InitArg.Type => |t| { },
11 \\ InitArg.None,
12 \\ InitArg.Enum => { }//line comment
13 \\ }
14 \\}
15 ,
16 \\test "aoeu" {
17 \\ switch (self.init_arg_expr) {
18 \\ InitArg.Type => |t| {},
19 \\ InitArg.None,
20 \\ InitArg.Enum => {},
21 \\ }
22 \\ switch (self.init_arg_expr) {
23 \\ InitArg.Type => |t| {},
24 \\ InitArg.None,
25 \\ InitArg.Enum => {}, //line comment
26 \\ }
27 \\}
28 \\
29 );
30}
31
1test "zig fmt: same-line doc comment on variable declaration" {32test "zig fmt: same-line doc comment on variable declaration" {
2 try testTransform(33 try testTransform(
3 \\pub const MAP_ANONYMOUS = 0x1000; /// allocated from memory, swap space34 \\pub const MAP_ANONYMOUS = 0x1000; /// allocated from memory, swap space
std/zig/render.zig+14-1
...@@ -831,7 +831,20 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind...@@ -831,7 +831,20 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
831 }831 }
832832
833 try renderExpression(allocator, stream, tree, indent, switch_case.expr);833 try renderExpression(allocator, stream, tree, indent, switch_case.expr);
834 try renderToken(tree, stream, switch_case.lastToken() + 1, indent, true);834 {
835 // Handle missing comma after last switch case
836 var index = switch_case.lastToken() + 1;
837 switch (tree.tokens.at(index).id) {
838 Token.Id.RBrace => {
839 try stream.write(",");
840 },
841 Token.Id.LineComment => {
842 try stream.write(", ");
843 try renderToken(tree, stream, index, indent, true);
844 },
845 else => try renderToken(tree, stream, index, indent, true),
846 }
847 }
835 },848 },
836 ast.Node.Id.SwitchElse => {849 ast.Node.Id.SwitchElse => {
837 const switch_else = @fieldParentPtr(ast.Node.SwitchElse, "base", base);850 const switch_else = @fieldParentPtr(ast.Node.SwitchElse, "base", base);