-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Autocomplete directories in new document text field
- Loading branch information
Showing
10 changed files
with
314 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package file | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// AutocompleteDirectory autocompletes the last subdirectory in a path. | ||
func AutocompleteDirectory(path string) ([]string, error) { | ||
baseDir, subdirPrefix := filepath.Split(path) | ||
if baseDir == "" { | ||
baseDir = "." | ||
} | ||
|
||
entries, err := os.ReadDir(baseDir) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var suffixes []string | ||
for _, e := range entries { | ||
if e.IsDir() { | ||
name := e.Name() | ||
if strings.HasPrefix(name, subdirPrefix) && len(subdirPrefix) < len(name) { | ||
suffixes = append(suffixes, name[len(subdirPrefix):]) | ||
} | ||
} | ||
} | ||
|
||
return suffixes, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package file | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAutocompleteDirectory(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
|
||
for _, subdir := range []string{"aaa", "aab", "aac", "aba", "abb", "abc", "xyz"} { | ||
path := filepath.Join(tmpDir, subdir) | ||
err := os.Mkdir(path, 0755) | ||
require.NoError(t, err) | ||
} | ||
|
||
testCases := []struct { | ||
name string | ||
prefix string | ||
chdir string | ||
expectedSuffixes []string | ||
}{ | ||
{ | ||
name: "empty prefix", | ||
prefix: "", | ||
chdir: tmpDir, | ||
expectedSuffixes: []string{"aaa", "aab", "aac", "aba", "abb", "abc", "xyz"}, | ||
}, | ||
{ | ||
name: "base directory, no trailing slash", | ||
prefix: tmpDir, | ||
expectedSuffixes: nil, | ||
}, | ||
{ | ||
name: "base directory with trailing slash", | ||
prefix: fmt.Sprintf("%s%c", tmpDir, filepath.Separator), | ||
expectedSuffixes: []string{"aaa", "aab", "aac", "aba", "abb", "abc", "xyz"}, | ||
}, | ||
{ | ||
name: "first character matches", | ||
prefix: filepath.Join(tmpDir, "x"), | ||
expectedSuffixes: []string{"yz"}, | ||
}, | ||
{ | ||
name: "first two characters match", | ||
prefix: filepath.Join(tmpDir, "ab"), | ||
expectedSuffixes: []string{"a", "b", "c"}, | ||
}, | ||
{ | ||
name: "all characters match", | ||
prefix: filepath.Join(tmpDir, "aac"), | ||
expectedSuffixes: nil, | ||
}, | ||
{ | ||
name: "no characters match", | ||
prefix: filepath.Join(tmpDir, "m"), | ||
expectedSuffixes: nil, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
if tc.chdir != "" { | ||
cwd, err := os.Getwd() | ||
require.NoError(t, err) | ||
defer func() { os.Chdir(cwd) }() | ||
|
||
err = os.Chdir(tc.chdir) | ||
require.NoError(t, err) | ||
} | ||
|
||
suffixes, err := AutocompleteDirectory(tc.prefix) | ||
require.NoError(t, err) | ||
assert.Equal(t, tc.expectedSuffixes, suffixes) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.