<commit_before>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 {
	return func(a Args) (prediction []string) {
		if strings.HasSuffix(a.Last, "/..") {
			return
		}
		dir := dirFromLast(a.Last)
		rel := !filepath.IsAbs(pattern)
		Log("looking for files in %s (last=%s)", dir, a.Last)
		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, a.Last) {
				prediction = append(prediction, f)
			}
		}
		return
	}
}

func listFiles(dir, pattern string) []string {<commit_msg>predict files: extract predictFiles function<commit_after>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 {
	return func(a Args) (prediction []string) {
		prediction = predictFiles(a.Last, pattern, allowFiles)
		return
	}
}

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 {
{'fraction_non_alphanumeric': 0.11404494382022472, 'fraction_numerical': 0.0, 'mean_word_length': 3.4736180904522613, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '867702', 'n_tokens_mistral': 1194, 'n_tokens_neox': 1099, 'n_words': 424}