-
Notifications
You must be signed in to change notification settings - Fork 18k
cmd/gofmt: proposal: Minimize adjacent whitespace changes on struct layout edits (user controlled positions). #37299
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
Comments
My first impression is that this proposal adds a fair bit of complexity to gofmt without a whole lot of benefit. I don't believe there exists any other mechanisms like this that allow you to influence gofmt's output, and I see that as mostly a good thing. When I write a struct with many fields, I often find myself dividing them up into logical sections. Since sections of fields with a blank line or comment in between are grouped independently of each other, I don't end up with massive diffs every time I add or change a field name. This isn't something I do to appease gofmt, it's just a way I find helps me organize the fields. type ClientConfig struct {
// Connection info
hostname string
port int
scheme string
retryCount int
// Authentication
username string
password string
} I think it's also worth mentioning that it is possible in most cases to hide whitespace-only changes when diffing. For Git, that's I'm not saying there isn't a problem here. I've certainly seen my fair share of Go diffs that are messier than they need to be because of this. However, I think it will be difficult to find a change to gofmt whose complexity or disruptiveness can be justified to solve what I find to be no more than a minor annoyance. For me, this proposal doesn't meet that bar. |
Remarks regarding allowing user-settable format are valid. I reopened proposal using less controversial solution as #37399 |
Not only humans do diffs. This proposal was about making changesets smaller in the repo, so to have changeset truly reflecting changes made by a human author.
Please see the simplified (37399) proposal. |
Proposal: Minimize adjacent whitespace changes on struct layout edits.
Author: Ohir Ripe [Wojciech S. Czarnecki]
Last updated: 2020/02/24
Discussion at https://golang.org/issues/37299
Abstract
I propose to add an opt-in go fmt solution meant to minimize whitespace changes to the block where a code author hinted at desired comments position.
Background
With current gofmt rules slight changes to a struct declaration make for avalanche changes across declaration whitespace due to gofmt trying to layout struct declaration "pretty". Produced changeset size make diffs unreadable if any field identifier or type changes its length.
Proposal
Comment-start hint has a form of a
/<
digraph (a cs-mark hereafter) put into the comment that follows the opening brace of the block.Note: cs-mark usually will be used on a struct declaration, but described feature will work for any kind of block.
Proposed new gofmt rules:
Caveat: If the block opening brace changes position pre or past its current tabstop span (eg. if a struct was renamed), whole block will make to the changeset. These changes though are less likely than changes to the inner structure.
Rationale
As explained in the background. This change will benefit Go programmers that use source control. Ie. likely all of them.
Compatibility
This is an opt-in feature. It does not affect already written code.
Implementation
None yet. It can successfully be implemented only by the core team.
(Because such a change makes sense if implemented across the whole Go ecosystem.)
Open issues
Edits
The text was updated successfully, but these errors were encountered: