complete icon indicating copy to clipboard operation
complete copied to clipboard

Module aware go completion

Open AdamSLevy opened this issue 4 years ago • 3 comments

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.

AdamSLevy avatar Feb 28 '20 18:02 AdamSLevy

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.

posener avatar Feb 29 '20 08:02 posener

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.

AdamSLevy avatar Feb 29 '20 23:02 AdamSLevy

Sounds good. Enjoy it!

posener avatar Mar 02 '20 14:03 posener