Repository navigation
Expand file tree
/
Copy pathcompletion.zsh
More file actions
66 lines (55 loc) · 2.47 KB
/
Copy pathcompletion.zsh
File metadata and controls
66 lines (55 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env zsh
# Custom completion widget that handles both :commands and @ completion
function forge-completion() {
local current_word="${LBUFFER##* }"
# Handle @ completion (files and directories)
if [[ "$current_word" =~ ^@.*$ ]]; then
local filter_text="${current_word#@}"
local selected
local fzf_args=(
--preview="if [ -d {} ]; then ls -la --color=always {} 2>/dev/null || ls -la {}; else $_FORGE_CAT_CMD {}; fi"
$_FORGE_PREVIEW_WINDOW
)
local file_list=$($_FORGE_FD_CMD --type f --type d --hidden --exclude .git)
if [[ -n "$filter_text" ]]; then
selected=$(echo "$file_list" | _forge_fzf --query "$filter_text" "${fzf_args[@]}")
else
selected=$(echo "$file_list" | _forge_fzf "${fzf_args[@]}")
fi
if [[ -n "$selected" ]]; then
selected="@[${selected}]"
LBUFFER="${LBUFFER%$current_word}"
BUFFER="${LBUFFER}${selected}${RBUFFER}"
CURSOR=$((${#LBUFFER} + ${#selected}))
fi
zle reset-prompt
return 0
fi
# Handle :command completion (supports letters, numbers, hyphens, underscores)
if [[ "${LBUFFER}" =~ "^:([a-zA-Z][a-zA-Z0-9_-]*)?$" ]]; then
# Extract the text after the colon for filtering
local filter_text="${LBUFFER#:}"
# Lazily load the commands list
local commands_list=$(_forge_get_commands)
if [[ -n "$commands_list" ]]; then
# Use fzf for interactive selection with prefilled filter
local selected
if [[ -n "$filter_text" ]]; then
selected=$(echo "$commands_list" | _forge_fzf --header-lines=1 --delimiter="$_FORGE_DELIMITER" --nth=1 --query "$filter_text" --prompt="Command ❯ ")
else
selected=$(echo "$commands_list" | _forge_fzf --header-lines=1 --delimiter="$_FORGE_DELIMITER" --nth=1 --prompt="Command ❯ ")
fi
if [[ -n "$selected" ]]; then
# Extract just the command name (first word before any description)
local command_name="${selected%% *}"
# Replace the current buffer with the selected command
BUFFER=":$command_name "
CURSOR=${#BUFFER}
fi
fi
zle reset-prompt
return 0
fi
# Fall back to default completion
zle expand-or-complete
}