gocomplete: go run should also predict non-main functions
This commit is contained in:
parent
dce08717c1
commit
b3f5ec22d3
|
@ -44,7 +44,7 @@ func main() {
|
||||||
Flags: complete.Flags{
|
Flags: complete.Flags{
|
||||||
"-exec": complete.PredictAnything,
|
"-exec": complete.PredictAnything,
|
||||||
},
|
},
|
||||||
Args: complete.PredictFunc(predictRunnableFiles),
|
Args: goFiles,
|
||||||
}
|
}
|
||||||
|
|
||||||
test := complete.Command{
|
test := complete.Command{
|
||||||
|
|
|
@ -4,8 +4,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/posener/complete"
|
"github.com/posener/complete"
|
||||||
|
@ -13,9 +11,6 @@ import (
|
||||||
|
|
||||||
const goListFormat = `{"Name": "{{.Name}}", "Path": "{{.Dir}}", "FilesString": "{{.GoFiles}}"}`
|
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) {
|
func predictPackages(a complete.Args) (prediction []string) {
|
||||||
dir := a.Directory()
|
dir := a.Directory()
|
||||||
pkgs := listPackages(dir)
|
pkgs := listPackages(dir)
|
||||||
|
@ -27,27 +22,6 @@ func predictPackages(a complete.Args) (prediction []string) {
|
||||||
return complete.PredictFilesSet(files).Predict(a)
|
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 {
|
type pack struct {
|
||||||
Name string
|
Name string
|
||||||
Path string
|
Path string
|
||||||
|
|
|
@ -47,16 +47,6 @@ func TestPredictions(t *testing.T) {
|
||||||
predictor: complete.PredictFunc(predictPackages),
|
predictor: complete.PredictFunc(predictPackages),
|
||||||
last: "X",
|
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 {
|
for _, tt := range tests {
|
||||||
|
|
Loading…
Reference in New Issue