Skip to content

Commit be47adf

Browse files
committed
chore: add comments
Signed-off-by: wxiwnd <[email protected]>
1 parent bfbc8d6 commit be47adf

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

scripts/bash_pinyin_completion

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,17 @@ _comp_compgen__call_builtin() {
4141

4242
# Function to add completion results
4343
_add_completion() {
44+
# cur: bash-completion's working value for the current word.
4445
local cur
4546

4647
eval "cur=${_cur}"
47-
# Save original cur (to restore later)
48+
# origin_cur: the user's raw buffer text before any expansion
49+
# including quotes or ~user prefixes. in other word, "snapshot".
4850
local orig_cur="$cur"
49-
local orig_dirpart=""
51+
# stripped_orig: an editable copy of orig_cur
52+
# used to compute orig_dirpart without mutating the original text.
5053
local stripped_orig="$orig_cur"
54+
local orig_dirpart=""
5155
if [[ "$stripped_orig" == "'"* || "$stripped_orig" == '"'* ]]; then
5256
stripped_orig="${stripped_orig:1}"
5357
fi
@@ -67,6 +71,8 @@ _add_completion() {
6771

6872
# Skip empty
6973
[[ -z "$cur" ]] && return
74+
75+
# perform bash-completion's normal expansions.
7076
_expand || return 0
7177

7278
local dirpart basepart
@@ -94,6 +100,8 @@ _add_completion() {
94100
done
95101

96102
if [[ -n "$dirpart" ]]; then
103+
# Resolve the working directory for compgen use realpath, but remember
104+
# the original textual prefix so completions can stay aligned with what the user typed.
97105
resolved_dir="$(realpath -- "$dirpart" 2>/dev/null)"
98106
if [[ -d "$resolved_dir" ]]; then
99107
cd -- "$resolved_dir" 2>/dev/null || return
@@ -102,7 +110,8 @@ _add_completion() {
102110
return
103111
fi
104112
fi
105-
113+
114+
# Kernel
106115
local -a pinyin_matched
107116
if [[ "$is_dir_only" == true ]]; then
108117
mapfile -t pinyin_matched < <(
@@ -122,6 +131,9 @@ _add_completion() {
122131
if [[ ${#pinyin_matched[@]} -gt 0 ]]; then
123132
local display_dirpart="$dirpart"
124133
if [[ -n "$orig_dirpart" ]]; then
134+
# When the user typed something like ~user/src, prefer their original prefix for display
135+
# instead of the realpath directory we temp into.
136+
# "snapshot" we saved before comes in handy here.
125137
display_dirpart="$orig_dirpart"
126138
fi
127139
if [[ -n "$display_dirpart" ]]; then
@@ -137,6 +149,8 @@ _add_completion() {
137149
orig_check="${orig_check:1}"
138150
fi
139151
if [[ "$orig_check" == ~* ]]; then
152+
# Map the tilde-prefix the user entered back onto the filesystem
153+
# path produced by compgen so the completion output preserves the symbolic form.
140154
local tilde_prefix="${orig_check%%/*}"
141155
local expanded_prefix=""
142156
if [[ "$tilde_prefix" == "~" ]]; then
@@ -148,15 +162,18 @@ _add_completion() {
148162
else
149163
local tilde_user="${tilde_prefix:1}"
150164
if [[ -n "$tilde_user" ]]; then
165+
# Find the user from passwd.
151166
expanded_prefix="$(getent passwd "$tilde_user" 2>/dev/null | cut -d: -f6)"
152167
fi
153168
fi
154169
if [[ -n "$expanded_prefix" ]]; then
155170
for i in "${!pinyin_matched[@]}"; do
156171
if [[ "${pinyin_matched[$i]}" == "$expanded_prefix" ]]; then
172+
# Exact home directory.
157173
pinyin_matched[$i]="$tilde_prefix"
158174
elif [[ "${pinyin_matched[$i]}" == "$expanded_prefix"/* ]]; then
159175
local suffix="${pinyin_matched[$i]#"$expanded_prefix/"}"
176+
# Join path under the user home.
160177
pinyin_matched[$i]="$tilde_prefix/$suffix"
161178
fi
162179
done

0 commit comments

Comments
 (0)