Merge pull request #28 from posener/gocomplete-improve
Gocomplete improve
This commit is contained in:
commit
a3e3c5274c
|
@ -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{
|
||||||
|
|
|
@ -1,76 +1,66 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"go/build"
|
||||||
"encoding/json"
|
"io/ioutil"
|
||||||
"os/exec"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/posener/complete"
|
"github.com/posener/complete"
|
||||||
)
|
)
|
||||||
|
|
||||||
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()
|
for {
|
||||||
pkgs := listPackages(dir)
|
prediction = complete.PredictFilesSet(listPackages(a)).Predict(a)
|
||||||
|
|
||||||
files := make([]string, 0, len(pkgs))
|
// if the number of prediction is not 1, we either have many results or
|
||||||
for _, p := range pkgs {
|
// have no results, so we return it.
|
||||||
files = append(files, p.Path)
|
if len(prediction) != 1 {
|
||||||
}
|
|
||||||
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
|
|
||||||
FilesString string
|
|
||||||
Files []string
|
|
||||||
}
|
|
||||||
|
|
||||||
func listPackages(dir string) (pkgs []pack) {
|
|
||||||
dir = strings.TrimRight(dir, "/") + "/..."
|
|
||||||
out, err := exec.Command("go", "list", "-f", goListFormat, dir).Output()
|
|
||||||
if err != nil {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
lines := bytes.Split(out, []byte("\n"))
|
|
||||||
for _, line := range lines {
|
// if the result is only one item, we might want to recursively check
|
||||||
var p pack
|
// for more accurate results.
|
||||||
err := json.Unmarshal(line, &p)
|
if prediction[0] == a.Last {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// only try deeper, if the one item is a directory
|
||||||
|
if stat, err := os.Stat(prediction[0]); err != nil || !stat.IsDir() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
a.Last = prediction[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func listPackages(a complete.Args) (dirctories []string) {
|
||||||
|
dir := a.Directory()
|
||||||
|
complete.Log("listing packages in %s", dir)
|
||||||
|
// import current directory
|
||||||
|
pkg, err := build.ImportDir(dir, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
complete.Log("failed importing directory %s: %s", dir, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
dirctories = append(dirctories, pkg.Dir)
|
||||||
|
|
||||||
|
// import subdirectories
|
||||||
|
files, err := ioutil.ReadDir(dir)
|
||||||
|
if err != nil {
|
||||||
|
complete.Log("failed reading directory %s: %s", dir, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, f := range files {
|
||||||
|
if !f.IsDir() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// parse the FileString from a string "[file1 file2 file3]" to a list of files
|
pkg, err := build.ImportDir(filepath.Join(dir, f.Name()), 0)
|
||||||
p.Files = strings.Split(strings.Trim(p.FilesString, "[]"), " ")
|
if err != nil {
|
||||||
pkgs = append(pkgs, p)
|
complete.Log("failed importing subdirectory %s: %s", filepath.Join(dir, f.Name()), err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
dirctories = append(dirctories, pkg.Dir)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -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