Skip to content

Commit eda6898

Browse files
committed
zig fmt: handle if and while indentation better
1 parent 530d175 commit eda6898

File tree

3 files changed

+658
-535
lines changed

3 files changed

+658
-535
lines changed

std/zig/ast.zig

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ pub const Tree = struct {
6868
return self.tokenLocationPtr(start_index, self.tokens.at(token_index));
6969
}
7070

71+
pub fn tokensOnSameLine(self: &Tree, token1_index: TokenIndex, token2_index: TokenIndex) bool {
72+
return self.tokensOnSameLinePtr(self.tokens.at(token1_index), self.tokens.at(token2_index));
73+
}
74+
75+
pub fn tokensOnSameLinePtr(self: &Tree, token1: &const Token, token2: &const Token) bool {
76+
return mem.indexOfScalar(u8, self.source[token1.end..token2.start], '\n') == null;
77+
}
78+
7179
pub fn dump(self: &Tree) void {
7280
self.root_node.base.dump(0);
7381
}
@@ -1529,14 +1537,14 @@ pub const Node = struct {
15291537

15301538
switch (self.op) {
15311539
Op.SliceType => |addr_of_info| {
1532-
if (addr_of_info.align_expr) |align_expr| {
1533-
if (i < 1) return align_expr;
1540+
if (addr_of_info.align_info) |align_info| {
1541+
if (i < 1) return align_info.node;
15341542
i -= 1;
15351543
}
15361544
},
15371545
Op.AddrOf => |addr_of_info| {
1538-
if (addr_of_info.align_expr) |align_expr| {
1539-
if (i < 1) return align_expr;
1546+
if (addr_of_info.align_info) |align_info| {
1547+
if (i < 1) return align_info.node;
15401548
i -= 1;
15411549
}
15421550
},
@@ -1553,7 +1561,9 @@ pub const Node = struct {
15531561
Op.NegationWrap,
15541562
Op.Try,
15551563
Op.Resume,
1556-
Op.UnwrapMaybe => {},
1564+
Op.UnwrapMaybe,
1565+
Op.PointerType,
1566+
=> {},
15571567
}
15581568

15591569
if (i < 1) return self.rhs;
@@ -1656,6 +1666,7 @@ pub const Node = struct {
16561666
if (i < fields.len) return fields.at(i).*;
16571667
i -= fields.len;
16581668
},
1669+
Op.Deref => {},
16591670
}
16601671

16611672
return null;
@@ -2082,9 +2093,6 @@ pub const Node = struct {
20822093
if (i < self.inputs.len) return &(self.inputs.at(index).*).base;
20832094
i -= self.inputs.len;
20842095

2085-
if (i < self.clobbers.len) return self.clobbers.at(index).*;
2086-
i -= self.clobbers.len;
2087-
20882096
return null;
20892097
}
20902098

std/zig/parser_test.zig

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,95 @@
1+
test "zig fmt: if condition wraps" {
2+
try testTransform(
3+
\\comptime {
4+
\\ if (cond and
5+
\\ cond) {
6+
\\ return x;
7+
\\ }
8+
\\ while (cond and
9+
\\ cond) {
10+
\\ return x;
11+
\\ }
12+
\\ if (a == b and
13+
\\ c) {
14+
\\ a = b;
15+
\\ }
16+
\\ while (a == b and
17+
\\ c) {
18+
\\ a = b;
19+
\\ }
20+
\\ if ((cond and
21+
\\ cond)) {
22+
\\ return x;
23+
\\ }
24+
\\ while ((cond and
25+
\\ cond)) {
26+
\\ return x;
27+
\\ }
28+
\\ var a = if (a) |*f| x: {
29+
\\ break :x &a.b;
30+
\\ } else |err| err;
31+
\\}
32+
,
33+
\\comptime {
34+
\\ if (cond and
35+
\\ cond)
36+
\\ {
37+
\\ return x;
38+
\\ }
39+
\\ while (cond and
40+
\\ cond)
41+
\\ {
42+
\\ return x;
43+
\\ }
44+
\\ if (a == b and
45+
\\ c)
46+
\\ {
47+
\\ a = b;
48+
\\ }
49+
\\ while (a == b and
50+
\\ c)
51+
\\ {
52+
\\ a = b;
53+
\\ }
54+
\\ if ((cond and
55+
\\ cond))
56+
\\ {
57+
\\ return x;
58+
\\ }
59+
\\ while ((cond and
60+
\\ cond))
61+
\\ {
62+
\\ return x;
63+
\\ }
64+
\\ var a = if (a) |*f| x: {
65+
\\ break :x &a.b;
66+
\\ } else |err| err;
67+
\\}
68+
\\
69+
);
70+
}
71+
72+
test "zig fmt: if condition has line break but must not wrap" {
73+
try testCanonical(
74+
\\comptime {
75+
\\ if (self.user_input_options.put(name, UserInputOption{
76+
\\ .name = name,
77+
\\ .used = false,
78+
\\ }) catch unreachable) |*prev_value| {
79+
\\ foo();
80+
\\ bar();
81+
\\ }
82+
\\ if (put(
83+
\\ a,
84+
\\ b,
85+
\\ )) {
86+
\\ foo();
87+
\\ }
88+
\\}
89+
\\
90+
);
91+
}
92+
193
test "zig fmt: same-line doc comment on variable declaration" {
294
try testTransform(
395
\\pub const MAP_ANONYMOUS = 0x1000; /// allocated from memory, swap space

0 commit comments

Comments
 (0)