From 61d9904ba1f47bf5bbd3497ac0c9f5787adb8633 Mon Sep 17 00:00:00 2001 From: Eyal Posener Date: Mon, 15 May 2017 23:52:04 +0300 Subject: Fix './' prefix for file completion --- utils.go | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'utils.go') diff --git a/utils.go b/utils.go index a59a0d4..58b8b79 100644 --- a/utils.go +++ b/utils.go @@ -3,10 +3,11 @@ package complete import ( "os" "path/filepath" + "strings" ) -// relativePath changes a file name to a relative name -func relativePath(file string) string { +// 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 { @@ -17,15 +18,29 @@ func relativePath(file string) string { 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 } - if rel != "." { + + // fix ./ prefix of path + if rel != "." && strings.HasPrefix(last, ".") { rel = "./" + rel } - if info, err := os.Stat(rel); err == nil && info.IsDir() { - rel += "/" + + return fixDirPath(rel) +} + +func fixDirPath(path string) string { + info, err := os.Stat(path) + if err == nil && info.IsDir() && !strings.HasSuffix(path, "/") { + path += "/" } - return rel + return path } -- cgit v1.2.3