Skip to content
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

Closed
benpaxton-hf opened this issue May 5, 2017 · 6 comments
Closed

Comments

@benpaxton-hf
Copy link

@benpaxton-hf benpaxton-hf commented May 5, 2017

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

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented May 5, 2017

This is working as intended. gofmt has heuristics to drop indentation for consecutive tags of very different lengths. Closing as dup of #10392.

@cznic
Copy link
Contributor

@cznic cznic commented May 5, 2017

This is working as intended. gofmt has heuristics to drop indentation for consecutive tags of very different lengths.

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.

@benpaxton-hf
Copy link
Author

@benpaxton-hf benpaxton-hf commented May 5, 2017

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",
}
@griesemer
Copy link
Contributor

@griesemer griesemer commented May 5, 2017

@benpaxton-hf See #10392 (comment) for an explanation.

@GinjaNinja32
Copy link

@GinjaNinja32 GinjaNinja32 commented May 5, 2017

I read through that one, yeah. I guess I just don't see the point of having that heuristic; if gofmt without that code produces highly-indented blocks, then the developer is using extremely long map keys; in that case, why not just suggest a blank line between the excessively-long keys and the short ones? What differs between the case of a struct with a very long variable or type name and the case of a map with a very long key that the heuristic is only used for one?

Edit: for clarity, I am @benpaxton-hf on my non-work account.

@griesemer
Copy link
Contributor

@griesemer griesemer commented May 5, 2017

@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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants
You can’t perform that action at this time.