Format if condition multiline

Is there a way to format an if statement that the or and keyword go to the beginning of the line instead of the end?
So instead of

if (something_long or
    something_other_long or
    next_long)

do this:

if (something_long
    or something_other_long
    or next_long)

The autoformatter always chooses the first but I really like the second.

1 Like

I don’t believe so, at least not without altering the formatter code yourself which is probably not what you had in mind with this question.

If I have learned one thing with programming languages, it simply causes friction to not embrace the conventional style, regardless of personal opinions. I have definitely adopted the ā€œdo as the Romans doā€ mentality when it comes to language conventions and formatters.

(I prefer the second style too)

5 Likes

I agree that accepting the ways of the formatter is probably best, but there’s always the possibility to temporarily disable zig fmt from inside your code:

switch (cond) {
// zig fmt: off
    .one_thing               => foo(),
    .another_thing           => bar(),
    .something_else_entirely => baz(),
// zig fmt: on
}

FWIW I also started to prefer putting the boolean operators at the front of the line, it is more readable than the ā€˜traditional’ way (IMHO). I wouldn’t mind this becoming the standard formatting in zig fmt.

5 Likes