Skip to content

Commit

Permalink
zig fmt: fix comment after if before another if
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrk committed May 17, 2018
1 parent 37c6afa commit b48d354
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
18 changes: 12 additions & 6 deletions std/segmented_list.zig
Expand Up @@ -298,21 +298,27 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type

return &it.list.dynamic_segments[it.shelf_index][it.box_index];
}

pub fn set(it: &Iterator, index: usize) void {
if (index < prealloc_item_count) {
it.index = index;
return;
}
it.shelf_index = shelfIndex(index);
it.box_index = boxIndex(index, it.shelf_index);
it.shelf_size = shelfSize(it.shelf_index);
}
};

pub fn iterator(self: &Self, start_index: usize) Iterator {
var it = Iterator {
.list = self,
.index = start_index,
.index = undefined,
.shelf_index = undefined,
.box_index = undefined,
.shelf_size = undefined,
};
if (start_index >= prealloc_item_count) {
it.shelf_index = shelfIndex(start_index);
it.box_index = boxIndex(start_index, it.shelf_index);
it.shelf_size = shelfSize(it.shelf_index);
}
it.set(start_index);
return it;
}
};
Expand Down
9 changes: 8 additions & 1 deletion std/zig/parse.zig
Expand Up @@ -1017,7 +1017,11 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
continue;
},
State.Else => |dest| {
while (try eatLineComment(arena, &tok_it, &tree)) |_| { }
const old_index = tok_it.index;
var need_index_restore = false;
while (try eatLineComment(arena, &tok_it, &tree)) |_| {
need_index_restore = true;
}
if (eatToken(&tok_it, &tree, Token.Id.Keyword_else)) |else_token| {
const node = try arena.construct(ast.Node.Else {
.base = ast.Node {.id = ast.Node.Id.Else },
Expand All @@ -1031,6 +1035,9 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
try stack.append(State { .Payload = OptionalCtx { .Optional = &node.payload } });
continue;
} else {
if (need_index_restore) {
tok_it.set(old_index);
}
continue;
}
},
Expand Down
15 changes: 15 additions & 0 deletions std/zig/parser_test.zig
@@ -1,3 +1,18 @@
test "zig fmt: comment after if before another if" {
try testCanonical(
\\test "aoeu" {
\\ if (x) {
\\ foo();
\\ }
\\ // comment
\\ if (x) {
\\ bar();
\\ }
\\}
\\
);
}

test "zig fmt: line comment between if block and else keyword" {
try testTransform(
\\test "aoeu" {
Expand Down

0 comments on commit b48d354

Please sign in to comment.