summaryrefslogtreecommitdiff
path: root/predict_files.go
blob: fe70c9732bc9bd70da587301a1faab35c67c2dc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package complete

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"

	"github.com/posener/complete/match"
)

// PredictDirs will search for directories in the given started to be typed
// path, if no path was started to be typed, it will complete to directories
// in the current working directory.
func PredictDirs(pattern string) Predictor {
	return files(pattern, false)
}

// PredictFiles will search for files matching the given pattern in the started to
// be typed path, if no path was started to be typed, it will complete to files that
// match the pattern in the current working directory.
// To match any file, use "*" as pattern. To match go files use "*.go", and so on.
func PredictFiles(pattern string) Predictor {
	return files(pattern, true)
}

func files(pattern string, allowFiles bool) PredictFunc {

	// search for files according to arguments,
	// if only one directory has matched the result, search recursively into
	// this directory to give more results.
	return func(a Args) (prediction []string) {
		last := a.Last
		for {

			prediction = predictFiles(last, pattern, allowFiles)

			// if the number of prediction is not 1, we either have many results or
			// have no results, so we return it.
			if len(prediction) != 1 {
				return
			}

			// if the result is only one item, we might want to recursively check
			// for more accurate results.
			if prediction[0] == last { // avoid loop forever
				return
			}

			// only try deeper, if the one item is a directory
			if stat, err := os.Stat(prediction[0]); err != nil || !stat.IsDir() {
				return
			}

			last = prediction[0]
		}
	}
}

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 {
		for _, f := range files {
			m[f] = true
		}
	}
	if dirs, err := ioutil.ReadDir(dir); err == nil {
		for _, d := range dirs {
			if d.IsDir() {
				m[d.Name()] = true
			}
		}
	}
	list := make([]string, 0, len(m))
	for k := range m {
		list = append(list, k)
	}
	return list
}

// toRel changes a file name to a relative name
func toRel(wd, file string) string {
	abs, err := filepath.Abs(file)
	if err != nil {
		return file
	}
	rel, err := filepath.Rel(wd, abs)
	if err != nil {
		return file
	}
	if rel != "." {
		rel = "./" + rel
	}
	if info, err := os.Stat(rel); err == nil && info.IsDir() {
		rel += "/"
	}
	return rel
}

// dirFromLast gives the directory of the current written
// last argument if it represents a file name being written.
// in case that it is not, we fall back to the current directory.
func dirFromLast(last string) string {
	if info, err := os.Stat(last); err == nil && info.IsDir() {
		return last
	}
	dir := filepath.Dir(last)
	_, err := os.Stat(dir)
	if err != nil {
		return "./"
	}
	return dir
}