segmentio / golines

A golang formatter that fixes long lines
MIT License
903 stars 56 forks source link

Tag alignment/width doesn't respect comments #108

Open adriangoransson opened 11 months ago

adriangoransson commented 11 months ago

It seems that the inline spacing/width of tags does not take comments into account.

In the example below, we see that the sql tag is placed 42 characters before the string start. Which looks strange because the fields are not aligned by their name or types due to the comment.

type Example struct {
    A string `json:"some-really-really-long-identifier" sql:"-"`
    // SomeOtherField
    SomeOtherField []int `                                          sql:"-"`
}

If we were to remove the comment, everything looks nicely aligned.

type Example struct {
    A              string `json:"some-really-really-long-identifier" sql:"-"`
    SomeOtherField []int  `                                          sql:"-"`
}

Likewise if we inserted a blank line before the comment:

type Example struct {
    A string `json:"some-really-really-long-identifier" sql:"-"`

    // SomeOtherField
    SomeOtherField []int `sql:"-"`
}

This is the desired effect:

type Example struct {
    A string `json:"some-really-really-long-identifier" sql:"-"`
    // SomeOtherField
    SomeOtherField []int `sql:"-"`
}

Since names and types are only aligned within groups of non-comment lines I think tags should respect that as well.