vscode-go icon indicating copy to clipboard operation
vscode-go copied to clipboard

Allow for setting a package name when importing a package

Open makkes opened this issue 4 years ago • 5 comments

Is your feature request related to a problem? Please describe. Oftentimes when I import a package using the "Go: Add Import" feature I'd like to assign a package name to it that is different from the one specified by the imported package. Example:

import corev1 "k8s.io/api/core/v1"

Describe the solution you'd like I'd like to be able to assign a package name when importing a package name so that I don't have to add the import first, then scroll to the top of the file and change the imported package's name (and then scroll back to the original location I was working on).

Describe alternatives you've considered n/a

Additional context n/a

makkes avatar Jun 29 '21 08:06 makkes

This is currently implemented through gopls's gopls.add_import command. @marwan-at-work @stamblerre can the command extended to take an additional parameter?

hyangah avatar Sep 22 '21 23:09 hyangah

Somewhat related, but I just opened an issue to enhance the "Add Import" flow. This could potentially be a part of?

can the command extended to take an additional parameter?

It certainly can on the gopls side. However, I'm not sure how the UX will look like on the VSCode side. For example:

  1. Once we prompt the user with a list of packages, do we prompt them again to write a package name? That adds an extra step for those who don't want to rename their package.
  2. Would we expose a new command that would do the point flow? Something like Go: Add Renamed Import?
  3. I don't think this is an option but if VSCode can detect selecting an import vs. cmd+selecting an import, then we can have the best of both worlds where selecting an import just adds the import path as is but cmd+selecting an import would prompt the user again to rename the import path before adding it.

marwan-at-work avatar Sep 23 '21 01:09 marwan-at-work

Thanks @marwan-at-work

Other options are

  • Like 'Go: Add Tags', have a setting that controls the behavior (go.addTags.promptForTags). But I think we need more flexible option because a user may not always want the prompt.
  • Include a snippet in a text edit that places cursor before the imported package name. But that will cause the editor focus to change and may not be ideal.

I'd prefer the option 3 (cmd+selecting an item from quickpick), but I cannot find an easy way to implement this yet. The option 2 seems reasonable and users can define a separate key-binding for the new command.

hyangah avatar Sep 23 '21 12:09 hyangah

Option 2 sounds clear enough to me as well 👍🏼

marwan-at-work avatar Sep 24 '21 14:09 marwan-at-work

Any updates on this branch?

ciricc avatar Jan 28 '25 06:01 ciricc