gofmt reformats comments

Answered

As of Go 1.19 gofmt now reformats doc comments to make their rendered meaning clearer. Basically gofmt is trying to reformat comments for use in godoc.

Today I noticed that this:

// Note: something something something
//  something else
var x int

is reformatted to:

// Note: something something something
//
//	something else
var x int

I had a very long note, configured in GoLand to be green, and I did what I'm supposed to do, splitting it and indenting the second line. The second line gets interpreted as a block quote or code block or something and you see the result.

This only happens if there is a declaration after the comment. It happens with comment blocks as well.

This is all by design apparently. I've found a variety of references to issues that have occurred due to these changes. So it's not just me.

I use GoLand and I like the configurable TODO/NOTE/BUG blocks with the color coding. However the way long lines of this type are broken up conflicts with godoc style and therefore they can't be used in any comment attached to a declaration.

0
2 comments

Hi Marc,

Thanks for sharing. Indeed, we should provide a different pattern for TODO comments in the case of Go language.

Could you please try out the following pattern instead (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
0

That seems to work, thanks!

0

Please sign in to leave a comment.