Alternate implementation for matching setlocal path (#2259)
* Alternate implementation for matching setlocal * Handle escaped paths
This commit is contained in:
1 parent
64081886c3
commit
3f46fba85b
1 file changed
+18
-49
+18
-49
@@ -311,60 +311,29 @@ func matchShellFile(s string) (matches []compMatch, result string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func matchSetlocalDir(s string) (matches []compMatch, result string) {
|
func matchSetlocalDir(s string) (matches []compMatch, result string) {
|
||||||
dir, file := filepath.Split(cmdUnescape(replaceTilde(s)))
|
matches, result = matchFile(s, true, cmdEscape, cmdUnescape)
|
||||||
|
if len(matches) == 0 {
|
||||||
d := dir
|
|
||||||
if dir == "" {
|
|
||||||
d = "."
|
|
||||||
}
|
|
||||||
files, err := os.ReadDir(d)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("reading directory: %s", err)
|
|
||||||
result = s
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var commonName string
|
trimSep := func(path string) string {
|
||||||
|
return strings.TrimSuffix(path, string(filepath.Separator))
|
||||||
for _, f := range files {
|
|
||||||
isDir := false
|
|
||||||
if f.IsDir() {
|
|
||||||
isDir = true
|
|
||||||
} else if f.Type()&os.ModeSymlink != 0 {
|
|
||||||
if stat, err := os.Stat(filepath.Join(d, f.Name())); err == nil && stat.IsDir() {
|
|
||||||
isDir = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !isDir {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if !strings.HasPrefix(strings.ToLower(f.Name()), strings.ToLower(file)) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
name := f.Name()
|
|
||||||
base := dir + name
|
|
||||||
sep := string(filepath.Separator)
|
|
||||||
|
|
||||||
matches = append(matches,
|
|
||||||
compMatch{name, cmdEscape(base)},
|
|
||||||
compMatch{name + sep, cmdEscape(base + sep)},
|
|
||||||
)
|
|
||||||
|
|
||||||
if len(matches) == 2 {
|
|
||||||
commonName = name
|
|
||||||
} else {
|
|
||||||
commonName = commonPrefix(strings.ToLower(commonName), strings.ToLower(name))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch len(matches) {
|
trimSepEsc := func(path string) string {
|
||||||
case 0:
|
return cmdEscape(trimSep(cmdUnescape(path)))
|
||||||
result = s
|
}
|
||||||
default:
|
|
||||||
result = cmdEscape(dir + commonName)
|
// add separate matches for path and recursive path
|
||||||
|
tmp := make([]compMatch, 0, len(matches)*2)
|
||||||
|
for _, match := range matches {
|
||||||
|
trimmedMatch := compMatch{trimSep(match.name), trimSepEsc(match.result)}
|
||||||
|
tmp = append(tmp, trimmedMatch, match)
|
||||||
|
}
|
||||||
|
matches = tmp
|
||||||
|
|
||||||
|
if result != s {
|
||||||
|
result = trimSepEsc(result)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user