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

Module aware go completion #115

Open
AdamSLevy opened this issue Feb 28, 2020 · 3 comments
Open

Module aware go completion #115

AdamSLevy opened this issue Feb 28, 2020 · 3 comments

Comments

@AdamSLevy
Copy link
Contributor

I use this package's go completion daily and I really appreciate it. However the completion suggestions for packages is both limited and now outdated as it relies on GOPATH, which is effectively deprecated.

I would like the go completion to take advantage of go list to obtain a module aware list of suggestions. This can even be used to greatly improve the use of go doc and complete not just package name but also symbols from the package. The suggestions can even take into account the -u flag and offer unexported completion suggestions as well.

This can fall back to searching the module cache if no matching name is in the local projects go.mod file, and finally it could fall back to searching $GOPATH/src/ for package name completions.

I did a little research and discovered that there is no package that exposes the same functionality as go list however it has well defined JSON output option so parsing the output is trivial.

I am interested in submitting a PR for this but wanted to discuss it in an issue first.

@posener
Copy link
Owner

posener commented Feb 29, 2020

I did a little research and discovered that there is no package that exposes the same functionality as go list however it has well defined JSON output option so parsing the output is trivial.

I would first ask in go-nuts group, or at Go slack if anyone knows on such API, or what is the best practice to do it. Shelling out and decoding the json can be last resort.

I am interested in submitting a PR for this but wanted to discuss it in an issue first.

This sounds great. I would appreciate such a PR.

@AdamSLevy
Copy link
Contributor Author

Good suggestion. I asked around on the Go slack and got some suggestions. I was able to write some test code that lists all names within a package's scope using golang.org/x/tools/go/packages. This list can be filtered using ast.IsExported.

	cfg := Config{Mode: NeedName | NeedTypes | NeedTypesInfo}
	pkgs, err := packages.Load(&cfg, "github.com/pkg/xattr")
	if err != nil {
		log.Fatal(err)
	}
	pkg := pkgs[0]
	fmt.Printf("%+#v\n", pkg.Types.Scope().Names())

A list of imports can be obtained by setting the right Mode.

I'm working on a PR that updates the gocomplete tool with the latest 1.14 flags and I'm starting to play with completion for godoc.

@posener
Copy link
Owner

posener commented Mar 2, 2020

Sounds good. Enjoy it!

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

No branches or pull requests

2 participants