Skip to content

Commit

Permalink
Deprecate Args.Directory()
Browse files Browse the repository at this point in the history
  • Loading branch information
posener committed Jul 5, 2019
1 parent 29f43e2 commit 72c5c94
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 48 deletions.
2 changes: 2 additions & 0 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion gocomplete/complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ func main() {
"-unusedfuncs": complete.PredictAnything,
"-unusedresult": complete.PredictNothing,
"-unusedstringmethods": complete.PredictAnything,
"-v": complete.PredictNothing,
"-v": complete.PredictNothing,
},
Args: anyGo,
},
Expand Down
54 changes: 53 additions & 1 deletion predict_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
46 changes: 0 additions & 46 deletions utils.go

This file was deleted.

0 comments on commit 72c5c94

Please sign in to comment.