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

x/tools/gopls: add "Eliminate dot import" code action #70319

Open
adonovan opened this issue Nov 13, 2024 · 2 comments
Open

x/tools/gopls: add "Eliminate dot import" code action #70319

adonovan opened this issue Nov 13, 2024 · 2 comments
Labels
FeatureRequest gopls Issues related to the Go language server, gopls. help wanted Refactoring Issues related to refactoring tools Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@adonovan
Copy link
Member

adonovan commented Nov 13, 2024

Sometimes we regret using dot imports but they are too tedious to remove. It would be easy (and infallible actually fallible) to offer a code action to automate this transformation. We could also offer the reverse ("Convert to dot import"), but perhaps we should not place temptation in front of weak mortals. (The reverse operation is fallible.)

@adonovan adonovan added the Refactoring Issues related to refactoring tools label Nov 13, 2024
@gopherbot gopherbot added Tools This label describes issues relating to any tools in the x/tools repository. gopls Issues related to the Go language server, gopls. labels Nov 13, 2024
@gopherbot gopherbot added this to the Unreleased milestone Nov 13, 2024
@findleyr findleyr modified the milestones: Unreleased, gopls/backlog Nov 13, 2024
@findleyr
Copy link
Member

findleyr commented Nov 13, 2024

Sure, this could be useful, and would be easy to implement as it's just a sequence of simple scope checks and string replacements. I'll note that it wouldn't be (naively) infallible due to shadowing.

In short:

  1. search through types.Info.Uses for identifiers from the imported package in question
  2. find the scope at that identifier, and check that it does not contain the package name. If it does, either fail the operation or rename the package and start over
  3. produce an edit that qualifies the identifier
  4. if all qualification was successful, prepend an edit that removes the "." from the import.

As much as I'd like to get nerd-sniped, I'll leave this as a good help-wanted issue. If nobody picks it up, one of us can do it.

@findleyr
Copy link
Member

The opposite operation is essentially the same, except that the scope lookup is different, and we must fail if the scope lookup fails. However, since introducing dot imports violates the style guide, I'm less inclined to do it. If someone wants to contribute that direction as well, I'd vote for accepting the feature. (For symmetry, and because I personally am not that opposed to dot imports in tests :)).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest gopls Issues related to the Go language server, gopls. help wanted Refactoring Issues related to refactoring tools Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

3 participants