diff options
| author | Eyal Posener <[email protected]> | 2017-05-11 00:38:24 +0300 |
|---|---|---|
| committer | Eyal Posener <[email protected]> | 2017-05-11 02:16:39 +0300 |
| commit | 72dfe017e9209c1809cfcfcbd9039551fe4d2103 (patch) | |
| tree | 4881f90157f3605fbb488c32e0faae7dff1bc818 /match | |
| parent | 1c743d8c0b8235ea2dbf0856987f8bd5b77a0042 (diff) | |
Improve files and directories completion
Diffstat (limited to 'match')
| -rw-r--r-- | match/file.go | 18 | ||||
| -rw-r--r-- | match/match_test.go | 19 |
2 files changed, 24 insertions, 13 deletions
diff --git a/match/file.go b/match/file.go index c972ce0..0b554ce 100644 --- a/match/file.go +++ b/match/file.go @@ -1,7 +1,6 @@ package match import ( - "path/filepath" "strings" ) @@ -15,16 +14,13 @@ func (a File) String() string { // Match returns true if prefix's abs path prefixes a's abs path func (a File) Match(prefix string) bool { - full, err := filepath.Abs(string(a)) - if err != nil { - return false - } - prefixFull, err := filepath.Abs(prefix) - if err != nil { - return false + + // special case for current directory completion + if a == "./" && (prefix == "." || prefix == "") { + return true } - // if the file has the prefix as prefix, - // but we don't want to show too many files, so, if it is in a deeper directory - omit it. - return strings.HasPrefix(full, prefixFull) && (full == prefixFull || !strings.Contains(full[len(prefixFull)+1:], "/")) + cmp := strings.TrimPrefix(string(a), "./") + prefix = strings.TrimPrefix(prefix, "./") + return strings.HasPrefix(cmp, prefix) } diff --git a/match/match_test.go b/match/match_test.go index ae1ffea..d7a851a 100644 --- a/match/match_test.go +++ b/match/match_test.go @@ -45,6 +45,7 @@ func TestMatch(t *testing.T) { {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}, @@ -54,6 +55,7 @@ func TestMatch(t *testing.T) { {prefix: "/file.txt", want: false}, {prefix: "/fil", want: false}, {prefix: "/file.txt2", want: false}, + {prefix: "/.", want: false}, }, }, { @@ -62,6 +64,7 @@ func TestMatch(t *testing.T) { {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}, @@ -71,14 +74,16 @@ func TestMatch(t *testing.T) { {prefix: "/file.txt", want: false}, {prefix: "/fil", want: false}, {prefix: "/file.txt2", want: false}, + {prefix: "/.", want: false}, }, }, { m: File("/file.txt"), tests: []matcherTest{ - {prefix: "", want: false}, + {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}, @@ -88,13 +93,23 @@ func TestMatch(t *testing.T) { {prefix: "/file.txt", want: true}, {prefix: "/fil", want: true}, {prefix: "/file.txt2", want: false}, + {prefix: "/.", want: false}, + }, + }, + { + m: File("./"), + tests: []matcherTest{ + {prefix: "", want: true}, + {prefix: ".", want: true}, + {prefix: "./", want: true}, + {prefix: "./.", want: false}, }, }, } for _, tt := range tests { for _, ttt := range tt.tests { - name := "matcher:" + tt.m.String() + "/prefix:" + ttt.prefix + name := "matcher='" + tt.m.String() + "'&prefix='" + ttt.prefix + "'" t.Run(name, func(t *testing.T) { got := tt.m.Match(ttt.prefix) if got != ttt.want { |
