Skip to content

Commit 212a579

Browse files
committed
add: debug version
Signed-off-by: wxiwnd <wxiwnd@outlook.com>
1 parent 5f819ec commit 212a579

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

scripts/bash_pinyin_completion_debug

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ debug() {
99
}
1010
debug "Debug Session:"
1111

12-
# Load bash-completion if not loaded
13-
# [ -f /usr/share/bash-completion/bash_completion ] && \
14-
# . /usr/share/bash-completion/bash_completion
15-
1612
# Detect bash-completion
1713
if ! declare -F _comp_compgen_filedir &>/dev/null; then
1814
echo "No function _comp_compgen_filedir found. Please install bash-completion first."
@@ -24,32 +20,36 @@ eval "function __bak_comp_compgen_filedir_xspec() { $(declare -f _comp_compgen_f
2420

2521
# replace _comp_compgen_filedir
2622
_comp_compgen_filedir() {
27-
debug "Pre _comp_compgen_filedir: $*"
2823
__bak_comp_compgen_filedir "$@"
29-
debug "Original COMPREPLY: ${COMPREPLY[*]}"
3024
_pinyin_completion "$@"
31-
debug "Post COMPREPLY: ${COMPREPLY[*]}"
3225
}
3326

3427
_comp_compgen_filedir_xspec() {
35-
debug "Pre _comp_compgen_filedir: $*"
3628
__bak_comp_compgen_filedir_xspec "$@"
37-
debug "Original COMPREPLY: ${COMPREPLY[*]}"
3829
_pinyin_completion "$@"
39-
debug "Post COMPREPLY: ${COMPREPLY[*]}"
4030
}
4131

4232
_pinyin_completion() {
4333
local cur="${COMP_WORDS[COMP_CWORD]}"
44-
34+
4535
# ignore empty
4636
[ -z "$cur" ] && return
4737

38+
_expand || return 0
4839

49-
local dirpart="$(dirname -- "$cur")"
50-
local basepart="$(basename -- "$cur")"
40+
if [[ "${cur:0:1}" == "'" || "${cur:0:1}" == "\"" ]]; then
41+
local dirpart="$(dirname -- "${cur:1}")"
42+
local basepart="$(basename -- "${cur:1}")"
43+
else
44+
local dirpart="$(dirname -- "$cur")"
45+
local basepart="$(basename -- "$cur")"
46+
fi
5147

52-
[[ "$dirpart" == "." && "$cur" != */* ]] && dirpart=""
48+
debug "After extracting: dirpart='$dirpart', basepart='$basepart', cur='$cur'"
49+
50+
# realpath resolve current path as ".", if user did not enter "./" then ignore.
51+
[[ "$dirpart" == "." && "${cur:0:2}" != "./" ]] && dirpart=""
52+
debug "Adjusted dirpart: '$dirpart'"
5353

5454
local savedPWD="$PWD"
5555
local resolved_dir
@@ -58,6 +58,7 @@ _pinyin_completion() {
5858

5959
if [[ -n "$dirpart" ]]; then
6060
resolved_dir="$(realpath -- "$dirpart" 2>/dev/null)"
61+
debug "resolved_dir: '$resolved_dir'"
6162
if [[ -d "$resolved_dir" ]]; then
6263
cd -- "$resolved_dir" 2>/dev/null || return
6364
else
@@ -83,8 +84,11 @@ _pinyin_completion() {
8384
fi
8485
done | bash-pinyin-completion-rs "$basepart" 2>/dev/null
8586
)
87+
if [ ${#pinyin_matched[@]} -ne 0 ]; then
88+
compopt -o filenames 2>/dev/null
89+
fi
8690
fi
87-
91+
debug "Before join, pinyin_matched: $(declare -p pinyin_matched)"
8892
if [[ -n "$dirpart" ]]; then
8993
local sep="/"
9094
# dirpart is root
@@ -93,16 +97,20 @@ _pinyin_completion() {
9397
fi
9498

9599
for i in "${!pinyin_matched[@]}"; do
100+
debug "Before join: pinyin_matched[$i]='${pinyin_matched[$i]}'"
96101
pinyin_matched[$i]="${dirpart}${sep}${pinyin_matched[$i]}"
102+
debug "After join: pinyin_matched[$i]='${pinyin_matched[$i]}'"
97103
done
98104
fi
99105

100106
cd "$savedPWD" || return
101-
102107
# merge result
108+
debug "Final COMPREPLY before merge: $(declare -p COMPREPLY)"
103109
local -a old_candidates=("${COMPREPLY[@]}")
104110
COMPREPLY=("${old_candidates[@]}" "${pinyin_matched[@]}")
111+
debug "Final COMPREPLY after merge: $(declare -p COMPREPLY)"
105112

113+
debug "Final COMPREPLY before deduplication: $(declare -p COMPREPLY)"
106114
# mapfile -t COMPREPLY < <(printf "%s\n" "${COMPREPLY[@]}" | awk '!seen[$0]++')
107115
declare -A seen
108116
local -a unique_compreply=()
@@ -113,9 +121,11 @@ _pinyin_completion() {
113121
fi
114122
done
115123
COMPREPLY=( "${unique_compreply[@]}" )
124+
debug "Final COMPREPLY after deduplication: $(declare -p COMPREPLY)"
125+
116126

117-
# fix space postfix
118-
if ((${#COMPREPLY[@]} == 1)) && [[ ${COMPREPLY[0]} != */ ]]; then
119-
compopt -o nospace 2>/dev/null
120-
fi
127+
# # fix space postfix
128+
# if ((${#COMPREPLY[@]} == 1)) && [[ ${COMPREPLY[0]} != */ ]]; then
129+
# compopt -o nospace 2>/dev/null
130+
# fi
121131
}

0 commit comments

Comments
 (0)