Recursive directory lookup
When only one directory matches the result, search recursively whithin this directory for files.
This commit is contained in:
parent
136e52e074
commit
6640208067
|
@ -25,10 +25,36 @@ func PredictFiles(pattern string) Predictor {
|
||||||
}
|
}
|
||||||
|
|
||||||
func files(pattern string, allowFiles bool) PredictFunc {
|
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) {
|
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
|
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) {
|
func predictFiles(last string, pattern string, allowFiles bool) (prediction []string) {
|
||||||
|
|
|
@ -13,7 +13,7 @@ func TestPredicate(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
p Predictor
|
p Predictor
|
||||||
arg string
|
argList []string
|
||||||
want []string
|
want []string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
|
@ -24,7 +24,7 @@ func TestPredicate(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "set with does",
|
name: "set with does",
|
||||||
p: PredictSet("./..", "./x"),
|
p: PredictSet("./..", "./x"),
|
||||||
arg: "./.",
|
argList: []string{"./.", "./.."},
|
||||||
want: []string{"./.."},
|
want: []string{"./.."},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -65,55 +65,58 @@ func TestPredicate(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "files/txt",
|
name: "files/txt",
|
||||||
p: PredictFiles("*.txt"),
|
p: PredictFiles("*.txt"),
|
||||||
arg: "./dir/",
|
argList: []string{"./dir/"},
|
||||||
want: []string{"./dir/"},
|
want: []string{"./dir/"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "files/x",
|
name: "complete files inside dir if it is the only match",
|
||||||
p: PredictFiles("x"),
|
p: PredictFiles("foo"),
|
||||||
arg: "./dir/",
|
argList: []string{"./dir/", "./d"},
|
||||||
want: []string{"./dir/", "./dir/x"},
|
want: []string{"./dir/", "./dir/foo"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "files/*",
|
name: "complete files inside dir when argList includes file name",
|
||||||
p: PredictFiles("x*"),
|
p: PredictFiles("*"),
|
||||||
arg: "./dir/",
|
argList: []string{"./dir/f", "./dir/foo"},
|
||||||
want: []string{"./dir/", "./dir/x"},
|
want: []string{"./dir/foo"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "files/md",
|
name: "files/md",
|
||||||
p: PredictFiles("*.md"),
|
p: PredictFiles("*.md"),
|
||||||
|
argList: []string{"", ".", "./"},
|
||||||
want: []string{"./", "./dir/", "./readme.md"},
|
want: []string{"./", "./dir/", "./readme.md"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "dirs",
|
name: "dirs",
|
||||||
p: PredictDirs("*"),
|
p: PredictDirs("*"),
|
||||||
arg: "./dir/",
|
argList: []string{"./dir/", "./di", "di", "dir", "dir/"},
|
||||||
want: []string{"./dir/"},
|
want: []string{"./dir/"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "dirs and files",
|
name: "predict anything in dir",
|
||||||
p: PredictFiles("*"),
|
p: PredictFiles("*"),
|
||||||
arg: "./dir",
|
argList: []string{"./dir", "dir", "./dir/", "./di"},
|
||||||
want: []string{"./dir/", "./dir/x"},
|
want: []string{"./dir/", "./dir/foo", "./dir/bar"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "dirs",
|
name: "root directories",
|
||||||
p: PredictDirs("*"),
|
p: PredictDirs("*"),
|
||||||
|
argList: []string{"", ".", "./"},
|
||||||
want: []string{"./", "./dir/"},
|
want: []string{"./", "./dir/"},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: "subdir",
|
|
||||||
p: PredictFiles("*"),
|
|
||||||
arg: "./dir/",
|
|
||||||
want: []string{"./dir/", "./dir/x"},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
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(matches)
|
||||||
sort.Strings(tt.want)
|
sort.Strings(tt.want)
|
||||||
|
@ -127,3 +130,4 @@ func TestPredicate(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue