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

feature request: autocomplete non imported packages #368

Closed
trajber opened this issue Aug 16, 2016 · 14 comments
Closed

feature request: autocomplete non imported packages #368

trajber opened this issue Aug 16, 2016 · 14 comments

Comments

@trajber
Copy link

trajber commented Aug 16, 2016

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.

output

@nsf
Copy link
Owner

nsf commented Aug 16, 2016

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.

@nhooyr
Copy link
Contributor

nhooyr commented Aug 21, 2016

If you're on neovim and use deoplete, zchee/deoplete-go implements this.

@th3noname
Copy link

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.
This means that i always need to get the information on the first method call from a Godoc page. At least if i don't know it off the top of my head.

I would prefer to get autocomplete suggestions from the most likely package than no suggestions at all.

@Olivia5k
Copy link

I would personally not really have any problems with gocode adding the import of "os" as soon as I type the dot in os.. If the identifier before the dot resolves to something, I'm probably about to import it anyway.

Could that be a nice compromise for this?

@th3noname
Copy link

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.

@zmb3
Copy link

zmb3 commented Aug 27, 2016

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.

@nsf
Copy link
Owner

nsf commented Aug 28, 2016

It should be very easy to add though, maybe I'll try that tomorrow.

@nhooyr
Copy link
Contributor

nhooyr commented Aug 28, 2016

@zmb3 that is exactly what zchee/deoplete-go does. It builds a json cache of the stdlib packages and uses that to provide completions.

nsf added a commit that referenced this issue Aug 28, 2016
Right now works on Go standard library packages only.

Feature is optional, can be enabled via: `gocode set unimported-packages true`.

Issue #368.
@nsf
Copy link
Owner

nsf commented Aug 28, 2016

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

@nsf
Copy link
Owner

nsf commented Aug 28, 2016

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.

@nsf
Copy link
Owner

nsf commented Aug 28, 2016

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())
}

@zmb3
Copy link

zmb3 commented Aug 28, 2016

Works like a charm, thanks!

@dmitshur
Copy link

dmitshur commented Aug 28, 2016

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.

goimports had to solve the same problem. Maybe it's a good idea to copy it's behavior, so that it's consistent. Otherwise it might be a bad experience if one tool tends to prefer similar package A but the other tool picks package B.

See https://github.com/golang/tools/commits/master/imports/zstdlib.go.

Also see relevant golang/tools@0835c73.

@nsf
Copy link
Owner

nsf commented Aug 28, 2016

@shurcooL I don't think it's very important.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants