IDE Code formatting: Zig formatter is in my way

Hi,
there are things which I don’t want to format. I.e., a chess board arrangement:

pub const Square = enum(u6) {
        a1, b1, c1, d1, e1, f1, g1, h1,
        a2, b2, c2, d2, e2, f2, g2, h2,
        a3, b3, c3, d3, e3, f3, g3, h3,
        a4, b4, c4, d4, e4, f4, g4, h4,
        a5, b5, c5, d5, e5, f5, g5, h5,
        a6, b6, c6, d6, e6, f6, g6, h6,
        a7, b7, c7, d7, e7, f7, g7, h7,
        a8, b8, c8, d8, e8, f8, g8, h8,
};

The problem is, that this is turned into following by the Zig formatter:

pub const Square = enum(u6) {
        a1,
        b1,
        c1,
        d1,
        e1,
        f1,
        g1,
        h1,
        a2,
        b2,
        c2,
        d2,
        e2,
        f2,
        g2,
        h2,
        a3,
        b3,
        c3,
        d3,
        e3,
        f3,
        g3,
        h3,
        a4,
        b4,
        c4,
        d4,
        e4,
        f4,
        g4,
        h4,
        a5,
        b5,
        c5,
        d5,
        e5,
        f5,
        g5,
        h5,
        a6,
        b6,
        c6,
        d6,
        e6,
        f6,
        g6,
        h6,
        a7,
        b7,
        c7,
        d7,
        e7,
        f7,
        g7,
        h7,
        a8,
        b8,
        c8,
        d8,
        e8,
        f8,
        g8,
        h8,
    };

How can I prevent this?

1 Like

You can turn off formatter with // zig fmt: off and then turning back it on with // zig fmt: on like:

// zig fmt: off
pub const Square = enum(u6) {
        a1, b1, c1, d1, e1, f1, g1, h1,
        a2, b2, c2, d2, e2, f2, g2, h2,
        a3, b3, c3, d3, e3, f3, g3, h3,
        a4, b4, c4, d4, e4, f4, g4, h4,
        a5, b5, c5, d5, e5, f5, g5, h5,
        a6, b6, c6, d6, e6, f6, g6, h6,
        a7, b7, c7, d7, e7, f7, g7, h7,
        a8, b8, c8, d8, e8, f8, g8, h8,
};
// zig fmt: on
12 Likes

Thank you, that worked! :smile:

3 Likes

Apart from zig fmt: off/on I would really like the zig formatter being a bit smarter when it comes to table-data, ideally honoring the existing number of columns, and aligning the columns vertically by the delimiter (might be a bit much to ask though - so maybe zig fmt: off/on is indeed the better solution.

5 Likes

I’ve noticed that the formatter will preserve some table data, especially in array initialization. In this case it’s an enum which I think the formatter will always try to place each value on separate lines. So some of it depends on the kind of token it is looking at.
But I agree, having it preserve the number of columns even in the case of Enums would be nice.

1 Like

Relevant proposal: zig fmt: embrace alignment · Issue #17145 · ziglang/zig · GitHub

Currently, only array initializers have special logic for aligning elements like table. All other comma-separated list constructs can only be formatted as a single row/column.

5 Likes
// omg: begin
pub const Square = enum(u6) {
        a1,
        b1,
        c1,
        d1,
        e1,
        f1,
        ...
// omg: end

After one close encounter with this behavior I decided that I can format myself. The formatter messes up everything.
This is the line that every of my zig files starts with.

// zig fmt: off
1 Like

Personally I think you are too focused on what your personal style is, it is your choice, but I would rather have the benefits of using the default formatting for 99.9% of cases, instead of completely opting out of it.

If you don’t use the formatter ever, I would rather configure it to be off instead of use the comment to turn it off.

I also wouldn’t want to work on a code base where everyone follows their own favorite style, not allowing personal style also eliminates a lot of the discussions around formatting style. I think some formatting could be improved, but I can live with some imperfections if it means it becomes something that is automated (and doesn’t have to be bike-shed about all the time).

8 Likes

Why shouldnt we be personal? Even when working with a group on 1 codebase. As long as it is readable… I generally don’t like “everyone and everything the same”.
I turned off the zig formatter but once or twice it still triggered. That’s why I do this now.

Because using a formatter gets rid of a ton of unnecessary bikeshedding. This is a lesson I personally learned from go fmt. The Zig formattter certainly isn’t perfect, but I think the better solution is to make it better rather than completely ignore it.

But hey, if you want to express yourself in the form of code style, be my guest. It’s just that formattters save a ton of brain cycles: tons of times you don’t have to hit tab, or think about whether or not you need a trailing comma, or whether you should call a class HttpClient or HTTPClient.

Amd more importantly, during code review, no one has to waste time arguing about small details that have little to do with the code’s functionality or maintainability, they can instead focus on its content.

This counts double for a language that strives to “communicate intent precisely”. You can make the same piece of code look wildly different if you turn off formattters and put your mind to it.

2 Likes

Also, I very much appreciate that you can turn zig fmt off and on arbitrarily. A lot of formattters only let you turn them off at the line or maybe block level, so that’s a nice touch :slight_smile:

4 Likes

i’m using zls with nvim, that’s pretty cool. wanna take a try ?