Recursive directory lookup

When only one directory matches the result, search recursively
whithin this directory for files.
This commit is contained in:
Eyal Posener 2017-05-12 22:43:33 +03:00
parent 136e52e074
commit 6640208067
5 changed files with 85 additions and 55 deletions

View File

@ -25,10 +25,36 @@ func PredictFiles(pattern string) Predictor {
}
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) {
prediction = predictFiles(a.Last, pattern, allowFiles)
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) {

View File

@ -13,7 +13,7 @@ func TestPredicate(t *testing.T) {
tests := []struct {
name string
p Predictor
arg string
argList []string
want []string
}{
{
@ -24,7 +24,7 @@ func TestPredicate(t *testing.T) {
{
name: "set with does",
p: PredictSet("./..", "./x"),
arg: "./.",
argList: []string{"./.", "./.."},
want: []string{"./.."},
},
{
@ -65,55 +65,58 @@ func TestPredicate(t *testing.T) {
{
name: "files/txt",
p: PredictFiles("*.txt"),
arg: "./dir/",
argList: []string{"./dir/"},
want: []string{"./dir/"},
},
{
name: "files/x",
p: PredictFiles("x"),
arg: "./dir/",
want: []string{"./dir/", "./dir/x"},
name: "complete files inside dir if it is the only match",
p: PredictFiles("foo"),
argList: []string{"./dir/", "./d"},
want: []string{"./dir/", "./dir/foo"},
},
{
name: "files/*",
p: PredictFiles("x*"),
arg: "./dir/",
want: []string{"./dir/", "./dir/x"},
name: "complete files inside dir when argList includes file name",
p: PredictFiles("*"),
argList: []string{"./dir/f", "./dir/foo"},
want: []string{"./dir/foo"},
},
{
name: "files/md",
p: PredictFiles("*.md"),
argList: []string{"", ".", "./"},
want: []string{"./", "./dir/", "./readme.md"},
},
{
name: "dirs",
p: PredictDirs("*"),
arg: "./dir/",
argList: []string{"./dir/", "./di", "di", "dir", "dir/"},
want: []string{"./dir/"},
},
{
name: "dirs and files",
name: "predict anything in dir",
p: PredictFiles("*"),
arg: "./dir",
want: []string{"./dir/", "./dir/x"},
argList: []string{"./dir", "dir", "./dir/", "./di"},
want: []string{"./dir/", "./dir/foo", "./dir/bar"},
},
{
name: "dirs",
name: "root directories",
p: PredictDirs("*"),
argList: []string{"", ".", "./"},
want: []string{"./", "./dir/"},
},
{
name: "subdir",
p: PredictFiles("*"),
arg: "./dir/",
want: []string{"./dir/", "./dir/x"},
},
}
for _, tt := range tests {
t.Run(tt.name+"?arg='"+tt.arg+"'", func(t *testing.T) {
matches := tt.p.Predict(newArgs(strings.Split(tt.arg, " ")))
// no args in argList, means an empty argument
if len(tt.argList) == 0 {
tt.argList = append(tt.argList, "")
}
for _, arg := range tt.argList {
t.Run(tt.name+"?arg='"+arg+"'", func(t *testing.T) {
matches := tt.p.Predict(newArgs(strings.Split(arg, " ")))
sort.Strings(matches)
sort.Strings(tt.want)
@ -126,4 +129,5 @@ func TestPredicate(t *testing.T) {
}
})
}
}
}

0
tests/dir/foo Normal file
View File