Skip to content

Commit 05bd2a4

Browse files
committed
update
1 parent 307bf63 commit 05bd2a4

File tree

4 files changed

+181
-58
lines changed

4 files changed

+181
-58
lines changed

custom_completions/_pnpm_enhanced

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,14 @@ fi
1010
_has_tab_completion() {
1111
local cmd="$1"
1212

13-
# Try different methods to detect if the command has Tab completions
14-
15-
# Method 1: Try running the command with the 'complete' subcommand
16-
# This is the standard Tab completion method
17-
if pnpm exec $cmd complete zsh &>/dev/null; then
18-
return 0 # Success - command has Tab completions
19-
fi
20-
21-
# Method 2: Check if running the command's completion outputs a directive
13+
# The most reliable method: Check if running the command's completion outputs a directive
2214
# Tab completions end with a line like ":4" to indicate the completion directive
23-
if pnpm exec $cmd complete -- "" 2>/dev/null | grep -q ':[0-9]\+$'; then
15+
# Use timeout to prevent hanging on commands that don't support completions
16+
if timeout 1 pnpm exec $cmd complete -- "" 2>/dev/null | grep -q ':[0-9]\+$'; then
2417
return 0 # Success - command has Tab completions
2518
fi
2619

27-
# Method 3: Check if the command's help mentions completions
28-
if pnpm exec $cmd --help 2>/dev/null | grep -q -i 'complet'; then
29-
return 0 # Success - command likely has completions
30-
fi
31-
32-
# None of the detection methods worked
20+
# No completion found
3321
return 1 # Failure - command doesn't have Tab completions
3422
}
3523

@@ -38,27 +26,10 @@ _get_tab_completions() {
3826
local cmd="$1"
3927
shift
4028
local args=("$@")
41-
local result
42-
43-
# Try different methods to get completions
44-
45-
# Method 1: Standard completion method
46-
result=$(pnpm exec $cmd complete -- "${args[@]}" 2>/dev/null)
47-
if [[ -n "$result" && $(echo "$result" | grep -c ':[0-9]\+$') -gt 0 ]]; then
48-
echo "$result"
49-
return 0
50-
fi
51-
52-
# Method 2: Try with the 'complete' subcommand, which some tools use
53-
result=$(pnpm exec $cmd complete "${args[@]}" 2>/dev/null)
54-
if [[ -n "$result" ]]; then
55-
echo "$result"
56-
return 0
57-
fi
5829

59-
# If we couldn't get any completions, return an empty result
60-
# This will make the shell fall back to its default completion behavior
61-
return 1
30+
# Standard completion method with timeout to prevent hanging
31+
timeout 1 pnpm exec $cmd complete -- "${args[@]}" 2>/dev/null
32+
return $?
6233
}
6334

6435
_pnpm() {

custom_completions/generate.sh

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
#!/usr/bin/env bash
2+
3+
# Generate the enhanced pnpm completion script
4+
cat << 'EOF'
5+
#compdef pnpm
6+
7+
if command -v pnpm-shell-completion &> /dev/null; then
8+
pnpm_comp_bin="$(which pnpm-shell-completion)"
9+
else
10+
pnpm_comp_bin="$(dirname $0)/pnpm-shell-completion"
11+
fi
12+
13+
# Function to check if a command has Tab-powered completions
14+
_has_tab_completion() {
15+
local cmd="$1"
16+
17+
# The most reliable method: Check if running the command's completion outputs a directive
18+
# Tab completions end with a line like ":4" to indicate the completion directive
19+
# Use timeout to prevent hanging on commands that don't support completions
20+
if timeout 1 pnpm exec $cmd complete -- "" 2>/dev/null | grep -q ':[0-9]\+$'; then
21+
return 0 # Success - command has Tab completions
22+
fi
23+
24+
# No completion found
25+
return 1 # Failure - command doesn't have Tab completions
26+
}
27+
28+
# Function to get completions from a Tab-powered command
29+
_get_tab_completions() {
30+
local cmd="$1"
31+
shift
32+
local args=("$@")
33+
34+
# Standard completion method with timeout to prevent hanging
35+
timeout 1 pnpm exec $cmd complete -- "${args[@]}" 2>/dev/null
36+
return $?
37+
}
38+
39+
_pnpm() {
40+
typeset -A opt_args
41+
local cmd_index=1
42+
local has_custom_completion=0
43+
local custom_cmd=""
44+
45+
# Check if we have command arguments beyond "pnpm"
46+
if (( CURRENT > 1 )); then
47+
# The first argument after pnpm might be a command with its own completion
48+
custom_cmd="${words[2]}"
49+
50+
# Check for workspace-specific flags that would shift the command position
51+
if [[ "${words[2]}" == "--filter" || "${words[2]}" == "-F" ]]; then
52+
# The command comes after the filter and value
53+
if (( CURRENT > 3 )); then
54+
custom_cmd="${words[4]}"
55+
cmd_index=4
56+
else
57+
custom_cmd=""
58+
fi
59+
fi
60+
61+
# Check if the command has Tab completions
62+
if [[ -n "$custom_cmd" ]] && _has_tab_completion "$custom_cmd"; then
63+
has_custom_completion=1
64+
fi
65+
fi
66+
67+
# If we found a command with Tab completions and we're trying to complete its arguments
68+
if [[ $has_custom_completion -eq 1 ]] && (( CURRENT > cmd_index )); then
69+
# Extract the arguments for the custom command
70+
local cmd_args=("${words[@]:cmd_index}")
71+
72+
# Get Tab completions for this command
73+
_get_tab_completions "$custom_cmd" "${cmd_args[@]}"
74+
return 0
75+
fi
76+
77+
# Original pnpm completion logic
78+
_arguments \
79+
'(--filter -F)'{--filter,-F}'=:flag:->filter' \
80+
':command:->scripts' \
81+
'*:: :->command_args'
82+
83+
local target_pkg=${opt_args[--filter]:-$opt_args[-F]}
84+
85+
case $state in
86+
filter)
87+
if [[ -f ./pnpm-workspace.yaml ]]; then
88+
_values 'filter packages' $(FEATURE=filter $pnpm_comp_bin)
89+
fi
90+
;;
91+
scripts)
92+
_values 'scripts' $(FEATURE=scripts TARGET_PKG=$target_pkg ZSH=true $pnpm_comp_bin) \
93+
add remove install update publish
94+
;;
95+
command_args)
96+
local cmd=$(FEATURE=pnpm_cmd $pnpm_comp_bin $words)
97+
case $cmd in
98+
add)
99+
_arguments \
100+
'(--global -g)'{--global,-g}'[Install as a global package]' \
101+
'(--save-dev -D)'{--save-dev,-D}'[Save package to your `devDependencies`]' \
102+
'--save-peer[Save package to your `peerDependencies` and `devDependencies`]'
103+
;;
104+
install|i)
105+
_arguments \
106+
'(--dev -D)'{--dev,-D}'[Only `devDependencies` are installed regardless of the `NODE_ENV`]' \
107+
'--fix-lockfile[Fix broken lockfile entries automatically]' \
108+
'--force[Force reinstall dependencies]' \
109+
"--ignore-scripts[Don't run lifecycle scripts]" \
110+
'--lockfile-only[Dependencies are not downloaded. Only `pnpm-lock.yaml` is updated]' \
111+
'--no-optional[`optionalDependencies` are not installed]' \
112+
'--offline[Trigger an error if any required dependencies are not available in local store]' \
113+
'--prefer-offline[Skip staleness checks for cached data, but request missing data from the server]' \
114+
'(--prod -P)'{--prod,-P}"[Packages in \`devDependencies\` won't be installed]"
115+
;;
116+
remove|rm|why)
117+
if [[ -f ./package.json ]]; then
118+
_values 'deps' $(FEATURE=deps TARGET_PKG=$target_pkg $pnpm_comp_bin)
119+
fi
120+
;;
121+
update|upgrade|up)
122+
_arguments \
123+
'(--dev -D)'{--dev,-D}'[Update packages only in "devDependencies"]' \
124+
'(--global -g)'{--global,-g}'[Update globally installed packages]' \
125+
'(--interactive -i)'{--interactive,-i}'[Show outdated dependencies and select which ones to update]' \
126+
'(--latest -L)'{--latest,-L}'[Ignore version ranges in package.json]' \
127+
"--no-optional[Don't update packages in \`optionalDependencies\`]" \
128+
'(--prod -P)'{--prod,-P}'[Update packages only in "dependencies" and "optionalDependencies"]' \
129+
'(--recursive -r)'{--recursive,-r}'[Update in every package found in subdirectories or every workspace package]'
130+
if [[ -f ./package.json ]]; then
131+
_values 'deps' $(FEATURE=deps TARGET_PKG=$target_pkg $pnpm_comp_bin)
132+
fi
133+
;;
134+
publish)
135+
_arguments \
136+
'--access=[Tells the registry whether this package should be published as public or restricted]: :(public restricted)' \
137+
'--dry-run[Does everything a publish would do except actually publishing to the registry]' \
138+
'--force[Packages are proceeded to be published even if their current version is already in the registry]' \
139+
'--ignore-scripts[Ignores any publish related lifecycle scripts (prepublishOnly, postpublish, and the like)]' \
140+
"--no-git-checks[Don't check if current branch is your publish branch, clean, and up to date]" \
141+
'--otp[Specify a one-time password]' \
142+
'--publish-branch[Sets branch name to publish]' \
143+
'(--recursive -r)'{--recursive,-r}'[Publish all packages from the workspace]' \
144+
'--tag=[Registers the published package with the given tag]'
145+
;;
146+
run)
147+
if [[ -f ./package.json ]]; then
148+
_values 'scripts' $(FEATURE=scripts TARGET_PKG=$target_pkg ZSH=true $pnpm_comp_bin)
149+
fi
150+
;;
151+
*)
152+
_files
153+
esac
154+
esac
155+
}
156+
157+
compdef _pnpm pnpm
158+
EOF

