Is there a gofmt flag to avoid reformatting comments?

175 views
Skip to first unread message

Marc Adkins

unread,
Apr 19, 2023, 5:11:58 PM4/19/23
to golang-nuts
I like gofmt a lot. I'm not as happy with godoc. Apparently the in the 1.19 release gofmt started reformatting comments to be consistent with godoc. I missed this in the release notes (which I may not have read, mea culpa).

For a long time this wasn't an issue for me but recently I've had gofmt "fix" something that IMHO wasn't broken (and in the process break it for my purposes). OK, it's a weird edge case related to specially formatted comments that are important to my IDE. So not all that important.

Still, I'd like to be able to turn off comment reformatting. I've looked but there doesn't seem to be a gofmt flag to avoid comments being reformatted for godoc. Am I missing something?

Ian Lance Taylor

unread,
Apr 19, 2023, 5:31:11 PM4/19/23
to Marc Adkins, golang-nuts
You aren't missing anything. The philosophy here is that this
reformatting, which only applies to doc comments, will ensure that the
comment looks acceptable as a comment, as HTML, as Markdown, and as
plain text. That necessarily restricts the kinds of things that can
appear. The exact docs are at https://go.dev/doc/comment.

If you give us more details perhaps there is some common ground
available. In particular, a sequence of lines where each line is
indented will be treated as a code block, and not reformatted.

Ian

Dan Kortschak

unread,
Apr 19, 2023, 10:06:46 PM4/19/23
to golan...@googlegroups.com
On Wed, 2023-04-19 at 14:30 -0700, Ian Lance Taylor wrote:
>
> If you give us more details perhaps there is some common ground
> available.  In particular, a sequence of lines where each line is
> indented will be treated as a code block, and not reformatted.

Related, I'd like to reiterate the concerns with quote rewriting (filed
here https://go.dev/issue/54312) which is an accessibility issue.

Marc Adkins

unread,
Apr 20, 2023, 10:52:59 AM4/20/23
to golang-nuts
GoLand IDE (Jetbrains) support found a solution. Per their instructions change the following pattern:

(update it in Preferences | Editor | TODO)

\/(\/|\*)[ ]*\btodo\b(.|\n)*(\*\/|)

It should highlight multiline TODOs without additional two spaces on the next line:

// TODO something something something
// something else
var x int

This is working for me.
Reply all
Reply to author
Forward
0 new messages