From 72c5c945f0d5861ba1d4e51a0a5ac36a4bef3868 Mon Sep 17 00:00:00 2001 From: Eyal Posener Date: Fri, 5 Jul 2019 15:10:58 +0300 Subject: [PATCH] Deprecate Args.Directory() --- args.go | 2 ++ gocomplete/complete.go | 2 +- predict_files.go | 54 +++++++++++++++++++++++++++++++++++++++++- utils.go | 46 ----------------------------------- 4 files changed, 56 insertions(+), 48 deletions(-) delete mode 100644 utils.go diff --git a/args.go b/args.go index 17ab2c6..16e5ab3 100644 --- a/args.go +++ b/args.go @@ -28,6 +28,8 @@ type Args struct { // Directory gives the directory of the current written // last argument if it represents a file name being written. // in case that it is not, we fall back to the current directory. +// +// Deprecated. func (a Args) Directory() string { if info, err := os.Stat(a.Last); err == nil && info.IsDir() { return fixPathForm(a.Last, a.Last) diff --git a/gocomplete/complete.go b/gocomplete/complete.go index 95f2002..2c31010 100644 --- a/gocomplete/complete.go +++ b/gocomplete/complete.go @@ -449,7 +449,7 @@ func main() { "-unusedfuncs": complete.PredictAnything, "-unusedresult": complete.PredictNothing, "-unusedstringmethods": complete.PredictAnything, - "-v": complete.PredictNothing, + "-v": complete.PredictNothing, }, Args: anyGo, }, diff --git a/predict_files.go b/predict_files.go index c8baec1..25ae2d5 100644 --- a/predict_files.go +++ b/predict_files.go @@ -51,7 +51,7 @@ func predictFiles(a Args, pattern string, allowFiles bool) []string { return nil } - dir := a.Directory() + dir := directory(a.Last) files := listFiles(dir, pattern, allowFiles) // add dir if match @@ -60,6 +60,19 @@ func predictFiles(a Args, pattern string, allowFiles bool) []string { return PredictFilesSet(files).Predict(a) } +// directory gives the directory of the given partial path +// in case that it is not, we fall back to the current directory. +func directory(path string) string { + if info, err := os.Stat(path); err == nil && info.IsDir() { + return fixPathForm(path, path) + } + dir := filepath.Dir(path) + if info, err := os.Stat(dir); err == nil && info.IsDir() { + return fixPathForm(path, dir) + } + return "./" +} + // PredictFilesSet predict according to file rules to a given set of file names func PredictFilesSet(files []string) PredictFunc { return func(a Args) (prediction []string) { @@ -120,3 +133,42 @@ func matchFile(file, prefix string) bool { return strings.HasPrefix(file, prefix) } + +// fixPathForm changes a file name to a relative name +func fixPathForm(last string, file string) string { + // get wording directory for relative name + workDir, err := os.Getwd() + if err != nil { + return file + } + + abs, err := filepath.Abs(file) + if err != nil { + return file + } + + // if last is absolute, return path as absolute + if filepath.IsAbs(last) { + return fixDirPath(abs) + } + + rel, err := filepath.Rel(workDir, abs) + if err != nil { + return file + } + + // fix ./ prefix of path + if rel != "." && strings.HasPrefix(last, ".") { + rel = "./" + rel + } + + return fixDirPath(rel) +} + +func fixDirPath(path string) string { + info, err := os.Stat(path) + if err == nil && info.IsDir() && !strings.HasSuffix(path, "/") { + path += "/" + } + return path +} diff --git a/utils.go b/utils.go deleted file mode 100644 index 58b8b79..0000000 --- a/utils.go +++ /dev/null @@ -1,46 +0,0 @@ -package complete - -import ( - "os" - "path/filepath" - "strings" -) - -// fixPathForm changes a file name to a relative name -func fixPathForm(last string, file string) string { - // get wording directory for relative name - workDir, err := os.Getwd() - if err != nil { - return file - } - - abs, err := filepath.Abs(file) - if err != nil { - return file - } - - // if last is absolute, return path as absolute - if filepath.IsAbs(last) { - return fixDirPath(abs) - } - - rel, err := filepath.Rel(workDir, abs) - if err != nil { - return file - } - - // fix ./ prefix of path - if rel != "." && strings.HasPrefix(last, ".") { - rel = "./" + rel - } - - return fixDirPath(rel) -} - -func fixDirPath(path string) string { - info, err := os.Stat(path) - if err == nil && info.IsDir() && !strings.HasSuffix(path, "/") { - path += "/" - } - return path -}