Stream: compiler development

Topic: zig-compiler - Formatter and style


view this post on Zulip Anthony Bullard (Mar 17 2025 at 11:31):

I want to make this thread so I can ask for pointed feedback on the style of the formatter and any general questions about expectations around formatting

view this post on Zulip Anthony Bullard (Mar 17 2025 at 11:32):

I have this EXTREMELY OVER COMMENTED block of code. Does this look how people could expect it to look if someone had typed this in? I.e., should this parse and format stably? (Please ignore \\ for zig multiline comments)

const stmt =
        \\import # Comment here
        \\    pkg # And here
        \\        .Something # Another one
        \\            exposing # Still commenting
        \\                [ # I really like comments
        \\                    func # Commenting for life
        \\                        as # Even here
        \\                            function, # And here
        \\                    Type # Commenting again
        \\                        as # And again...
        \\                            ValueCategory, # Why can't I stop?
        \\                    Custom # This is getting ridiculous
        \\                        .* # Even Uncle Bob is emabarrased
        \\                ] # And...I'm done
;

view this post on Zulip Joshua Warner (Mar 17 2025 at 11:38):

Seems not unreasonable

view this post on Zulip Anthony Bullard (Mar 17 2025 at 11:40):

I guess the big question is: is the principle that in a continuation where there is a comment not at end of line (and not in some collection) that we indent on the next newline and continue?

view this post on Zulip Anthony Bullard (Mar 17 2025 at 11:42):

Or should things like qualifier and identifier proceed at the same level of indent?

view this post on Zulip Anthony Bullard (Mar 17 2025 at 12:07):

The alternative would be:

const stmt =
        \\import # Comment here
        \\    pkg # And here
        \\    .Something # Another one
        \\        exposing # Still commenting
        \\            [ # I really like comments
        \\                func # Commenting for life
        \\                    as # Even here
        \\                    function, # And here
        \\                Type # Commenting again
        \\                    as # And again...
        \\                    ValueCategory, # Why can't I stop?
        \\                Custom # This is getting ridiculous
        \\                    .* # Even Uncle Bob is emabarrased
        \\            ] # And...I'm done
;

view this post on Zulip Anthony Bullard (Mar 17 2025 at 12:09):

I'll also write up an example with complex binops as well. There are SO many places where comments could be inserted there

view this post on Zulip Joshua Warner (Mar 17 2025 at 12:19):

On balance, less rightward drift seems good - but this is such an unusual situation that I’m not particularly concerned about it

view this post on Zulip Sam Mohr (Mar 17 2025 at 22:33):

I'd push for less rightward drift whenever consistently possible, so the second example is preferable to me

view this post on Zulip Brendan Hansknecht (Mar 17 2025 at 22:42):

Hmm. I think it should be consistent with what we would do for a function chain

x
    .y
    .transform()
    .run!()

So I would vote for this:

const stmt =
        \\import # Comment here
        \\    pkg # And here
        \\        .ExtraNesting # not sure this is actualy valid, but just an example of more nesting
        \\        .Something # Another one
        \\            exposing # Still commenting
        \\                [ # I really like comments
        \\                    func # Commenting for life
        \\                        as # Even here
        \\                        function, # And here
        \\                    Type # Commenting again
        \\                        as # And again...
        \\                        ValueCategory, # Why can't I stop?
        \\                    Custom # This is getting ridiculous
        \\                        .* # Even Uncle Bob is emabarrased
        \\                ] # And...I'm done
;

view this post on Zulip Richard Feldman (Mar 17 2025 at 22:46):

yeah I agree with that

view this post on Zulip Richard Feldman (Mar 17 2025 at 22:46):

I don't think anyone is actually going to write comments in all these places, so I don't think the quantity of indentation really matters here

view this post on Zulip Richard Feldman (Mar 17 2025 at 22:47):

I think what's more important is that if you put comments in a small subset of these places, it looks consistent with what we do elsewhere


Last updated: Jul 06 2025 at 12:14 UTC