Skip to content

Commit

Permalink
Append trailing slash in directory autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
wedaly committed Dec 28, 2023
1 parent 62637c7 commit 06f072b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
4 changes: 3 additions & 1 deletion file/autocomplete.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package file

import (
"fmt"
"os"
"path/filepath"
"strings"
Expand All @@ -23,7 +24,8 @@ func AutocompleteDirectory(path string) ([]string, error) {
if e.IsDir() {
name := e.Name()
if strings.HasPrefix(name, subdirPrefix) && len(subdirPrefix) < len(name) {
suffixes = append(suffixes, name[len(subdirPrefix):])
s := fmt.Sprintf("%s%c", name[len(subdirPrefix):], filepath.Separator)
suffixes = append(suffixes, s)
}
}
}
Expand Down
25 changes: 20 additions & 5 deletions file/autocomplete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ import (
"github.com/stretchr/testify/require"
)

func pathWithTrailingSlash(p string) string {
if len(p) > 0 && p[len(p)-1] == filepath.Separator {
return p
}
return fmt.Sprintf("%s%c", p, filepath.Separator)
}

func pathsWithTrailingSlashes(paths []string) []string {
result := make([]string, 0, len(paths))
for _, p := range paths {
result = append(result, pathWithTrailingSlash(p))
}
return result
}

func TestAutocompleteDirectory(t *testing.T) {
tmpDir := t.TempDir()

Expand All @@ -29,7 +44,7 @@ func TestAutocompleteDirectory(t *testing.T) {
name: "empty prefix",
prefix: "",
chdir: tmpDir,
expectedSuffixes: []string{"aaa", "aab", "aac", "aba", "abb", "abc", "xyz"},
expectedSuffixes: pathsWithTrailingSlashes([]string{"aaa", "aab", "aac", "aba", "abb", "abc", "xyz"}),
},
{
name: "base directory, no trailing slash",
Expand All @@ -38,18 +53,18 @@ func TestAutocompleteDirectory(t *testing.T) {
},
{
name: "base directory with trailing slash",
prefix: fmt.Sprintf("%s%c", tmpDir, filepath.Separator),
expectedSuffixes: []string{"aaa", "aab", "aac", "aba", "abb", "abc", "xyz"},
prefix: pathWithTrailingSlash(tmpDir),
expectedSuffixes: pathsWithTrailingSlashes([]string{"aaa", "aab", "aac", "aba", "abb", "abc", "xyz"}),
},
{
name: "first character matches",
prefix: filepath.Join(tmpDir, "x"),
expectedSuffixes: []string{"yz"},
expectedSuffixes: pathsWithTrailingSlashes([]string{"yz"}),
},
{
name: "first two characters match",
prefix: filepath.Join(tmpDir, "ab"),
expectedSuffixes: []string{"a", "b", "c"},
expectedSuffixes: pathsWithTrailingSlashes([]string{"a", "b", "c"}),
},
{
name: "all characters match",
Expand Down

0 comments on commit 06f072b

Please sign in to comment.