authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-17 00:44:55-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-17 00:44:55-04:00
logb48d354600406d39b29fe7327b09a62d2584a83f
tree6133c6ca43bc144308fa09e7a09e4a786b0a8c68
parent37c6afa5b4c25e008206d8a036e02b7fd7189459

zig fmt: fix comment after if before another if


3 files changed, 35 insertions(+), 7 deletions(-)

std/segmented_list.zig+12-6
......@@ -298,21 +298,27 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type
298298
299299 return &it.list.dynamic_segments[it.shelf_index][it.box_index];
300300 }
301
302 pub fn set(it: &Iterator, index: usize) void {
303 if (index < prealloc_item_count) {
304 it.index = index;
305 return;
306 }
307 it.shelf_index = shelfIndex(index);
308 it.box_index = boxIndex(index, it.shelf_index);
309 it.shelf_size = shelfSize(it.shelf_index);
310 }
301311 };
302312
303313 pub fn iterator(self: &Self, start_index: usize) Iterator {
304314 var it = Iterator {
305315 .list = self,
306 .index = start_index,
316 .index = undefined,
307317 .shelf_index = undefined,
308318 .box_index = undefined,
309319 .shelf_size = undefined,
310320 };
311 if (start_index >= prealloc_item_count) {
312 it.shelf_index = shelfIndex(start_index);
313 it.box_index = boxIndex(start_index, it.shelf_index);
314 it.shelf_size = shelfSize(it.shelf_index);
315 }
321 it.set(start_index);
316322 return it;
317323 }
318324 };
std/zig/parse.zig+8-1
......@@ -1017,7 +1017,11 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
10171017 continue;
10181018 },
10191019 State.Else => |dest| {
1020 while (try eatLineComment(arena, &tok_it, &tree)) |_| { }
1020 const old_index = tok_it.index;
1021 var need_index_restore = false;
1022 while (try eatLineComment(arena, &tok_it, &tree)) |_| {
1023 need_index_restore = true;
1024 }
10211025 if (eatToken(&tok_it, &tree, Token.Id.Keyword_else)) |else_token| {
10221026 const node = try arena.construct(ast.Node.Else {
10231027 .base = ast.Node {.id = ast.Node.Id.Else },
......@@ -1031,6 +1035,9 @@ pub fn parse(allocator: &mem.Allocator, source: []const u8) !ast.Tree {
10311035 try stack.append(State { .Payload = OptionalCtx { .Optional = &node.payload } });
10321036 continue;
10331037 } else {
1038 if (need_index_restore) {
1039 tok_it.set(old_index);
1040 }
10341041 continue;
10351042 }
10361043 },
std/zig/parser_test.zig+15
......@@ -1,3 +1,18 @@
1test "zig fmt: comment after if before another if" {
2 try testCanonical(
3 \\test "aoeu" {
4 \\ if (x) {
5 \\ foo();
6 \\ }
7 \\ // comment
8 \\ if (x) {
9 \\ bar();
10 \\ }
11 \\}
12 \\
13 );
14}
15
116test "zig fmt: line comment between if block and else keyword" {
217 try testTransform(
318 \\test "aoeu" {