custom_completions/install.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# deprecated!! new file is: generate.sh
2+
13
#!/bin/bash
24

35
# Determine the ZSH completion directory

custom_completions/test_completion.sh

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,26 @@ echo "-----------------------------------------------------------"
2020
echo "Testing the core completion detection and delegation logic"
2121
echo "-----------------------------------------------------------"
2222

23-
# Extract and test just the core functions from the ZSH completion script
24-
# without trying to run the full ZSH completion system
25-
2623
# Function to check if a command has Tab-powered completions
2724
has_tab_completion() {
2825
local cmd="$1"
2926

30-
# For the sake of the demo, we want to simulate a working vite command
27+
# For our demo vite command
3128
if [[ "$cmd" == "vite" ]]; then
32-
# Test that our detection would work by verifying our demo script
33-
# produces Tab-style completions
34-
local result
29+
# For the demo, we need to use the tsx command directly
3530
result=$(pnpm tsx "$DEMO_SCRIPT" complete -- "" 2>/dev/null)
3631

3732
# Check if the result ends with a directive like :4
38-
if [[ "$result" =~ :[0-9]+$ ]]; then
39-
echo "Detection method works: Found completion directive in output"
33+
if echo "$result" | grep -q ':[0-9]\+$'; then
34+
echo "Found completion directive in output"
4035
return 0
4136
fi
37+
38+
echo "No completion directive found in output"
39+
echo "Output was: $result"
4240
fi
4341

44-
# For other commands, simulate a real detection
45-
echo "Would check if '$cmd' has Tab completions in a real environment"
42+
# No completion found
4643
return 1
4744
}
4845

@@ -51,17 +48,12 @@ get_tab_completions() {
5148
local cmd="$1"
5249
shift
5350
local args=("$@")
54-
local result
5551

52+
# For our demo vite command
5653
if [[ "$cmd" == "vite" ]]; then
57-
# For our demo vite command, use the demo script
58-
result=$(pnpm tsx "$DEMO_SCRIPT" complete -- "${args[@]}" 2>/dev/null)
59-
if [[ -n "$result" ]]; then
60-
echo "$result"
61-
return 0
62-
fi
63-
else
64-
echo "Would get completions for '$cmd' with args: ${args[*]}"
54+
# For the demo, we use the tsx directly
55+
pnpm tsx "$DEMO_SCRIPT" complete -- "${args[@]}" 2>/dev/null
56+
return $?
6557
fi
6658

6759
return 1
@@ -70,9 +62,9 @@ get_tab_completions() {
7062
# Test if our vite command has Tab completions
7163
echo "Testing if 'vite' has Tab completions:"
7264
if has_tab_completion "vite"; then
73-
echo "vite has Tab completions!!!"
65+
echo "vite has Tab completions!"
7466
else
75-
echo "vite does not have Tab completions!!!"
67+
echo "vite does not have Tab completions!"
7668
fi
7769

7870
echo ""

0 commit comments

Comments
 (0)