zig icon indicating copy to clipboard operation
zig copied to clipboard

std.zig.render.hasComment: skip contents of nested blocks

Open Lzard opened this issue 1 year ago • 0 comments

Given this unformatted code:

const block = .{
    .foo = .{
        // comment
        .bar = 0,
    }
};

Current zig fmt formats it this way:

const block = .{
    .foo = .{
        // comment
        .bar = 0,
    },
};

A trailing comma is added even though the comment is within a nested block. Without the comment, the result is this:

const block = .{ .foo = .{
    .bar = 0,
} };

This branch's implementation of std.zig.render.hasComment skips any comment within nested blocks, giving the following result instead:

const block = .{ .foo = .{
    // comment
    .bar = 0,
} };

Note that in status quo (and in this branch), the following does not get changed:

const block = .{ // comment
    .foo = .{ .bar = 0 },
};

I am unsure whether this is intended or not, and in doubt I did not search to fix it.

Fixes #20529.

Redo of #20721; sorry again for failing to run the correct test suite in the original PR.

Lzard avatar Jul 22 '24 12:07 Lzard