How to config `zig fmt` to leave my doc strings alone

Is there a way to config the zig fmt so it doesn’t break my doc strings?

When I use zig fmt file.zig on a code like this one:

/// this is the first line.<Space><Space>
/// this is the second line.<Space><Space>
fn main() !void {

}

it turns to

/// this is the first line.
/// this is the second line.
fn main() !void {

}

If it isn’t obvious what happened, it took out the trailing white spaces in the doc comments. Which is a problem, as it is the only way of adding a linebreak in hoverable comments that I have found so far.

2 Likes

I believe their should be a doc langage for zig, like doxygen is to C/C++, Java, Python, etc… It would help standardize everything.

zig uses markdown

zig fmt is just ignorant of that, I expect it will be changed in the future.

I’m not sure what exactly zig docs support regarding markdown, but <br> should work, \ may also work but its less likely to be supported.

the formatting doesnt matter too much anyway.

1 Like

Don’t think I agree with that view. It matters if you want the doc system to be used. It’s just not currently used much beyond the (rather light) std docs.

5 Likes

i think you can do something like

// zig fmt: off
[stuff]
// zig fmt: on

I believe the issue is that there are too many version of markdown, as your hesitation between \ and <br> shows. This will lead to further incompatibilities as slightly different version/implementation of markdown are used by zig users. We can’t embed a lot of things in standard markdown. Having graphs would be an improvement for example.

2 Likes

Zig formatter removes whitespace from the end of comments (well, all lines). This is intentional.

In your case, why not have an empty line if you need a line break? Instead relying on a mystical double space to insert a line break, put in the new lines yourself.

/// this is the first line.
///
/// this is the second line.
///
fn main() !void {}

And markdown expects two newlines to put space between paragraphs or elements. This is standard markdown, so putting in the newlines yourself makes more sense then the two spaces.

9 Likes

like others said, in Markdown, you add an empty new line between two paragraphs.

I will take the chance to shill zift (a fork of the zig formatter), but note it retains the trimming lines end thing. If you’d like to add a config for it yourself (and send a patch for it) you can use that.

1 Like

There is a difference between two spaces and two newlines, though. In Commonmark, two newlines split paragraphs[1] (i.e. <p>...</p>), while two spaces at the end of a line denote a hard line break[2] inside of a single paragraph (i.e. <br>). Those are two semantically different things.


  1. CommonMark Spec ↩︎

  2. CommonMark Spec ↩︎

5 Likes

Significant trailing spaces are even worse than significant leading spaces. It’s an abomination. Use a markdown variant that supports the common-sense trailing \for hard line break.

4 Likes

that could work for programs that generate a doc website from doc comments, but I don’t think the tools used for in-IDE doc comments allow you to simply choose to use a different Markdown dialect…

We are talking here about entirely human facing documentation.

Formatting very much DOES matter.

To make an example: Would you like to read a book where every page is formatted as just one big paragraph, or where there are multiple paragraphs?

3 Likes

I was not saying formatting doesn’t matter, just that it doesn’t have to be the best.

I do think zig should fix this, and I do think it should allow you to write good, readable, docs.

But I don’t think it is or should be a priority, as you can still make readable docs at the moment, even if they aren’t as readable as you’d like them to be.

I am aware of this. But, I am hesitant to use it as the doc comment appears too spread out for someone trying to read from the src code. Especially when we are talking about 5+ lines.

I have tried a few tips that were shared earlier, and I have found this to work. ( ‘-’ represents Space ).

/// This is a multiline--\
/// doc comment--\
fn doSomething() !void {
    if (!want_to_do_something) return error.NoIWont;
}
4 Likes

There’s usually a difference between a paragraph break and a hard line break: the paragraph break adds extra space between the lines but a hard line break simply breaks the line at that point and the following lines don’t have any additional vertical space. The distinction can be useful sometimes.

1 Like

No. No there is not.

You can, however, tell it to leave your code alone:

Which is not configuration. But that’s it.

I want to go on record as saying that I have always considered this brain damaged, and don’t want Zig’s Markdown flavor to repeat this mistake.

If a format has assigned semantic meaning to trailing whitespace, well, shame on that format.

5 Likes

I think zig fmt trims comments even with the directives.

EditL just checked. it does.

Not on my computer!

Because I have my editor do that before zig fmt ever sees it. Trailing whitespace, really? Eww.

You might be right, I have no idea. Good for zig fmt I guess.