From 66402080675c250edabe50839555942d1b1aa189 Mon Sep 17 00:00:00 2001 From: Eyal Posener Date: Fri, 12 May 2017 22:43:33 +0300 Subject: Recursive directory lookup When only one directory matches the result, search recursively whithin this directory for files. --- predict_files.go | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'predict_files.go') diff --git a/predict_files.go b/predict_files.go index 8ad5368..fe70c97 100644 --- a/predict_files.go +++ b/predict_files.go @@ -25,9 +25,35 @@ 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) - return + 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] + } } } -- cgit v1.2.3