predict files: extract predictFiles function

This commit is contained in:
Eyal Posener 2017-05-11 22:19:23 +03:00
parent 200b1b5b69
commit 026906bf10
1 changed files with 39 additions and 33 deletions

View File

@ -26,43 +26,49 @@ func PredictFiles(pattern string) Predictor {
func files(pattern string, allowFiles bool) PredictFunc {
return func(a Args) (prediction []string) {
if strings.HasSuffix(a.Last, "/..") {
return
}
dir := dirFromLast(a.Last)
rel := !filepath.IsAbs(pattern)
Log("looking for files in %s (last=%s)", dir, a.Last)
files := listFiles(dir, pattern)
// get wording directory for relative name
workDir, err := os.Getwd()
if err != nil {
workDir = ""
}
// add dir if match
files = append(files, dir)
// add all matching files to prediction
for _, f := range files {
if stat, err := os.Stat(f); err != nil || (!stat.IsDir() && !allowFiles) {
continue
}
// change file name to relative if necessary
if rel && workDir != "" {
f = toRel(workDir, f)
}
// test matching of file to the argument
if match.File(f, a.Last) {
prediction = append(prediction, f)
}
}
prediction = predictFiles(a.Last, pattern, allowFiles)
return
}
}
func predictFiles(last string, pattern string, allowFiles bool) (prediction []string) {
if strings.HasSuffix(last, "/..") {
return
}
dir := dirFromLast(last)
rel := !filepath.IsAbs(pattern)
files := listFiles(dir, pattern)
// get wording directory for relative name
workDir, err := os.Getwd()
if err != nil {
workDir = ""
}
// add dir if match
files = append(files, dir)
// add all matching files to prediction
for _, f := range files {
if stat, err := os.Stat(f); err != nil || (!stat.IsDir() && !allowFiles) {
continue
}
// change file name to relative if necessary
if rel && workDir != "" {
f = toRel(workDir, f)
}
// test matching of file to the argument
if match.File(f, last) {
prediction = append(prediction, f)
}
}
return
}
func listFiles(dir, pattern string) []string {
m := map[string]bool{}
if files, err := filepath.Glob(filepath.Join(dir, pattern)); err == nil {