gocomplete: go run should also predict non-main functions

This commit is contained in:
Eyal Posener 2017-05-18 23:40:02 +03:00
parent dce08717c1
commit b3f5ec22d3
3 changed files with 1 additions and 37 deletions

View File

@ -44,7 +44,7 @@ func main() {
Flags: complete.Flags{
"-exec": complete.PredictAnything,
},
Args: complete.PredictFunc(predictRunnableFiles),
Args: goFiles,
}
test := complete.Command{

View File

@ -4,8 +4,6 @@ import (
"bytes"
"encoding/json"
"os/exec"
"path/filepath"
"regexp"
"strings"
"github.com/posener/complete"
@ -13,9 +11,6 @@ import (
const goListFormat = `{"Name": "{{.Name}}", "Path": "{{.Dir}}", "FilesString": "{{.GoFiles}}"}`
// regexp matches a main function
var reMainFunc = regexp.MustCompile("^main$")
func predictPackages(a complete.Args) (prediction []string) {
dir := a.Directory()
pkgs := listPackages(dir)
@ -27,27 +22,6 @@ func predictPackages(a complete.Args) (prediction []string) {
return complete.PredictFilesSet(files).Predict(a)
}
func predictRunnableFiles(a complete.Args) (prediction []string) {
dir := a.Directory()
pkgs := listPackages(dir)
files := []string{}
for _, p := range pkgs {
// filter non main pacakges
if p.Name != "main" {
continue
}
for _, f := range p.Files {
path := filepath.Join(p.Path, f)
if len(functionsInFile(path, reMainFunc)) > 0 {
files = append(files, path)
}
}
}
complete.Log("FILES: %s", files)
return complete.PredictFilesSet(files).Predict(a)
}
type pack struct {
Name string
Path string

View File

@ -47,16 +47,6 @@ func TestPredictions(t *testing.T) {
predictor: complete.PredictFunc(predictPackages),
last: "X",
},
{
name: "predict runnable ok",
predictor: complete.PredictFunc(predictRunnableFiles),
completion: []string{"complete.go"},
},
{
name: "predict runnable not found",
predictor: complete.PredictFunc(predictRunnableFiles),
last: "X",
},
}
for _, tt := range tests {