Skip to content

Commit

Permalink
deprecate match package
Browse files Browse the repository at this point in the history
  • Loading branch information
posener committed Jul 5, 2019
1 parent f71e6ba commit 29f43e2
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 33 deletions.
4 changes: 2 additions & 2 deletions complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"io"
"os"
"strconv"
"strings"

"github.com/posener/complete/cmd"
"github.com/posener/complete/match"
)

const (
Expand Down Expand Up @@ -72,7 +72,7 @@ func (c *Complete) Complete() bool {
// filter only options that match the last argument
matches := []string{}
for _, option := range options {
if match.Prefix(option, a.Last) {
if strings.HasPrefix(option, a.Last) {
matches = append(matches, option)
}
}
Expand Down
19 changes: 0 additions & 19 deletions match/file.go

This file was deleted.

32 changes: 32 additions & 0 deletions match/match.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,39 @@
// Package match contains matchers that decide if to apply completion.
//
// This package is deprecated.
package match

import "strings"

// Match matches two strings
// it is used for comparing a term to the last typed
// word, the prefix, and see if it is a possible auto complete option.
//
// Deprecated.
type Match func(term, prefix string) bool

// Prefix is a simple Matcher, if the word is it's prefix, there is a match
// Match returns true if a has the prefix as prefix
//
// Deprecated.
func Prefix(long, prefix string) bool {
return strings.HasPrefix(long, prefix)
}

// File returns true if prefix can match the file
//
// Deprecated.
func File(file, prefix string) bool {
// special case for current directory completion
if file == "./" && (prefix == "." || prefix == "") {
return true
}
if prefix == "." && strings.HasPrefix(file, ".") {
return true
}

file = strings.TrimPrefix(file, "./")
prefix = strings.TrimPrefix(prefix, "./")

return strings.HasPrefix(file, prefix)
}
9 changes: 0 additions & 9 deletions match/prefix.go

This file was deleted.

20 changes: 17 additions & 3 deletions predict_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"os"
"path/filepath"
"strings"

"github.com/posener/complete/match"
)

// PredictDirs will search for directories in the given started to be typed
Expand Down Expand Up @@ -70,7 +68,7 @@ func PredictFilesSet(files []string) PredictFunc {
f = fixPathForm(a.Last, f)

// test matching of file to the argument
if match.File(f, a.Last) {
if matchFile(f, a.Last) {
prediction = append(prediction, f)
}
}
Expand Down Expand Up @@ -106,3 +104,19 @@ func listFiles(dir, pattern string, allowFiles bool) []string {
}
return list
}

// MatchFile returns true if prefix can match the file
func matchFile(file, prefix string) bool {
// special case for current directory completion
if file == "./" && (prefix == "." || prefix == "") {
return true
}
if prefix == "." && strings.HasPrefix(file, ".") {
return true
}

file = strings.TrimPrefix(file, "./")
prefix = strings.TrimPrefix(prefix, "./")

return strings.HasPrefix(file, prefix)
}
102 changes: 102 additions & 0 deletions predict_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package complete

import (
"fmt"
"os"
"sort"
"strings"
"testing"
Expand Down Expand Up @@ -167,3 +169,103 @@ func TestPredicate(t *testing.T) {
}
}
}

func TestMatchFile(t *testing.T) {
t.Parallel()

// Change to tests directory for testing completion of
// files and directories
err := os.Chdir("../tests")
if err != nil {
panic(err)
}

type matcherTest struct {
prefix string
want bool
}

tests := []struct {
long string
tests []matcherTest
}{
{
long: "file.txt",
tests: []matcherTest{
{prefix: "", want: true},
{prefix: "f", want: true},
{prefix: "./f", want: true},
{prefix: "./.", want: false},
{prefix: "file.", want: true},
{prefix: "./file.", want: true},
{prefix: "file.txt", want: true},
{prefix: "./file.txt", want: true},
{prefix: "other.txt", want: false},
{prefix: "/other.txt", want: false},
{prefix: "/file.txt", want: false},
{prefix: "/fil", want: false},
{prefix: "/file.txt2", want: false},
{prefix: "/.", want: false},
},
},
{
long: "./file.txt",
tests: []matcherTest{
{prefix: "", want: true},
{prefix: "f", want: true},
{prefix: "./f", want: true},
{prefix: "./.", want: false},
{prefix: "file.", want: true},
{prefix: "./file.", want: true},
{prefix: "file.txt", want: true},
{prefix: "./file.txt", want: true},
{prefix: "other.txt", want: false},
{prefix: "/other.txt", want: false},
{prefix: "/file.txt", want: false},
{prefix: "/fil", want: false},
{prefix: "/file.txt2", want: false},
{prefix: "/.", want: false},
},
},
{
long: "/file.txt",
tests: []matcherTest{
{prefix: "", want: true},
{prefix: "f", want: false},
{prefix: "./f", want: false},
{prefix: "./.", want: false},
{prefix: "file.", want: false},
{prefix: "./file.", want: false},
{prefix: "file.txt", want: false},
{prefix: "./file.txt", want: false},
{prefix: "other.txt", want: false},
{prefix: "/other.txt", want: false},
{prefix: "/file.txt", want: true},
{prefix: "/fil", want: true},
{prefix: "/file.txt2", want: false},
{prefix: "/.", want: false},
},
},
{
long: "./",
tests: []matcherTest{
{prefix: "", want: true},
{prefix: ".", want: true},
{prefix: "./", want: true},
{prefix: "./.", want: false},
},
},
}

for _, tt := range tests {
for _, ttt := range tt.tests {
name := fmt.Sprintf("long=%q&prefix=%q", tt.long, ttt.prefix)
t.Run(name, func(t *testing.T) {
got := matchFile(tt.long, ttt.prefix)
if got != ttt.want {
t.Errorf("Failed %s: got = %t, want: %t", name, got, ttt.want)
}
})
}
}
}

0 comments on commit 29f43e2

Please sign in to comment.