Deprecate Args.Directory()
This commit is contained in:
parent
29f43e246e
commit
72c5c945f0
2
args.go
2
args.go
|
@ -28,6 +28,8 @@ type Args struct {
|
||||||
// Directory gives the directory of the current written
|
// Directory gives the directory of the current written
|
||||||
// last argument if it represents a file name being written.
|
// last argument if it represents a file name being written.
|
||||||
// in case that it is not, we fall back to the current directory.
|
// in case that it is not, we fall back to the current directory.
|
||||||
|
//
|
||||||
|
// Deprecated.
|
||||||
func (a Args) Directory() string {
|
func (a Args) Directory() string {
|
||||||
if info, err := os.Stat(a.Last); err == nil && info.IsDir() {
|
if info, err := os.Stat(a.Last); err == nil && info.IsDir() {
|
||||||
return fixPathForm(a.Last, a.Last)
|
return fixPathForm(a.Last, a.Last)
|
||||||
|
|
|
@ -449,7 +449,7 @@ func main() {
|
||||||
"-unusedfuncs": complete.PredictAnything,
|
"-unusedfuncs": complete.PredictAnything,
|
||||||
"-unusedresult": complete.PredictNothing,
|
"-unusedresult": complete.PredictNothing,
|
||||||
"-unusedstringmethods": complete.PredictAnything,
|
"-unusedstringmethods": complete.PredictAnything,
|
||||||
"-v": complete.PredictNothing,
|
"-v": complete.PredictNothing,
|
||||||
},
|
},
|
||||||
Args: anyGo,
|
Args: anyGo,
|
||||||
},
|
},
|
||||||
|
|
|
@ -51,7 +51,7 @@ func predictFiles(a Args, pattern string, allowFiles bool) []string {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
dir := a.Directory()
|
dir := directory(a.Last)
|
||||||
files := listFiles(dir, pattern, allowFiles)
|
files := listFiles(dir, pattern, allowFiles)
|
||||||
|
|
||||||
// add dir if match
|
// add dir if match
|
||||||
|
@ -60,6 +60,19 @@ func predictFiles(a Args, pattern string, allowFiles bool) []string {
|
||||||
return PredictFilesSet(files).Predict(a)
|
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
|
// PredictFilesSet predict according to file rules to a given set of file names
|
||||||
func PredictFilesSet(files []string) PredictFunc {
|
func PredictFilesSet(files []string) PredictFunc {
|
||||||
return func(a Args) (prediction []string) {
|
return func(a Args) (prediction []string) {
|
||||||
|
@ -120,3 +133,42 @@ func matchFile(file, prefix string) bool {
|
||||||
|
|
||||||
return strings.HasPrefix(file, prefix)
|
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
|
||||||
|
}
|
||||||
|
|
46
utils.go
46
utils.go
|
@ -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
|
|
||||||
}
|
|
Loading…
Reference in New Issue