Skip to content

Commit 8794ce6

Browse files
authored
Merge pull request #6293 from LakeByTheWoods/fmt_fixes
zig fmt fixes
2 parents eab51b7 + 4496a6c commit 8794ce6

File tree

3 files changed

+575
-142
lines changed

3 files changed

+575
-142
lines changed

lib/std/zig/ast.zig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,15 @@ pub const Node = struct {
823823
}
824824
}
825825

826+
pub fn findFirstWithId(self: *Node, id: Id) ?*Node {
827+
if (self.id == id) return self;
828+
var child_i: usize = 0;
829+
while (self.iterate(child_i)) |child| : (child_i += 1) {
830+
if (child.findFirstWithId(id)) |result| return result;
831+
}
832+
return null;
833+
}
834+
826835
pub fn dump(self: *Node, indent: usize) void {
827836
{
828837
var i: usize = 0;

lib/std/zig/parser_test.zig

Lines changed: 325 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,8 +1301,10 @@ test "zig fmt: array literal with hint" {
13011301
\\const a = []u8{
13021302
\\ 1, 2,
13031303
\\ 3, 4,
1304-
\\ 5, 6, // blah
1305-
\\ 7, 8,
1304+
\\ 5,
1305+
\\ 6, // blah
1306+
\\ 7,
1307+
\\ 8,
13061308
\\};
13071309
\\const a = []u8{
13081310
\\ 1, 2,
@@ -1372,7 +1374,7 @@ test "zig fmt: multiline string parameter in fn call with trailing comma" {
13721374
\\ \\ZIG_C_HEADER_FILES {}
13731375
\\ \\ZIG_DIA_GUIDS_LIB {}
13741376
\\ \\
1375-
\\ ,
1377+
\\ ,
13761378
\\ std.cstr.toSliceConst(c.ZIG_CMAKE_BINARY_DIR),
13771379
\\ std.cstr.toSliceConst(c.ZIG_CXX_COMPILER),
13781380
\\ std.cstr.toSliceConst(c.ZIG_DIA_GUIDS_LIB),
@@ -3321,6 +3323,326 @@ test "zig fmt: Don't add extra newline after if" {
33213323
);
33223324
}
33233325

3326+
test "zig fmt: comments in ternary ifs" {
3327+
try testCanonical(
3328+
\\const x = if (true) {
3329+
\\ 1;
3330+
\\} else if (false)
3331+
\\ // Comment
3332+
\\ 0;
3333+
\\const y = if (true)
3334+
\\ // Comment
3335+
\\ 1
3336+
\\else
3337+
\\ 0;
3338+
\\
3339+
\\pub extern "c" fn printf(format: [*:0]const u8, ...) c_int;
3340+
\\
3341+
);
3342+
}
3343+
3344+
test "zig fmt: test comments in field access chain" {
3345+
try testCanonical(
3346+
\\pub const str = struct {
3347+
\\ pub const Thing = more.more //
3348+
\\ .more() //
3349+
\\ .more().more() //
3350+
\\ .more() //
3351+
\\ // .more() //
3352+
\\ .more() //
3353+
\\ .more();
3354+
\\ data: Data,
3355+
\\};
3356+
\\
3357+
\\pub const str = struct {
3358+
\\ pub const Thing = more.more //
3359+
\\ .more() //
3360+
\\ // .more() //
3361+
\\ // .more() //
3362+
\\ // .more() //
3363+
\\ .more() //
3364+
\\ .more();
3365+
\\ data: Data,
3366+
\\};
3367+
\\
3368+
\\pub const str = struct {
3369+
\\ pub const Thing = more //
3370+
\\ .more //
3371+
\\ .more() //
3372+
\\ .more();
3373+
\\ data: Data,
3374+
\\};
3375+
\\
3376+
);
3377+
}
3378+
3379+
test "zig fmt: Indent comma correctly after multiline string literals in arg list (trailing comma)" {
3380+
try testCanonical(
3381+
\\fn foo() void {
3382+
\\ z.display_message_dialog(
3383+
\\ *const [323:0]u8,
3384+
\\ \\Message Text
3385+
\\ \\------------
3386+
\\ \\xxxxxxxxxxxx
3387+
\\ \\xxxxxxxxxxxx
3388+
\\ ,
3389+
\\ g.GtkMessageType.GTK_MESSAGE_WARNING,
3390+
\\ null,
3391+
\\ );
3392+
\\
3393+
\\ z.display_message_dialog(*const [323:0]u8,
3394+
\\ \\Message Text
3395+
\\ \\------------
3396+
\\ \\xxxxxxxxxxxx
3397+
\\ \\xxxxxxxxxxxx
3398+
\\ , g.GtkMessageType.GTK_MESSAGE_WARNING, null);
3399+
\\}
3400+
\\
3401+
);
3402+
}
3403+
3404+
test "zig fmt: Control flow statement as body of blockless if" {
3405+
try testCanonical(
3406+
\\pub fn main() void {
3407+
\\ const zoom_node = if (focused_node == layout_first)
3408+
\\ if (it.next()) {
3409+
\\ if (!node.view.pending.float and !node.view.pending.fullscreen) break node;
3410+
\\ } else null
3411+
\\ else
3412+
\\ focused_node;
3413+
\\
3414+
\\ const zoom_node = if (focused_node == layout_first) while (it.next()) |node| {
3415+
\\ if (!node.view.pending.float and !node.view.pending.fullscreen) break node;
3416+
\\ } else null else
3417+
\\ focused_node;
3418+
\\
3419+
\\ const zoom_node = if (focused_node == layout_first)
3420+
\\ if (it.next()) {
3421+
\\ if (!node.view.pending.float and !node.view.pending.fullscreen) break node;
3422+
\\ } else null;
3423+
\\
3424+
\\ const zoom_node = if (focused_node == layout_first) while (it.next()) |node| {
3425+
\\ if (!node.view.pending.float and !node.view.pending.fullscreen) break node;
3426+
\\ };
3427+
\\
3428+
\\ const zoom_node = if (focused_node == layout_first) for (nodes) |node| {
3429+
\\ break node;
3430+
\\ };
3431+
\\
3432+
\\ const zoom_node = if (focused_node == layout_first) switch (nodes) {
3433+
\\ 0 => 0,
3434+
\\ } else
3435+
\\ focused_node;
3436+
\\}
3437+
\\
3438+
);
3439+
}
3440+
3441+
test "zig fmt: " {
3442+
try testCanonical(
3443+
\\pub fn sendViewTags(self: Self) void {
3444+
\\ var it = ViewStack(View).iterator(self.output.views.first, std.math.maxInt(u32));
3445+
\\ while (it.next()) |node|
3446+
\\ view_tags.append(node.view.current_tags) catch {
3447+
\\ c.wl_resource_post_no_memory(self.wl_resource);
3448+
\\ log.crit(.river_status, "out of memory", .{});
3449+
\\ return;
3450+
\\ };
3451+
\\}
3452+
\\
3453+
);
3454+
}
3455+
3456+
test "zig fmt: allow trailing line comments to do manual array formatting" {
3457+
try testCanonical(
3458+
\\fn foo() void {
3459+
\\ self.code.appendSliceAssumeCapacity(&[_]u8{
3460+
\\ 0x55, // push rbp
3461+
\\ 0x48, 0x89, 0xe5, // mov rbp, rsp
3462+
\\ 0x48, 0x81, 0xec, // sub rsp, imm32 (with reloc)
3463+
\\ });
3464+
\\
3465+
\\ di_buf.appendAssumeCapacity(&[_]u8{
3466+
\\ 1, DW.TAG_compile_unit, DW.CHILDREN_no, // header
3467+
\\ DW.AT_stmt_list, DW_FORM_data4, // form value pairs
3468+
\\ DW.AT_low_pc, DW_FORM_addr,
3469+
\\ DW.AT_high_pc, DW_FORM_addr,
3470+
\\ DW.AT_name, DW_FORM_strp,
3471+
\\ DW.AT_comp_dir, DW_FORM_strp,
3472+
\\ DW.AT_producer, DW_FORM_strp,
3473+
\\ DW.AT_language, DW_FORM_data2,
3474+
\\ 0, 0, // sentinel
3475+
\\ });
3476+
\\
3477+
\\ self.code.appendSliceAssumeCapacity(&[_]u8{
3478+
\\ 0x55, // push rbp
3479+
\\ 0x48, 0x89, 0xe5, // mov rbp, rsp
3480+
\\ // How do we handle this?
3481+
\\ //0x48, 0x81, 0xec, // sub rsp, imm32 (with reloc)
3482+
\\ // Here's a blank line, should that be allowed?
3483+
\\
3484+
\\ 0x48, 0x89, 0xe5,
3485+
\\ 0x33, 0x45,
3486+
\\ // Now the comment breaks a single line -- how do we handle this?
3487+
\\ 0x88,
3488+
\\ });
3489+
\\}
3490+
\\
3491+
);
3492+
}
3493+
3494+
test "zig fmt: multiline string literals should play nice with array initializers" {
3495+
try testCanonical(
3496+
\\fn main() void {
3497+
\\ var a = .{.{.{.{.{.{.{.{
3498+
\\ 0,
3499+
\\ }}}}}}}};
3500+
\\ myFunc(.{
3501+
\\ "aaaaaaa", "bbbbbb", "ccccc",
3502+
\\ "dddd", ("eee"), ("fff"),
3503+
\\ ("gggg"),
3504+
\\ // Line comment
3505+
\\ \\Multiline String Literals can be quite long
3506+
\\ ,
3507+
\\ \\Multiline String Literals can be quite long
3508+
\\ \\Multiline String Literals can be quite long
3509+
\\ ,
3510+
\\ \\Multiline String Literals can be quite long
3511+
\\ \\Multiline String Literals can be quite long
3512+
\\ \\Multiline String Literals can be quite long
3513+
\\ \\Multiline String Literals can be quite long
3514+
\\ ,
3515+
\\ (
3516+
\\ \\Multiline String Literals can be quite long
3517+
\\ ),
3518+
\\ .{
3519+
\\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
3520+
\\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
3521+
\\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
3522+
\\ },
3523+
\\ .{(
3524+
\\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
3525+
\\ )},
3526+
\\ .{
3527+
\\ "xxxxxxx", "xxx",
3528+
\\ (
3529+
\\ \\ xxx
3530+
\\ ),
3531+
\\ "xxx", "xxx",
3532+
\\ },
3533+
\\ .{ "xxxxxxx", "xxx", "xxx", "xxx" }, .{ "xxxxxxx", "xxx", "xxx", "xxx" },
3534+
\\ "aaaaaaa", "bbbbbb", "ccccc", // -
3535+
\\ "dddd", ("eee"), ("fff"),
3536+
\\ .{
3537+
\\ "xxx", "xxx",
3538+
\\ (
3539+
\\ \\ xxx
3540+
\\ ),
3541+
\\ "xxxxxxxxxxxxxx", "xxx",
3542+
\\ },
3543+
\\ .{
3544+
\\ (
3545+
\\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
3546+
\\ ),
3547+
\\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
3548+
\\ },
3549+
\\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
3550+
\\ \\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
3551+
\\ });
3552+
\\}
3553+
\\
3554+
);
3555+
}
3556+
3557+
test "zig fmt: use of comments and Multiline string literals may force the parameters over multiple lines" {
3558+
try testCanonical(
3559+
\\pub fn makeMemUndefined(qzz: []u8) i1 {
3560+
\\ cases.add( // fixed bug #2032
3561+
\\ "compile diagnostic string for top level decl type",
3562+
\\ \\export fn entry() void {
3563+
\\ \\ var foo: u32 = @This(){};
3564+
\\ \\}
3565+
\\ , &[_][]const u8{
3566+
\\ "tmp.zig:2:27: error: type 'u32' does not support array initialization",
3567+
\\ });
3568+
\\ @compileError(
3569+
\\ \\ unknown-length pointers and C pointers cannot be hashed deeply.
3570+
\\ \\ Consider providing your own hash function.
3571+
\\ \\ unknown-length pointers and C pointers cannot be hashed deeply.
3572+
\\ \\ Consider providing your own hash function.
3573+
\\ );
3574+
\\ return @intCast(i1, doMemCheckClientRequestExpr(0, // default return
3575+
\\ .MakeMemUndefined, @ptrToInt(qzz.ptr), qzz.len, 0, 0, 0));
3576+
\\}
3577+
\\
3578+
\\// This looks like garbage don't do this
3579+
\\const rparen = tree.prevToken(
3580+
\\// the first token for the annotation expressions is the left
3581+
\\// parenthesis, hence the need for two prevToken
3582+
\\ if (fn_proto.getAlignExpr()) |align_expr|
3583+
\\ tree.prevToken(tree.prevToken(align_expr.firstToken()))
3584+
\\else if (fn_proto.getSectionExpr()) |section_expr|
3585+
\\ tree.prevToken(tree.prevToken(section_expr.firstToken()))
3586+
\\else if (fn_proto.getCallconvExpr()) |callconv_expr|
3587+
\\ tree.prevToken(tree.prevToken(callconv_expr.firstToken()))
3588+
\\else switch (fn_proto.return_type) {
3589+
\\ .Explicit => |node| node.firstToken(),
3590+
\\ .InferErrorSet => |node| tree.prevToken(node.firstToken()),
3591+
\\ .Invalid => unreachable,
3592+
\\});
3593+
\\
3594+
);
3595+
}
3596+
3597+
test "zig fmt: single argument trailing commas in @builtins()" {
3598+
try testCanonical(
3599+
\\pub fn foo(qzz: []u8) i1 {
3600+
\\ @panic(
3601+
\\ foo,
3602+
\\ );
3603+
\\ panic(
3604+
\\ foo,
3605+
\\ );
3606+
\\ @panic(
3607+
\\ foo,
3608+
\\ bar,
3609+
\\ );
3610+
\\}
3611+
\\
3612+
);
3613+
}
3614+
3615+
test "zig fmt: trailing comma should force multiline 1 column" {
3616+
try testTransform(
3617+
\\pub const UUID_NULL: uuid_t = [16]u8{0,0,0,0,};
3618+
\\
3619+
,
3620+
\\pub const UUID_NULL: uuid_t = [16]u8{
3621+
\\ 0,
3622+
\\ 0,
3623+
\\ 0,
3624+
\\ 0,
3625+
\\};
3626+
\\
3627+
);
3628+
}
3629+
3630+
test "zig fmt: function params should align nicely" {
3631+
try testCanonical(
3632+
\\pub fn foo() void {
3633+
\\ cases.addRuntimeSafety("slicing operator with sentinel",
3634+
\\ \\const std = @import("std");
3635+
\\ ++ check_panic_msg ++
3636+
\\ \\pub fn main() void {
3637+
\\ \\ var buf = [4]u8{'a','b','c',0};
3638+
\\ \\ const slice = buf[0..:0];
3639+
\\ \\}
3640+
\\ );
3641+
\\}
3642+
\\
3643+
);
3644+
}
3645+
33243646
const std = @import("std");
33253647
const mem = std.mem;
33263648
const warn = std.debug.warn;

0 commit comments

Comments
 (0)