New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/gofmt: map key indentation resets on long-to-short keys or vice versa #20258
Comments
This is working as intended. gofmt has heuristics to drop indentation for consecutive tags of very different lengths. Closing as dup of #10392. |
FTR: OP's link https://play.golang.org/p/PXnPCVi6pz shows not a drop of indentation after formatting, its result looks like an off by one bug. |
AFAICT that second link is being treated the same as the following for indentation (note the blank line in the middle): var M = map[string]string{
"quite long map key": "bar",
"very long map key aaaa": "baz",
"shrt": "foo",
"mid size key": "aaa",
"slightly longer key": "bbb",
"very long map key bbbbb": "ccc",
} |
@benpaxton-hf See #10392 (comment) for an explanation. |
I read through that one, yeah. I guess I just don't see the point of having that heuristic; if Edit: for clarity, I am @benpaxton-hf on my non-work account. |
@GinjaNinja32 Your suggestion may be the right one. But we don't want to change this now as it would produce needless churn in many people's files. Perhaps a future gofmt 2. |
What version of Go are you using (
go version
)?go version go1.8.1 linux/amd64
What operating system and processor architecture are you using (
go env
)?GOARCH="amd64"
GOOS="linux"
What did you do?
Formatted my code with
gofmt
and received odd indentation.Try formatting the following code, which I have manually formatted as I expected it to be formatted:
https://play.golang.org/p/n8Heut5lhe
What did you expect to see?
The indentation of map keys would be consistent across the map unless broken by a blank line
What did you see instead?
The indentation of map keys resets if a very long key (20+ characters) is followed by a very short key (3-4 characters), or vice versa. The threshold is consistent for a given short or long key length, but doesn't appear to follow any pattern:
19 -> 1, 2, 3 (i.e. a 19-character key followed by a 1-, 2-, or 3-character key will break indentation, but an 18-character followed by these won't, nor will 19 followed by 4)
22 -> 4
26 -> 5
30 -> 6
This has some strange implications; for example, try formatting this: https://play.golang.org/p/PXnPCVi6pz
The text was updated successfully, but these errors were encountered: