diff --git a/CHANGELOG.md b/CHANGELOG.md index d8b5314f..74b7783d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,7 +35,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), - Natural sorting now compares string lengths when dealing with equivalent numbers (e.g. `0` is ordered before `00`) (#2177). - A bug where the copy progress indicator only displayed the first time a file was copied is now fixed (#2181). - A bug where the `source` command does not show an error message upon failure is now fixed (#2189). -- The `addcustominfo` command as well as the `cleaner` and `previewer` options now support file completions (#2198) (#2211). +- The `addcustominfo` and `setlocal` commands as well as the `cleaner` and `previewer` options now support file completions (#2198) (#2254) (#2211). - A bug where an empty `custom` info property would still take up space is now fixed (#2208). - A bug where setting `drawbox` could lead to scrolling outside the view is now fixed (#2210) (#2218). - The preview cache is now not cleared when setting `ratios` to its current value (#2218). diff --git a/complete.go b/complete.go index b7077606..359f4e8b 100644 --- a/complete.go +++ b/complete.go @@ -310,6 +310,65 @@ func matchShellFile(s string) (matches []compMatch, result string) { return } +func matchSetlocalDir(s string) (matches []compMatch, result string) { + dir, file := filepath.Split(cmdUnescape(replaceTilde(s))) + + d := dir + if dir == "" { + d = "." + } + files, err := os.ReadDir(d) + if err != nil { + log.Printf("reading directory: %s", err) + result = s + return + } + + var commonName string + + 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) { + case 0: + result = s + default: + result = cmdEscape(dir + commonName) + } + return +} + func matchExec(s string) (matches []compMatch, result string) { var words []string for p := range strings.SplitSeq(envPath, string(filepath.ListSeparator)) { @@ -411,6 +470,10 @@ func completeCmd(acc []rune) (matches []compMatch, result string) { } } case "setlocal": + if len(f) == 2 { + matches, result = matchSetlocalDir(f[1]) + break + } if len(f) == 3 { matches, result = matchWord(f[2], gLocalOptWords) break