Formatter add a new line between type declaration and const values

Answered

Goland's code formatter add a new line between type declaration and const values and I found no option to disable it.

Here's an example:

type PartyMemberFlag uint8

const (
PMFlagUndef PartyMemberFlag = iota + 0 // 0
)

The options don't mention anything like this:

0
4 comments

Hello,

GoLand formatter is compatible with gofmt rules in most cases and you can only change options that satisfy gofmt rules. gofmt adds leading new lines between type declarations and const values. That's why there is no way to configure it in GoLand.

Could you please expand this case? Why would it be helpful to have that option if gofmt adds new lines anyway? Please keep in mind that gofmt is a de-facto standard formatting tool in Go world. For example, your coworkers may break your custom formatting rules if they're using gofmt.

0

I concluded that it was done by GoLand and not by gofmt, hence my original question.

I turned off the option to run gofmt on code reformat in Editor->Code Style->Go ->  "other" tab.

Then I ran code reformat using Ctrl+Alt+L and it added a new line. It follows that it's done by GoLand and not by gofmt. Where did I go wrong in my thinking?

0

Sorry for not being clear.

You're right. It comes from GoLand formatter. However, GoLand formatter relies on gofmt rules. So, if gofmt formats a code in this way, GoLand formatter will format it accordingly.

It doesn't depend on Run gofmt on code reformat option. GoLand formatter inherits most of the gofmt rules and formatting. This option exists to confirm that GoLand formatter options and gofmt reformat your code properly according to Go standards.

You can think of it in this way:

  • Invoke gofmt on your code snippet.
  • If gofmt adds new lines, then GoLand formatter should add them too. Otherwise, it may produce unexpected results (e.g., you've configured formatting rules, then gofmt break them and vice versa).
  • If gofmt doesn't add new lines, then GoLand shouldn't add them, or there should be an option to configure it.

Your case is the second one. If we assume that the option to configure it exists, then gofmt someday will break your custom rules.

0

I see. Thanks for the clarification.

0

Please sign in to leave a comment.