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
6
args.go
6
args.go
|
@ -3,15 +3,15 @@ package complete
|
||||||
// Args describes command line arguments
|
// Args describes command line arguments
|
||||||
type Args struct {
|
type Args struct {
|
||||||
// All lists of all arguments in command line (not including the command itself)
|
// All lists of all arguments in command line (not including the command itself)
|
||||||
All []string
|
All []string
|
||||||
// Completed lists of all completed arguments in command line,
|
// Completed lists of all completed arguments in command line,
|
||||||
// If the last one is still being typed - no space after it,
|
// If the last one is still being typed - no space after it,
|
||||||
// it won't appear in this list of arguments.
|
// it won't appear in this list of arguments.
|
||||||
Completed []string
|
Completed []string
|
||||||
// Last argument in command line, the one being typed, if the last
|
// Last argument in command line, the one being typed, if the last
|
||||||
// character in the command line is a space, this argument will be empty,
|
// character in the command line is a space, this argument will be empty,
|
||||||
// otherwise this would be the last word.
|
// otherwise this would be the last word.
|
||||||
Last string
|
Last string
|
||||||
// LastCompleted is the last argument that was fully typed.
|
// LastCompleted is the last argument that was fully typed.
|
||||||
// If the last character in the command line is space, this would be the
|
// If the last character in the command line is space, this would be the
|
||||||
// last word, otherwise, it would be the word before that.
|
// last word, otherwise, it would be the word before that.
|
||||||
|
|
|
@ -25,9 +25,35 @@ 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
|
||||||
return
|
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]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
104
predict_test.go
104
predict_test.go
|
@ -11,10 +11,10 @@ func TestPredicate(t *testing.T) {
|
||||||
initTests()
|
initTests()
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
p Predictor
|
p Predictor
|
||||||
arg string
|
argList []string
|
||||||
want []string
|
want []string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "set",
|
name: "set",
|
||||||
|
@ -22,10 +22,10 @@ func TestPredicate(t *testing.T) {
|
||||||
want: []string{"a", "b", "c"},
|
want: []string{"a", "b", "c"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "set with does",
|
name: "set with does",
|
||||||
p: PredictSet("./..", "./x"),
|
p: PredictSet("./..", "./x"),
|
||||||
arg: "./.",
|
argList: []string{"./.", "./.."},
|
||||||
want: []string{"./.."},
|
want: []string{"./.."},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "set/empty",
|
name: "set/empty",
|
||||||
|
@ -63,67 +63,71 @@ func TestPredicate(t *testing.T) {
|
||||||
want: []string{"./", "./dir/", "./a.txt", "./b.txt", "./c.txt", "./.dot.txt"},
|
want: []string{"./", "./dir/", "./a.txt", "./b.txt", "./c.txt", "./.dot.txt"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
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"),
|
||||||
want: []string{"./", "./dir/", "./readme.md"},
|
argList: []string{"", ".", "./"},
|
||||||
|
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("*"),
|
||||||
want: []string{"./", "./dir/"},
|
argList: []string{"", ".", "./"},
|
||||||
},
|
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, "")
|
||||||
|
}
|
||||||
|
|
||||||
sort.Strings(matches)
|
for _, arg := range tt.argList {
|
||||||
sort.Strings(tt.want)
|
t.Run(tt.name+"?arg='"+arg+"'", func(t *testing.T) {
|
||||||
|
|
||||||
got := strings.Join(matches, ",")
|
matches := tt.p.Predict(newArgs(strings.Split(arg, " ")))
|
||||||
want := strings.Join(tt.want, ",")
|
|
||||||
|
|
||||||
if got != want {
|
sort.Strings(matches)
|
||||||
t.Errorf("failed %s\ngot = %s\nwant: %s", t.Name(), got, want)
|
sort.Strings(tt.want)
|
||||||
}
|
|
||||||
})
|
got := strings.Join(matches, ",")
|
||||||
|
want := strings.Join(tt.want, ",")
|
||||||
|
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("failed %s\ngot = %s\nwant: %s", t.Name(), got, want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue