Skip to content

Commit

Permalink
Merge pull request shyiko#3 from posener/predicate-as-function-type
Browse files Browse the repository at this point in the history
Predicate as function type
  • Loading branch information
posener authored May 6, 2017
2 parents 07b98cb + 9963a85 commit 2b6aed2
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 112 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ before_install:
- go get -u -t ./...

script:
- ./go.test.sh
- go test -v -race -coverprofile=coverage.txt -covermode=atomic

after_success:
- bash <(curl -s https://codecov.io/bash)
Expand Down
12 changes: 6 additions & 6 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package complete

type Commands map[string]Command

type Flags map[string]*Predicate
type Flags map[string]Predicate

type Command struct {
Name string
Sub Commands
Flags Flags
Args *Predicate
Args Predicate
}

// options returns all available complete options for the given command
Expand All @@ -17,11 +17,11 @@ func (c *Command) options(args []string) (options []Option, only bool) {

// remove the first argument, which is the command name
args = args[1:]

last := last(args)
// if prev has something that needs to follow it,
// it is the most relevant completion
if predicate, ok := c.Flags[last(args)]; ok && predicate != nil {
return predicate.predict(), true
if predicate, ok := c.Flags[last]; ok && predicate != nil {
return predicate.predict(last), true
}

sub, options, only := c.searchSub(args)
Expand All @@ -41,7 +41,7 @@ func (c *Command) options(args []string) (options []Option, only bool) {
}

// add additional expected argument of the command
options = append(options, c.Args.predict()...)
options = append(options, c.Args.predict(last)...)

return
}
Expand Down
12 changes: 0 additions & 12 deletions go.test.sh

This file was deleted.

32 changes: 16 additions & 16 deletions gocomplete/complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ var (
predictEllipsis = complete.PredictSet("./...")

goFilesOrPackages = complete.PredictFiles("**.go").
Or(complete.PredictDirs("./")).
Or(predictEllipsis)
Or(complete.PredictDirs).
Or(predictEllipsis)
)

func main() {
Expand All @@ -34,7 +34,7 @@ func main() {
"-installsuffix": complete.PredictAnything,
"-ldflags": complete.PredictAnything,
"-linkshared": complete.PredictNothing,
"-pkgdir": complete.PredictDirs("./"),
"-pkgdir": complete.PredictDirs,
"-tags": complete.PredictAnything,
"-toolexec": complete.PredictAnything,
},
Expand All @@ -45,7 +45,7 @@ func main() {
Flags: complete.Flags{
"-exec": complete.PredictAnything,
},
Args: complete.PredictFiles("**.go"),
Args: complete.PredictFiles("*.go"),
}

test := complete.Command{
Expand All @@ -59,23 +59,23 @@ func main() {
"-count": complete.PredictAnything,
"-cover": complete.PredictNothing,
"-covermode": complete.PredictSet("set", "count", "atomic"),
"-coverpkg": complete.PredictDirs("./"),
"-coverpkg": complete.PredictDirs,
"-cpu": complete.PredictAnything,
"-run": predictTest("test"),
"-short": complete.PredictNothing,
"-timeout": complete.PredictAnything,

"-benchmem": complete.PredictNothing,
"-blockprofile": complete.PredictFiles("**.out"),
"-blockprofile": complete.PredictFiles("*.out"),
"-blockprofilerate": complete.PredictAnything,
"-coverprofile": complete.PredictFiles("**.out"),
"-cpuprofile": complete.PredictFiles("**.out"),
"-memprofile": complete.PredictFiles("**.out"),
"-coverprofile": complete.PredictFiles("*.out"),
"-cpuprofile": complete.PredictFiles("*.out"),
"-memprofile": complete.PredictFiles("*.out"),
"-memprofilerate": complete.PredictAnything,
"-mutexprofile": complete.PredictFiles("**.out"),
"-mutexprofile": complete.PredictFiles("*.out"),
"-mutexprofilefraction": complete.PredictAnything,
"-outputdir": complete.PredictDirs("./"),
"-trace": complete.PredictFiles("**.out"),
"-outputdir": complete.PredictDirs,
"-trace": complete.PredictFiles("*.out"),
},
Args: goFilesOrPackages,
}
Expand Down Expand Up @@ -115,7 +115,7 @@ func main() {
"-n": complete.PredictNothing,
"-x": complete.PredictNothing,
},
Args: complete.PredictDirs("./"),
Args: complete.PredictDirs,
}

list := complete.Command{
Expand All @@ -124,7 +124,7 @@ func main() {
"-f": complete.PredictAnything,
"-json": complete.PredictNothing,
},
Args: complete.PredictDirs("./"),
Args: complete.PredictDirs,
}

tool := complete.Command{
Expand All @@ -141,7 +141,7 @@ func main() {
"-n": complete.PredictNothing,
"-x": complete.PredictNothing,
},
Args: complete.PredictDirs("./"),
Args: complete.PredictDirs,
}

env := complete.Command{
Expand All @@ -152,7 +152,7 @@ func main() {
version := complete.Command{}

fix := complete.Command{
Args: complete.PredictDirs("./"),
Args: complete.PredictDirs,
}

// commands that also accepts the build flags
Expand Down
18 changes: 8 additions & 10 deletions gocomplete/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ import (
"github.com/posener/complete"
)

func predictTest(testType string) *complete.Predicate {
return &complete.Predicate{
Predictor: func() []complete.Option {
tests := testNames(testType)
options := make([]complete.Option, len(tests))
for i := range tests {
options[i] = complete.Arg(tests[i])
}
return options
},
func predictTest(testType string) complete.Predicate {
return func(last string) []complete.Option {
tests := testNames(testType)
options := make([]complete.Option, len(tests))
for i := range tests {
options[i] = complete.Arg(tests[i])
}
return options
}
}

Expand Down
4 changes: 2 additions & 2 deletions install/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"io/ioutil"
)

type home struct{}
Expand Down Expand Up @@ -150,4 +150,4 @@ func copyFile(src string, dst string) error {
defer out.Close()
_, err = io.Copy(out, in)
return err
}
}
3 changes: 1 addition & 2 deletions install/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@ func (root) Uninstall(cmd string, bin string) error {
}

func getBashCompletionDLink(cmd string) string {
return "/etc/bash_completion.d/"+cmd
return "/etc/bash_completion.d/" + cmd
}

93 changes: 49 additions & 44 deletions predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,74 +6,63 @@ import (
)

// Predicate determines what terms can follow a command or a flag
type Predicate struct {
// Predictor is function that returns list of arguments that can
// come after the flag/command
Predictor func() []Option
}
type Predicate func(last string) []Option

// Or unions two predicate struct, so that the result predicate
// returns the union of their predication
func (p *Predicate) Or(other *Predicate) *Predicate {
func (p Predicate) Or(other Predicate) Predicate {
if p == nil || other == nil {
return nil
}
return &Predicate{
Predictor: func() []Option { return append(p.predict(), other.predict()...) },
}
return func(last string) []Option { return append(p.predict(last), other.predict(last)...) }
}

func (p *Predicate) predict() []Option {
if p == nil || p.Predictor == nil {
func (p Predicate) predict(last string) []Option {
if p == nil {
return nil
}
return p.Predictor()
return p(last)
}

var (
PredictNothing *Predicate = nil
PredictAnything = &Predicate{}
PredictNothing Predicate = nil
)

func PredictSet(options ...string) *Predicate {
return &Predicate{
Predictor: func() []Option {
ret := make([]Option, len(options))
for i := range options {
ret[i] = Arg(options[i])
}
return ret
},
}
}
func PredictAnything(last string) []Option { return nil }

func PredictFiles(pattern string) *Predicate {
return &Predicate{Predictor: glob(pattern)}
func PredictSet(options ...string) Predicate {
return func(last string) []Option {
ret := make([]Option, len(options))
for i := range options {
ret[i] = Arg(options[i])
}
return ret
}
}

func PredictDirs(path string) *Predicate {
return &Predicate{Predictor: dirs(path)}
func PredictDirs(last string) (options []Option) {
dir := dirFromLast(last)
return dirsAt(dir)
}

func dirs(path string) func() []Option {
return func() (options []Option) {
dirs := []string{}
filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
dirs = append(dirs, path)
}
return nil
})
if !filepath.IsAbs(path) {
filesToRel(dirs)
func dirsAt(path string) []Option {
dirs := []string{}
filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
dirs = append(dirs, path)
}
return filesToOptions(dirs)
return nil
})
if !filepath.IsAbs(path) {
filesToRel(dirs)
}
return filesToOptions(dirs)
}

func glob(pattern string) func() []Option {
return func() []Option {
files, err := filepath.Glob(pattern)
func PredictFiles(pattern string) Predicate {
return func(last string) []Option {
dir := dirFromLast(last)
files, err := filepath.Glob(filepath.Join(dir, pattern))
if err != nil {
Log("failed glob operation with pattern '%s': %s", pattern, err)
}
Expand All @@ -83,6 +72,7 @@ func glob(pattern string) func() []Option {
return filesToOptions(files)
}
}

func filesToRel(files []string) {
wd, err := os.Getwd()
if err != nil {
Expand All @@ -97,6 +87,9 @@ func filesToRel(files []string) {
if err != nil {
continue
}
if rel == "." {
rel = ""
}
files[i] = "./" + rel
}
return
Expand All @@ -109,3 +102,15 @@ func filesToOptions(files []string) []Option {
}
return options
}

// dirFromLast 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.
func dirFromLast(last string) string {
dir := filepath.Dir(last)
_, err := os.Stat(dir)
if err != nil {
return "./"
}
return dir
}
Loading

0 comments on commit 2b6aed2

Please sign in to comment.