Complete paths relative to the config directory correctly

CompleteFiles resolved the prefix with filepath.Abs, which uses the
process cwd, instead of the cwd it was given. For the completers that are
relative to the config directory, like kitty --session and launch
--watcher, a prefix such as sessions/ was looked up under the shell's cwd,
so completing inside a sub-directory of the config directory gave nothing,
or the contents of a same named directory in the shell's cwd. Resolve
relative prefixes against the given cwd instead.
This commit is contained in:
devangpratap 2026-11-12 16:40:55 -05:00
parent fdea442b70
commit 30e7f3512e
2 changed files with 17 additions and 6 deletions

View file

@ -16,13 +16,12 @@ import (
var _ = fmt.Print
func absolutize_path(path string) string {
func absolutize_path(path, cwd string) string {
path = utils.Expanduser(path)
q, err := filepath.Abs(path)
if err == nil {
path = q
if !filepath.IsAbs(path) {
path = filepath.Join(cwd, path)
}
return path
return filepath.Clean(path)
}
type FileEntry struct {
@ -39,7 +38,7 @@ func CompleteFiles(prefix string, callback func(*FileEntry), cwd string) error {
return err
}
}
location := absolutize_path(prefix)
location := absolutize_path(prefix, cwd)
base_dir := ""
joinable_prefix := ""
switch prefix {

View file

@ -102,6 +102,18 @@ func TestCompleteFiles(t *testing.T) {
}
}
// paths are resolved relative to the cwd passed in, such as the config directory, not the process cwd
cdir := t.TempDir()
os.Mkdir(filepath.Join(cdir, "sessions"), 0700)
os.WriteFile(filepath.Join(cdir, "sessions", "work.kitty-session"), nil, 0600)
for _, prefix := range []string{"sessions/", "sessions/w"} {
actual := []string{}
CompleteFiles(prefix, func(entry *FileEntry) { actual = append(actual, entry.CompletionCandidate) }, cdir)
if !reflect.DeepEqual(actual, []string{"sessions/work.kitty-session"}) {
t.Fatalf("Did not complete %#v relative to the cwd argument: %#v", prefix, actual)
}
}
// symlinks to directories are directories
os.Symlink(filepath.Join(tdir, "odir"), filepath.Join(tdir, "slink"))
c := NewCompletions()