-
Notifications
You must be signed in to change notification settings - Fork 138
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Extract gomodifytags to a library #117
base: main
Are you sure you want to change the base?
Conversation
677b892
to
95ee7b3
Compare
modifytags/gomodifytags.go
Outdated
|
||
ast.Inspect(node, rewriteFunc) | ||
|
||
c.Start = start |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks wrong. We should not be mutating the config. Perhaps this is meant to set the output start and end?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like the Start
and End
values aren't configured by the user, we just store them in the config. They're set by gomodifytags during the findSelection
step which uses the line, offset, or struct selection to determine the start and end position of fields that will be modified. (We don't use findSelection
in the new Apply routine since we are handling finding the selection on the gopls side).
modifytags/gomodifytags.go
Outdated
|
||
Fset *token.FileSet | ||
|
||
Remove []string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just did an experiment, that I think works out nicely:
There's this tension that many of the fields in the Config are actually related to the set up and tear down of the Run, and are not relevant to Apply. Specifically, if the Run is structured as Parse->Find->Apply->Format, many of these options are related to Parsing, Finding, and Formatting.
I think we should break up these fields into two structs, called something like Pass
and Mutation
. Pass holds everything above Remove, and Mutation holds everything in Remove and below.
The signature of Apply can then be:
func (m *Mutation) Apply(fset *token.FileSet, node ast.Node, mutation Mutation)
or maybe
func (m *Mutation) Apply(fset *token.FileSet, node ast.Node, start, end token.Pos)
(I'm not sure whether we need to pass start, end in gopls).
In fact, the more I read this, the more I think we should keep the Pass
objects in the main
package. We can of course move it here later if it is needed, but it is rather specific to the command line, and it seems prudent to keep the new API we are adding as minimal as possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I moved the Pass
fields and methods back into the main package, so now the modifytags
package only exposes Apply
, Rewrite
, Validate
, and the Mutation
and RewriteErrors
types used by the main run()
method
3085e5f
to
8aff27c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your patience! I have some high level comments about the API which, while superficial, I think we should sort out before finishing the review.
8aff27c
to
0f50ad5
Compare
This PR moves the gomodifytags tool into its package to be imported by other applications (i.e.: gopls). A new entrypoint into gomodifytags is provided via config.Apply(), which uses the specified start and end lines to apply struct tag modifications to the node without returning the new content. Also updates staticcheck version related: golang/vscode-go#2002
0f50ad5
to
1cfbd05
Compare
Thanks everyone for the thorough work. I welcome the new changes. On my end, the changes look great and I can merge it whenever you want. Please let me know if this finished and ready to be merged. I can cut a new release afterwards ( |
This PR moves the
gomodifytags
tool into its package to be imported by other applications (i.e.:gopls
).A new entrypoint into
gomodifytags
is provided via config.Apply(), which uses the specified start and end positions to apply struct tag modifications to the node without returning the new content.related: golang/vscode-go#2002