-
Notifications
You must be signed in to change notification settings - Fork 657
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
feature request: autocomplete non imported packages #368
Comments
People really hate import statements in golang aren't they. While I'll leave the issue open, I don't think it happens any time soon. I understand why it can be useful, but gocode is only a part of equation here. Say gocode fails to resolve an identifier and then it tries to import a package that may fit. Do I return back the autocompletion result and a suggestion to add an import statement? What if editor doesn't follow that suggestion and doesn't add an import statement? User can be confused why gocode autocompletes something and compilation fails at the end. Need to think about it. |
If you're on neovim and use deoplete, zchee/deoplete-go implements this. |
I use the Atom editor with joefitzgerald/go-plus and have a similar problem. If the package is not yet included in the import statement, then i don't get autocomplete suggestions and i need to open Godoc. The "problem" is that go-plus also uses goimports. If i now add the package to the import statement and save the file without using the package in the code, goimports removes it from the import statement. I would prefer to get autocomplete suggestions from the most likely package than no suggestions at all. |
I would personally not really have any problems with gocode adding the import of Could that be a nice compromise for this? |
I think gocode doesn't normally modify the source code on its own. And there already is goimports to do that. In my opinion it would be the best if gocode searched for the best fitting package (in most cases probably only one). Maybe prefer stdlib to external packages. This functionality could be hidden behind a cli flag or a build parameter. |
I'd be interested in working on this if we can come to a consensus on how it should be done. IMO, gocode doesn't need to add the import - we have goimports for that. I think a good first step would be to only offer suggestions for stdlib packages. It would keep the implementation simple and would offer immediate value. |
It should be very easy to add though, maybe I'll try that tomorrow. |
@zmb3 that is exactly what zchee/deoplete-go does. It builds a json cache of the stdlib packages and uses that to provide completions. |
Right now works on Go standard library packages only. Feature is optional, can be enabled via: `gocode set unimported-packages true`. Issue #368.
As usual, to try it out, don't forget to kill previously running gocode server: go get -u github.com/nsf/gocode
gocode close
gocode set unimported-packages true
...
PROFIT |
Right now gocode matches identifier against a built-in list of known package identifiers, you can find it here: https://github.com/nsf/gocode/blob/master/cursorcontext.go#L414-L557 Note that even in standard library there are some conflicts, like "text/template" and "html/template". I've made choices to prefer one over another based on my idea of popularity of those packages. |
Program used for extracting default package identifiers: package main
import "os"
import "go/importer"
func main() {
pkgName := os.Args[1]
pkg, err := importer.Default().Import(pkgName)
if err != nil {
panic(err)
}
os.Stdout.WriteString(pkg.Name())
} |
Works like a charm, thanks! |
See https://github.com/golang/tools/commits/master/imports/zstdlib.go. Also see relevant golang/tools@0835c73. |
@shurcooL I don't think it's very important. |
The Go plugin for IntelliJ has a very useful feature that allows code completion works even when the package is not imported yet.
On this example you can see "fmt" and "os" packages being used even with no imports at all.
The text was updated successfully, but these errors were encountered: