mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-28 03:01:57 +02:00
Allow using a base directory other than the cwd when completing files
This commit is contained in:
@@ -30,7 +30,14 @@ type FileEntry struct {
|
|||||||
is_dir, is_symlink, is_empty_dir bool
|
is_dir, is_symlink, is_empty_dir bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func complete_files(prefix string, callback func(*FileEntry)) error {
|
func complete_files(prefix string, callback func(*FileEntry), cwd string) error {
|
||||||
|
if cwd == "" {
|
||||||
|
var err error
|
||||||
|
cwd, err = os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
location := absolutize_path(prefix)
|
location := absolutize_path(prefix)
|
||||||
base_dir := ""
|
base_dir := ""
|
||||||
joinable_prefix := ""
|
joinable_prefix := ""
|
||||||
@@ -48,7 +55,7 @@ func complete_files(prefix string, callback func(*FileEntry)) error {
|
|||||||
base_dir = location
|
base_dir = location
|
||||||
joinable_prefix = "~/"
|
joinable_prefix = "~/"
|
||||||
case "":
|
case "":
|
||||||
base_dir = "."
|
base_dir = cwd
|
||||||
joinable_prefix = ""
|
joinable_prefix = ""
|
||||||
default:
|
default:
|
||||||
if strings.HasSuffix(prefix, utils.Sep) {
|
if strings.HasSuffix(prefix, utils.Sep) {
|
||||||
@@ -63,7 +70,10 @@ func complete_files(prefix string, callback func(*FileEntry)) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if base_dir == "" {
|
if base_dir == "" {
|
||||||
base_dir = "."
|
base_dir = cwd
|
||||||
|
}
|
||||||
|
if !strings.HasPrefix(base_dir, "~") && !filepath.IsAbs(base_dir) {
|
||||||
|
base_dir = filepath.Join(cwd, base_dir)
|
||||||
}
|
}
|
||||||
// fmt.Printf("prefix=%#v base_dir=%#v joinable_prefix=%#v\n", prefix, base_dir, joinable_prefix)
|
// fmt.Printf("prefix=%#v base_dir=%#v joinable_prefix=%#v\n", prefix, base_dir, joinable_prefix)
|
||||||
entries, err := os.ReadDir(base_dir)
|
entries, err := os.ReadDir(base_dir)
|
||||||
@@ -167,7 +177,7 @@ func complete_by_fnmatch(prefix string, patterns []string) []string {
|
|||||||
if matches(q) {
|
if matches(q) {
|
||||||
ans = append(ans, entry.completion_candidate)
|
ans = append(ans, entry.completion_candidate)
|
||||||
}
|
}
|
||||||
})
|
}, "")
|
||||||
return ans
|
return ans
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ func TestCompleteFiles(t *testing.T) {
|
|||||||
if _, err := os.Stat(entry.abspath); err != nil {
|
if _, err := os.Stat(entry.abspath); err != nil {
|
||||||
t.Fatalf("Abspath does not exist: %#v", entry.abspath)
|
t.Fatalf("Abspath does not exist: %#v", entry.abspath)
|
||||||
}
|
}
|
||||||
})
|
}, "")
|
||||||
sort.Strings(actual)
|
sort.Strings(actual)
|
||||||
if !reflect.DeepEqual(expected, actual) {
|
if !reflect.DeepEqual(expected, actual) {
|
||||||
t.Fatalf("Did not get expected completion candidates for prefix: %#v\nExpected: %#v\nActual: %#v", prefix, expected, actual)
|
t.Fatalf("Did not get expected completion candidates for prefix: %#v\nExpected: %#v\nActual: %#v", prefix, expected, actual)
|
||||||
|
|||||||
@@ -41,6 +41,6 @@ func complete_kitty(completions *Completions, word string, arg_num int) {
|
|||||||
} else if unix.Access(entry.abspath, unix.X_OK) == nil {
|
} else if unix.Access(entry.abspath, unix.X_OK) == nil {
|
||||||
mg.add_match(entry.completion_candidate)
|
mg.add_match(entry.completion_candidate)
|
||||||
}
|
}
|
||||||
})
|
}, "")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user