Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zig fmt doesn't align vertically on trailing comma #20757

Open
kristoff-it opened this issue Jul 23, 2024 · 1 comment
Open

Zig fmt doesn't align vertically on trailing comma #20757

kristoff-it opened this issue Jul 23, 2024 · 1 comment
Labels
bug Observed behavior contradicts documented or intended behavior zig fmt
Milestone

Comments

@kristoff-it
Copy link
Member

kristoff-it commented Jul 23, 2024

Zig Version

0.13.0

Steps to Reproduce and Observed Behavior

fn version(b: anytype) []const u8 {
    const git_describe = std.mem.trim(
        u8,
        b.run(&[_][]const u8{"git", "-C", b.build_root.path.?, "describe", "--match", "*.*.*", "--tags",
             }),
        " \n\r",
    );
}

Expected Behavior

On format the list of arguments should be aligned vertically because of the trailing comma but instead it all stays on one line.

Unless this is a peculiar corner-case of more-than-one-item-per-line formatting, it seems a bug.

Removing the comma, formatting, and then adding it back restored the expected behavior (because the first save changes the location of }),.

@kristoff-it kristoff-it added the bug Observed behavior contradicts documented or intended behavior label Jul 23, 2024
@castholm
Copy link
Contributor

castholm commented Jul 23, 2024

This is not a bug, zig fmt has special rules for array inits and tries to format them like tables. What you end up with in your repro is a table with 7 columns and 1 row. But if you for example instead first manually format your code like this

fn version(b: anytype) []const u8 {
    const git_describe = std.mem.trim(
        u8,
        b.run(&[_][]const u8{"git", "-C", b.build_root.path.?,
        "describe", "--match", "*.*.*", "--tags", }),
        " \n\r",
    );
}

before invoking zig fmt, you end up with a table with 3 columns and 3 rows:

fn version(b: anytype) []const u8 {
    const git_describe = std.mem.trim(
        u8,
        b.run(&[_][]const u8{
            "git",      "-C",      b.build_root.path.?,
            "describe", "--match", "*.*.*",
            "--tags",
        }),
        " \n\r",
    );
}

However, it should be noted that this special handling is a little awkward and is the reason that // zig fmt: off does not work in array inits. It has been suggested that this special handling should be removed in favor of manual alignment with // zig fmt: off (#10418 (comment)), or expanded upon as part of #17145.

@andrewrk andrewrk added this to the 0.15.0 milestone Jul 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior zig fmt
Projects
None yet
Development

No branches or pull requests

3 participants