Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Support "Go: Autocomplete Unimported Packages" feature when using Go modules #2484

Closed
ramya-rao-a opened this issue May 1, 2019 · 15 comments
Labels
go-modules Related to Go modules help wanted

Comments

@ramya-rao-a
Copy link
Contributor

Enabling the setting go.autocompleteUnimportedPackages used to provide completions for unimported packages. When such a completion item was selected by the user, not only was the current word completed, but an import statement is added in the file for that package.

This was implemented by going over all the packages under the GOPATH and keeping a cache of this result.

With Go modules, it is not clear how we can provide this feature.

@stamblerre Is this something that gopls can do as part of the completion provider?

@wxdao
Copy link

wxdao commented May 7, 2019

Maybe at least make it to work with packages under the current module and those in vendor folder? They are presented in source code files and the good old going-over approach should work.

@stamblerre
Copy link
Contributor

@ramya-rao-a, we are definitely planning on supporting this as part of gopls. I filed golang/go#31906 to track it. It's not as high priority as some other features, but it is definitely on our list.

@ramya-rao-a
Copy link
Contributor Author

Thanks @stamblerre
Once this feature makes it into gopls (assuming that it will work both for non modules set up as well), please log an issue or create a PR here to remove the similar feature here to avoid duplicate suggestions

Closing this issue in favor of the upstream issue.

@ramya-rao-a ramya-rao-a added the upstream-gopls Issue for gopls label May 9, 2019
@ramya-rao-a
Copy link
Contributor Author

Oh closed too soon!

Maybe at least make it to work with packages under the current module and those in vendor folder? They are presented in source code files and the good old going-over approach should work.

That is an interesting take, how do you suggest we get the list of such packages? Would go list provide this information?

@keyvchan
Copy link

keyvchan commented May 9, 2019

I think there is a way that caching standard library's packages information to some place, because everybody would use it everyday and itself isn't change too often. And provide the list of dependencies's by parsing go.mod.

@paivagustavo
Copy link

paivagustavo commented May 9, 2019

Doesn't go list -m all suffices for this case? It lists all dependencies, including indirect ones.

@paivagustavo
Copy link

With -m flag we only get the list of modules. If we drop it out, like go list all, we get the list of all packages, that is what we may need for this.

On modules it includes all dependencies' packages and standard library's packages as well.

@ramya-rao-a
Copy link
Contributor Author

Thanks @fintanchen and @paivagustavo
@stamblerre Is that what you would suggest to get list of packages that can be considered as valid imports?

@ianthehat
Copy link
Contributor

There is no fast way to do what you are trying to do here with existing tools.
If you want to see the kind of logic you need, you can look at the things that goimports does to try to find new imports, which is essentially the task you are trying to do here as well.
I don't think there is anything you could reasonable do here that it would not be easier and better to just add directly to gopls instead.

@ramya-rao-a
Copy link
Contributor Author

Thanks @ianthehat

If anyone wants to help with the issue, please look at golang/go#31906 to help contribute a way to get this feature from gopls

I'll keep this issue open just in case if anyone wants to give the go list option a try.

@colinlabs
Copy link

saibing/tools support "Autocomplete Unimported Packages", can this help with the issue?

@stamblerre
Copy link
Contributor

golang/go#31906 has been closed upstream, and this will be supported by default in gopls with gopls/v0.3.0, so I think that this issue can be resolved.

@ramya-rao-a
Copy link
Contributor Author

That's great news @stamblerre

Is there a way one can disable this feature?
When using this feature from the extension, one has to explicitly opt in using the go.autocompleteUnimportedPackages setting.
The reason behind doing so was that there was early feedback on this feature being noisy for some.

@stamblerre
Copy link
Contributor

It can be disabled by setting the "completeUnimported" value to false, but this feature has been refined a lot in gopls, so I expect that it will work a lot better for people than gocode did. gopls has careful ranking and relies on an exact package name match, so hopefully few people will want to turn it off.

@ramya-rao-a
Copy link
Contributor Author

Thanks @stamblerre

Closing this issue as the feature will be available in the next version gopls/v0.3.0 of the language server. Folks who want to try the feature out before the release can do so by downloading gopls by running

GO111MODULE=on 
go get golang.org/x/tools/gopls@master golang.org/x/tools@master

@vscodebot vscodebot bot locked and limited conversation to collaborators Mar 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
go-modules Related to Go modules help wanted
Projects
None yet
Development

No branches or pull requests

7 participants