complete/utils.go

47 lines
813 B
Go
Raw Normal View History

2017-05-13 14:44:36 -05:00
package complete
import (
"os"
"path/filepath"
2017-05-15 15:52:04 -05:00
"strings"
2017-05-13 14:44:36 -05:00
)
2017-05-15 15:52:04 -05:00
// fixPathForm changes a file name to a relative name
func fixPathForm(last string, file string) string {
2017-05-13 14:44:36 -05:00
// 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
}
2017-05-15 15:52:04 -05:00
// if last is absolute, return path as absolute
if filepath.IsAbs(last) {
return fixDirPath(abs)
}
2017-05-13 14:44:36 -05:00
rel, err := filepath.Rel(workDir, abs)
if err != nil {
return file
}
2017-05-15 15:52:04 -05:00
// fix ./ prefix of path
if rel != "." && strings.HasPrefix(last, ".") {
2017-05-13 14:44:36 -05:00
rel = "./" + rel
}
2017-05-15 15:52:04 -05:00
return fixDirPath(rel)
}
func fixDirPath(path string) string {
info, err := os.Stat(path)
if err == nil && info.IsDir() && !strings.HasSuffix(path, "/") {
path += "/"
2017-05-13 14:44:36 -05:00
}
2017-05-15 15:52:04 -05:00
return path
2017-05-13 14:44:36 -05:00
}