2017-05-12 16:17:48 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-05-18 16:11:30 -05:00
|
|
|
"go/build"
|
|
|
|
"io/ioutil"
|
2019-11-13 22:51:44 -06:00
|
|
|
"log"
|
2017-07-28 07:01:07 -05:00
|
|
|
"os"
|
2017-07-30 12:41:18 -05:00
|
|
|
"os/user"
|
2017-05-18 16:11:30 -05:00
|
|
|
"path/filepath"
|
2017-07-28 07:01:07 -05:00
|
|
|
"strings"
|
2017-05-12 16:17:48 -05:00
|
|
|
|
2019-11-17 17:25:16 -06:00
|
|
|
"github.com/posener/complete/v2/predict"
|
2017-05-12 16:17:48 -05:00
|
|
|
)
|
|
|
|
|
2017-05-19 03:59:51 -05:00
|
|
|
// predictPackages completes packages in the directory pointed by a.Last
|
|
|
|
// and packages that are one level below that package.
|
2019-11-13 22:51:44 -06:00
|
|
|
func predictPackages(prefix string) (prediction []string) {
|
|
|
|
prediction = []string{prefix}
|
2017-07-28 07:01:07 -05:00
|
|
|
lastPrediction := ""
|
|
|
|
for len(prediction) == 1 && (lastPrediction == "" || lastPrediction != prediction[0]) {
|
|
|
|
// if only one prediction, predict files within this prediction,
|
|
|
|
// for example, if the user entered 'pk' and we have a package named 'pkg',
|
|
|
|
// which is the only package prefixed with 'pk', we will automatically go one
|
|
|
|
// level deeper and give the user the 'pkg' and all the nested packages within
|
|
|
|
// that package.
|
|
|
|
lastPrediction = prediction[0]
|
2019-11-13 22:51:44 -06:00
|
|
|
prefix = prediction[0]
|
|
|
|
prediction = predictLocalAndSystem(prefix)
|
2017-05-18 16:11:30 -05:00
|
|
|
}
|
2017-07-28 07:01:07 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-13 22:51:44 -06:00
|
|
|
func predictLocalAndSystem(prefix string) []string {
|
|
|
|
localDirs := predict.FilesSet(listPackages(directory(prefix))).Predict(prefix)
|
2017-07-28 07:01:07 -05:00
|
|
|
// System directories are not actual file names, for example: 'github.com/posener/complete' could
|
|
|
|
// be the argument, but the actual filename is in $GOPATH/src/github.com/posener/complete'. this
|
|
|
|
// is the reason to use the PredictSet and not the PredictDirs in this case.
|
2019-11-13 22:51:44 -06:00
|
|
|
s := systemDirs(prefix)
|
|
|
|
sysDirs := predict.Set(s).Predict(prefix)
|
2017-07-28 07:01:07 -05:00
|
|
|
return append(localDirs, sysDirs...)
|
2017-05-19 03:59:51 -05:00
|
|
|
}
|
2017-05-12 16:17:48 -05:00
|
|
|
|
2017-05-19 03:59:51 -05:00
|
|
|
// listPackages looks in current pointed dir and in all it's direct sub-packages
|
|
|
|
// and return a list of paths to go packages.
|
|
|
|
func listPackages(dir string) (directories []string) {
|
|
|
|
// add subdirectories
|
2017-05-18 16:11:30 -05:00
|
|
|
files, err := ioutil.ReadDir(dir)
|
2017-05-12 16:17:48 -05:00
|
|
|
if err != nil {
|
2019-11-13 22:51:44 -06:00
|
|
|
log.Printf("failed reading directory %s: %s", dir, err)
|
2017-05-12 16:17:48 -05:00
|
|
|
return
|
|
|
|
}
|
2017-05-19 03:59:51 -05:00
|
|
|
|
|
|
|
// build paths array
|
|
|
|
paths := make([]string, 0, len(files)+1)
|
2017-05-18 16:11:30 -05:00
|
|
|
for _, f := range files {
|
2017-05-19 03:59:51 -05:00
|
|
|
if f.IsDir() {
|
|
|
|
paths = append(paths, filepath.Join(dir, f.Name()))
|
2017-05-18 16:11:30 -05:00
|
|
|
}
|
2017-05-19 03:59:51 -05:00
|
|
|
}
|
|
|
|
paths = append(paths, dir)
|
|
|
|
|
|
|
|
// import packages according to given paths
|
|
|
|
for _, p := range paths {
|
|
|
|
pkg, err := build.ImportDir(p, 0)
|
2017-05-15 14:44:19 -05:00
|
|
|
if err != nil {
|
2019-11-13 22:51:44 -06:00
|
|
|
log.Printf("failed importing directory %s: %s", p, err)
|
2017-05-15 14:44:19 -05:00
|
|
|
continue
|
2017-05-12 16:17:48 -05:00
|
|
|
}
|
2017-05-19 03:59:51 -05:00
|
|
|
directories = append(directories, pkg.Dir)
|
2017-05-12 16:17:48 -05:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2017-07-28 07:01:07 -05:00
|
|
|
|
|
|
|
func systemDirs(dir string) (directories []string) {
|
|
|
|
// get all paths from GOPATH environment variable and use their src directory
|
2017-07-30 12:41:18 -05:00
|
|
|
paths := findGopath()
|
2017-07-28 07:01:07 -05:00
|
|
|
for i := range paths {
|
|
|
|
paths[i] = filepath.Join(paths[i], "src")
|
|
|
|
}
|
|
|
|
|
|
|
|
// normalize the directory to be an actual directory since it could be with an additional
|
|
|
|
// characters after the last '/'.
|
|
|
|
if !strings.HasSuffix(dir, "/") {
|
|
|
|
dir = filepath.Dir(dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, basePath := range paths {
|
|
|
|
path := filepath.Join(basePath, dir)
|
|
|
|
files, err := ioutil.ReadDir(path)
|
|
|
|
if err != nil {
|
|
|
|
// path does not exists
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// add the base path as one of the completion options
|
|
|
|
switch dir {
|
|
|
|
case "", ".", "/", "./":
|
|
|
|
default:
|
|
|
|
directories = append(directories, dir)
|
|
|
|
}
|
|
|
|
// add all nested directories of the base path
|
|
|
|
// go supports only packages and not go files within the GOPATH
|
|
|
|
for _, f := range files {
|
|
|
|
if !f.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
directories = append(directories, filepath.Join(dir, f.Name())+"/")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2017-07-30 12:41:18 -05:00
|
|
|
|
|
|
|
func findGopath() []string {
|
|
|
|
gopath := os.Getenv("GOPATH")
|
|
|
|
if gopath == "" {
|
|
|
|
// By convention
|
|
|
|
// See rationale at https://github.com/golang/go/issues/17262
|
|
|
|
usr, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
usrgo := filepath.Join(usr.HomeDir, "go")
|
|
|
|
return []string{usrgo}
|
|
|
|
}
|
|
|
|
listsep := string([]byte{os.PathListSeparator})
|
|
|
|
entries := strings.Split(gopath, listsep)
|
|
|
|
return entries
|
|
|
|
}
|
2019-11-13 22:51:44 -06:00
|
|
|
|
|
|
|
func directory(prefix string) string {
|
|
|
|
if info, err := os.Stat(prefix); err == nil && info.IsDir() {
|
|
|
|
return fixPathForm(prefix, prefix)
|
|
|
|
}
|
|
|
|
dir := filepath.Dir(prefix)
|
|
|
|
if info, err := os.Stat(dir); err != nil || !info.IsDir() {
|
|
|
|
return "./"
|
|
|
|
}
|
|
|
|
return fixPathForm(prefix, dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// fixPathForm changes a file name to a relative name
|
|
|
|
func fixPathForm(last string, file string) string {
|
|
|
|
// get wording directory for relative name
|
|
|
|
workDir, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
|
|
|
|
abs, err := filepath.Abs(file)
|
|
|
|
if err != nil {
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
|
|
|
|
// if last is absolute, return path as absolute
|
|
|
|
if filepath.IsAbs(last) {
|
|
|
|
return fixDirPath(abs)
|
|
|
|
}
|
|
|
|
|
|
|
|
rel, err := filepath.Rel(workDir, abs)
|
|
|
|
if err != nil {
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
|
|
|
|
// fix ./ prefix of path
|
|
|
|
if rel != "." && strings.HasPrefix(last, ".") {
|
|
|
|
rel = "./" + rel
|
|
|
|
}
|
|
|
|
|
|
|
|
return fixDirPath(rel)
|
|
|
|
}
|
|
|
|
|
|
|
|
func fixDirPath(path string) string {
|
|
|
|
info, err := os.Stat(path)
|
|
|
|
if err == nil && info.IsDir() && !strings.HasSuffix(path, "/") {
|
|
|
|
path += "/"
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|