segmentio / golines

A golang formatter that fixes long lines

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Tag alignment issue

nikcorg opened this issue · comments

I've found a slight issue in tags alignment.

When a block where any line has more than one tag, the tags are correctly aligned. However, if the block is then altered so that only one tag remains, I expected the tags to be realigned, but they are actually left as they are.

More details below:

$ golines --version
golines v0.7.0

When this is the starting point:

type demo struct {
	Thing string `bson:"thing" json:"thing"`
	Thang int    `json:"thang"`
}

Passing that to golines produces the expected result:

type demo struct {
	Thing string `bson:"thing" json:"thing"`
	Thang int    `             json:"thang"`
}

However, if I then remove the bson tag, I would expect the tags to again be realigned from:

type demo struct {
	Thing string ` json:"thing"`
	Thang int    `             json:"thang"`
}

Into:

type demo struct {
	Thing string `json:"thing"`
	Thang int    `json:"thang"`
}

But the actual result is the tags are left untouched by golines.

I decided to poke around in the codebase, and added in my case in the large fixture file with many structs, only to see the problem not manifest itself. I did some further digging and the behaviour only occurs when a source file doesn't have a single multi-key tag on any struct.

This means the bug isn't a bug in the formatting, but in the heuristic for skipping unnecessary work: the HasMultiKeyTags check. I'm not sure what the "correct" fix for this would be, so I will leave it lying for now.

That said, if the maintainers would like to suggest a preferred approach, I'm more than happy to have an attempt at producing a PR.

Maybe an option to be extra diligent and not worry about a slow pass (i.e. skip the heuristic), or perhaps adding a second heuristic, checking whether any tag is without leading whitespace (logically there should always be at least one per struct), when there are no multi-key tags? [Edit. having slept on it, the suggested heuristic won't work. It would need to check whether every struct in a file has at least one tag without leading whitespace, and it would still be unreliable.]

/cc: @yolken-segment

Same issue. Using golines v0.11.0

type MyStruct struct {
	ThingOne string     `json:"thingone"  binding:"required"`
	Things   []ThingTwo `json:"thingstwo"                    bidding:"required, gte=1"`
}