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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

madelinekalil
Copy link

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

@madelinekalil madelinekalil force-pushed the mkalil/create-package branch 2 times, most recently from 677b892 to 95ee7b3 Compare February 19, 2025 16:02

ast.Inspect(node, rewriteFunc)

c.Start = start

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?

Copy link
Author

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


Fset *token.FileSet

Remove []string

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.

Copy link
Author

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

@madelinekalil madelinekalil force-pushed the mkalil/create-package branch 2 times, most recently from 3085e5f to 8aff27c Compare February 25, 2025 21:03
Copy link

@findleyr findleyr left a 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.

@madelinekalil madelinekalil force-pushed the mkalil/create-package branch from 8aff27c to 0f50ad5 Compare March 3, 2025 20:17
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
@madelinekalil madelinekalil force-pushed the mkalil/create-package branch from 0f50ad5 to 1cfbd05 Compare March 5, 2025 16:25
@fatih
Copy link
Owner

fatih commented Mar 5, 2025

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 (v1.18.0)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants