Skip to content

Commit

Permalink
zig fmt: add comma on last switch prong
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrk committed May 16, 2018
1 parent 492a214 commit ee5f9ff
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion std/zig/parse.zig
Expand Up @@ -1406,7 +1406,7 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
},

State.SwitchCaseCommaOrEnd => |list_state| {
switch (expectCommaOrEnd(&tok_it, &tree, Token.Id.RParen)) {
switch (expectCommaOrEnd(&tok_it, &tree, Token.Id.RBrace)) {
ExpectCommaOrEndResult.end_token => |maybe_end| if (maybe_end) |end| {
*list_state.ptr = end;
continue;
Expand Down
31 changes: 31 additions & 0 deletions std/zig/parser_test.zig
@@ -1,3 +1,34 @@
test "zig fmt: add comma on last switch prong" {
try testTransform(
\\test "aoeu" {
\\switch (self.init_arg_expr) {
\\ InitArg.Type => |t| { },
\\ InitArg.None,
\\ InitArg.Enum => { }
\\}
\\ switch (self.init_arg_expr) {
\\ InitArg.Type => |t| { },
\\ InitArg.None,
\\ InitArg.Enum => { }//line comment
\\ }
\\}
,
\\test "aoeu" {
\\ switch (self.init_arg_expr) {
\\ InitArg.Type => |t| {},
\\ InitArg.None,
\\ InitArg.Enum => {},
\\ }
\\ switch (self.init_arg_expr) {
\\ InitArg.Type => |t| {},
\\ InitArg.None,
\\ InitArg.Enum => {}, //line comment
\\ }
\\}
\\
);
}

test "zig fmt: same-line doc comment on variable declaration" {
try testTransform(
\\pub const MAP_ANONYMOUS = 0x1000; /// allocated from memory, swap space
Expand Down
15 changes: 14 additions & 1 deletion std/zig/render.zig
Expand Up @@ -831,7 +831,20 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
}

try renderExpression(allocator, stream, tree, indent, switch_case.expr);
try renderToken(tree, stream, switch_case.lastToken() + 1, indent, true);
{
// Handle missing comma after last switch case
var index = switch_case.lastToken() + 1;
switch (tree.tokens.at(index).id) {
Token.Id.RBrace => {
try stream.write(",");
},
Token.Id.LineComment => {
try stream.write(", ");
try renderToken(tree, stream, index, indent, true);
},
else => try renderToken(tree, stream, index, indent, true),
}
}
},
ast.Node.Id.SwitchElse => {
const switch_else = @fieldParentPtr(ast.Node.SwitchElse, "base", base);
Expand Down

0 comments on commit ee5f9ff

Please sign in to comment.