Skip to content

x/tools/gopls: handle import aliases automatically #40278

Closed
@muirdm

Description

@muirdm

It is annoying when your file needs to import multiple packages with the same name. Tooling often offers no relief from having to manually import and alias the second package. It would be nice if gopls could help by automatically adding package import aliases when possible and appropriate.

I propose we do something like this to start:

  1. When loading packages we keep track of package aliases we see in existing imports. We could store the alias counts by containing package to avoid over counting as a package gets re-type checked.
  2. When offering unimported package completions, if a candidate package's name is already used by an existing import in the file, automatically include the "best" alias we have seen elsewhere for that package. We probably want to make the aliased unimported package addressable by both the alias and the package name since the user may or may not anticipate the alias.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeToolsThis label describes issues relating to any tools in the x/tools repository.goplsIssues related to the Go language server, gopls.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions