@clerc/plugin-completions 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.mjs +113 -431
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -1,46 +1,41 @@
|
|
|
1
|
-
import e from
|
|
2
|
-
|
|
1
|
+
import e from"node:assert";import{Types as t,definePlugin as n,extractParameterInfo as r,normalizeFlagValue as i,normalizeParameterValue as a}from"@clerc/core";function o(e,t){return`#compdef ${e}
|
|
2
|
+
compdef _${e} ${e}
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
function t(e$1, t$1) {
|
|
6
|
-
return `#compdef ${e$1}
|
|
7
|
-
compdef _${e$1} ${e$1}
|
|
4
|
+
# zsh completion for ${e} -*- shell-script -*-
|
|
8
5
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
__${e$1}_debug() {
|
|
6
|
+
__${e}_debug() {
|
|
12
7
|
local file="$BASH_COMP_DEBUG_FILE"
|
|
13
8
|
if [[ -n \${file} ]]; then
|
|
14
9
|
echo "$*" >> "\${file}"
|
|
15
10
|
fi
|
|
16
11
|
}
|
|
17
12
|
|
|
18
|
-
_${e
|
|
19
|
-
local shellCompDirectiveError=${
|
|
20
|
-
local shellCompDirectiveNoSpace=${
|
|
21
|
-
local shellCompDirectiveNoFileComp=${
|
|
22
|
-
local shellCompDirectiveFilterFileExt=${
|
|
23
|
-
local shellCompDirectiveFilterDirs=${
|
|
24
|
-
local shellCompDirectiveKeepOrder=${
|
|
13
|
+
_${e}() {
|
|
14
|
+
local shellCompDirectiveError=${u.ShellCompDirectiveError}
|
|
15
|
+
local shellCompDirectiveNoSpace=${u.ShellCompDirectiveNoSpace}
|
|
16
|
+
local shellCompDirectiveNoFileComp=${u.ShellCompDirectiveNoFileComp}
|
|
17
|
+
local shellCompDirectiveFilterFileExt=${u.ShellCompDirectiveFilterFileExt}
|
|
18
|
+
local shellCompDirectiveFilterDirs=${u.ShellCompDirectiveFilterDirs}
|
|
19
|
+
local shellCompDirectiveKeepOrder=${u.ShellCompDirectiveKeepOrder}
|
|
25
20
|
|
|
26
21
|
local lastParam lastChar flagPrefix requestComp out directive comp lastComp noSpace keepOrder
|
|
27
22
|
local -a completions
|
|
28
23
|
|
|
29
|
-
__${e
|
|
30
|
-
__${e
|
|
24
|
+
__${e}_debug "\\n========= starting completion logic =========="
|
|
25
|
+
__${e}_debug "CURRENT: \${CURRENT}, words[*]: \${words[*]}"
|
|
31
26
|
|
|
32
27
|
# The user could have moved the cursor backwards on the command-line.
|
|
33
28
|
# We need to trigger completion from the $CURRENT location, so we need
|
|
34
29
|
# to truncate the command-line ($words) up to the $CURRENT location.
|
|
35
30
|
# (We cannot use $CURSOR as its value does not work when a command is an alias.)
|
|
36
31
|
words=( "\${=words[1,CURRENT]}" )
|
|
37
|
-
__${e
|
|
32
|
+
__${e}_debug "Truncated words[*]: \${words[*]},"
|
|
38
33
|
|
|
39
34
|
lastParam=\${words[-1]}
|
|
40
35
|
lastChar=\${lastParam[-1]}
|
|
41
|
-
__${e
|
|
36
|
+
__${e}_debug "lastParam: \${lastParam}, lastChar: \${lastChar}"
|
|
42
37
|
|
|
43
|
-
# For zsh, when completing a flag with an = (e.g., ${e
|
|
38
|
+
# For zsh, when completing a flag with an = (e.g., ${e} -n=<TAB>)
|
|
44
39
|
# completions must be prefixed with the flag
|
|
45
40
|
setopt local_options BASH_REMATCH
|
|
46
41
|
if [[ "\${lastParam}" =~ '-.*=' ]]; then
|
|
@@ -53,7 +48,7 @@ _${e$1}() {
|
|
|
53
48
|
if [ "\${lastChar}" = "" ]; then
|
|
54
49
|
# If the last parameter is complete (there is a space following it)
|
|
55
50
|
# We add an extra empty parameter so we can indicate this to the go completion code.
|
|
56
|
-
__${e
|
|
51
|
+
__${e}_debug "Adding extra empty parameter"
|
|
57
52
|
args_to_quote+=("")
|
|
58
53
|
fi
|
|
59
54
|
|
|
@@ -61,20 +56,20 @@ _${e$1}() {
|
|
|
61
56
|
local quoted_args=("\${(@q)args_to_quote}")
|
|
62
57
|
|
|
63
58
|
# Join the main command and the quoted arguments into a single string for eval
|
|
64
|
-
requestComp="${t
|
|
59
|
+
requestComp="${t} complete -- \${quoted_args[*]}"
|
|
65
60
|
|
|
66
|
-
__${e
|
|
61
|
+
__${e}_debug "About to call: eval \${requestComp}"
|
|
67
62
|
|
|
68
63
|
# Use eval to handle any environment variables and such
|
|
69
64
|
out=$(eval \${requestComp} 2>/dev/null)
|
|
70
|
-
__${e
|
|
65
|
+
__${e}_debug "completion output: \${out}"
|
|
71
66
|
|
|
72
67
|
# Extract the directive integer following a : from the last line
|
|
73
68
|
local lastLine
|
|
74
69
|
while IFS='\n' read -r line; do
|
|
75
70
|
lastLine=\${line}
|
|
76
71
|
done < <(printf "%s\n" "\${out[@]}")
|
|
77
|
-
__${e
|
|
72
|
+
__${e}_debug "last line: \${lastLine}"
|
|
78
73
|
|
|
79
74
|
if [ "\${lastLine[1]}" = : ]; then
|
|
80
75
|
directive=\${lastLine[2,-1]}
|
|
@@ -84,16 +79,16 @@ _${e$1}() {
|
|
|
84
79
|
out=\${out[1,-$suffix]}
|
|
85
80
|
else
|
|
86
81
|
# There is no directive specified. Leave $out as is.
|
|
87
|
-
__${e
|
|
82
|
+
__${e}_debug "No directive found. Setting to default"
|
|
88
83
|
directive=0
|
|
89
84
|
fi
|
|
90
85
|
|
|
91
|
-
__${e
|
|
92
|
-
__${e
|
|
93
|
-
__${e
|
|
86
|
+
__${e}_debug "directive: \${directive}"
|
|
87
|
+
__${e}_debug "completions: \${out}"
|
|
88
|
+
__${e}_debug "flagPrefix: \${flagPrefix}"
|
|
94
89
|
|
|
95
90
|
if [ $((directive & shellCompDirectiveError)) -ne 0 ]; then
|
|
96
|
-
__${e
|
|
91
|
+
__${e}_debug "Completion received error. Ignoring completions."
|
|
97
92
|
return
|
|
98
93
|
fi
|
|
99
94
|
|
|
@@ -104,11 +99,11 @@ _${e$1}() {
|
|
|
104
99
|
while IFS='\n' read -r comp; do
|
|
105
100
|
# Check if this is an activeHelp statement (i.e., prefixed with $activeHelpMarker)
|
|
106
101
|
if [ "\${comp[1,$endIndex]}" = "$activeHelpMarker" ];then
|
|
107
|
-
__${e
|
|
102
|
+
__${e}_debug "ActiveHelp found: $comp"
|
|
108
103
|
comp="\${comp[$startIndex,-1]}"
|
|
109
104
|
if [ -n "$comp" ]; then
|
|
110
105
|
compadd -x "\${comp}"
|
|
111
|
-
__${e
|
|
106
|
+
__${e}_debug "ActiveHelp will need delimiter"
|
|
112
107
|
hasActiveHelp=1
|
|
113
108
|
fi
|
|
114
109
|
continue
|
|
@@ -124,7 +119,7 @@ _${e$1}() {
|
|
|
124
119
|
local tab="$(printf '\\t')"
|
|
125
120
|
comp=\${comp//$tab/:}
|
|
126
121
|
|
|
127
|
-
__${e
|
|
122
|
+
__${e}_debug "Adding completion: \${comp}"
|
|
128
123
|
completions+=\${comp}
|
|
129
124
|
lastComp=$comp
|
|
130
125
|
fi
|
|
@@ -135,19 +130,19 @@ _${e$1}() {
|
|
|
135
130
|
# - file completion will be performed (so there will be choices after the activeHelp)
|
|
136
131
|
if [ $hasActiveHelp -eq 1 ]; then
|
|
137
132
|
if [ \${#completions} -ne 0 ] || [ $((directive & shellCompDirectiveNoFileComp)) -eq 0 ]; then
|
|
138
|
-
__${e
|
|
133
|
+
__${e}_debug "Adding activeHelp delimiter"
|
|
139
134
|
compadd -x "--"
|
|
140
135
|
hasActiveHelp=0
|
|
141
136
|
fi
|
|
142
137
|
fi
|
|
143
138
|
|
|
144
139
|
if [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ]; then
|
|
145
|
-
__${e
|
|
140
|
+
__${e}_debug "Activating nospace."
|
|
146
141
|
noSpace="-S ''"
|
|
147
142
|
fi
|
|
148
143
|
|
|
149
144
|
if [ $((directive & shellCompDirectiveKeepOrder)) -ne 0 ]; then
|
|
150
|
-
__${e
|
|
145
|
+
__${e}_debug "Activating keep order."
|
|
151
146
|
keepOrder="-V"
|
|
152
147
|
fi
|
|
153
148
|
|
|
@@ -164,17 +159,17 @@ _${e$1}() {
|
|
|
164
159
|
done
|
|
165
160
|
filteringCmd+=" \${flagPrefix}"
|
|
166
161
|
|
|
167
|
-
__${e
|
|
162
|
+
__${e}_debug "File filtering command: $filteringCmd"
|
|
168
163
|
_arguments '*:filename:'"$filteringCmd"
|
|
169
164
|
elif [ $((directive & shellCompDirectiveFilterDirs)) -ne 0 ]; then
|
|
170
165
|
# File completion for directories only
|
|
171
166
|
local subdir
|
|
172
167
|
subdir="\${completions[1]}"
|
|
173
168
|
if [ -n "$subdir" ]; then
|
|
174
|
-
__${e
|
|
169
|
+
__${e}_debug "Listing directories in $subdir"
|
|
175
170
|
pushd "\${subdir}" >/dev/null 2>&1
|
|
176
171
|
else
|
|
177
|
-
__${e
|
|
172
|
+
__${e}_debug "Listing directories in ."
|
|
178
173
|
fi
|
|
179
174
|
|
|
180
175
|
local result
|
|
@@ -185,17 +180,17 @@ _${e$1}() {
|
|
|
185
180
|
fi
|
|
186
181
|
return $result
|
|
187
182
|
else
|
|
188
|
-
__${e
|
|
183
|
+
__${e}_debug "Calling _describe"
|
|
189
184
|
if eval _describe $keepOrder "completions" completions -Q \${flagPrefix} \${noSpace}; then
|
|
190
|
-
__${e
|
|
185
|
+
__${e}_debug "_describe found some completions"
|
|
191
186
|
|
|
192
187
|
# Return the success of having called _describe
|
|
193
188
|
return 0
|
|
194
189
|
else
|
|
195
|
-
__${e
|
|
196
|
-
__${e
|
|
190
|
+
__${e}_debug "_describe did not find completions."
|
|
191
|
+
__${e}_debug "Checking if we should do file completion."
|
|
197
192
|
if [ $((directive & shellCompDirectiveNoFileComp)) -ne 0 ]; then
|
|
198
|
-
__${e
|
|
193
|
+
__${e}_debug "deactivating file completion"
|
|
199
194
|
|
|
200
195
|
# Return 0 to indicate completion is finished and prevent zsh from
|
|
201
196
|
# trying other completion algorithms (which could cause hanging).
|
|
@@ -203,7 +198,7 @@ _${e$1}() {
|
|
|
203
198
|
return 0
|
|
204
199
|
else
|
|
205
200
|
# Perform file completion
|
|
206
|
-
__${e
|
|
201
|
+
__${e}_debug "Activating file completion"
|
|
207
202
|
|
|
208
203
|
# We must return the result of this command, so it must be the
|
|
209
204
|
# last command, or else we must store its result to return it.
|
|
@@ -214,39 +209,35 @@ _${e$1}() {
|
|
|
214
209
|
}
|
|
215
210
|
|
|
216
211
|
# don't run the completion function when being sourced or eval-ed
|
|
217
|
-
if [ "\${funcstack[1]}" = "_${e
|
|
218
|
-
_${e
|
|
212
|
+
if [ "\${funcstack[1]}" = "_${e}" ]; then
|
|
213
|
+
_${e}
|
|
219
214
|
fi
|
|
220
|
-
|
|
221
|
-
}
|
|
222
|
-
function n(e$1, t$1) {
|
|
223
|
-
let n$1 = e$1.replace(/[-:]/g, `_`);
|
|
224
|
-
return `# bash completion for ${e$1}
|
|
215
|
+
`}function s(e,t){let n=e.replace(/[-:]/g,`_`);return`# bash completion for ${e}
|
|
225
216
|
|
|
226
217
|
# Define shell completion directives
|
|
227
|
-
readonly ShellCompDirectiveError=${
|
|
228
|
-
readonly ShellCompDirectiveNoSpace=${
|
|
229
|
-
readonly ShellCompDirectiveNoFileComp=${
|
|
230
|
-
readonly ShellCompDirectiveFilterFileExt=${
|
|
231
|
-
readonly ShellCompDirectiveFilterDirs=${
|
|
232
|
-
readonly ShellCompDirectiveKeepOrder=${
|
|
218
|
+
readonly ShellCompDirectiveError=${u.ShellCompDirectiveError}
|
|
219
|
+
readonly ShellCompDirectiveNoSpace=${u.ShellCompDirectiveNoSpace}
|
|
220
|
+
readonly ShellCompDirectiveNoFileComp=${u.ShellCompDirectiveNoFileComp}
|
|
221
|
+
readonly ShellCompDirectiveFilterFileExt=${u.ShellCompDirectiveFilterFileExt}
|
|
222
|
+
readonly ShellCompDirectiveFilterDirs=${u.ShellCompDirectiveFilterDirs}
|
|
223
|
+
readonly ShellCompDirectiveKeepOrder=${u.ShellCompDirectiveKeepOrder}
|
|
233
224
|
|
|
234
225
|
# Function to debug completion
|
|
235
|
-
__${n
|
|
226
|
+
__${n}_debug() {
|
|
236
227
|
if [[ -n \${BASH_COMP_DEBUG_FILE:-} ]]; then
|
|
237
228
|
echo "$*" >> "\${BASH_COMP_DEBUG_FILE}"
|
|
238
229
|
fi
|
|
239
230
|
}
|
|
240
231
|
|
|
241
232
|
# Function to handle completions
|
|
242
|
-
__${n
|
|
233
|
+
__${n}_complete() {
|
|
243
234
|
local cur prev words cword
|
|
244
235
|
_get_comp_words_by_ref -n "=:" cur prev words cword
|
|
245
236
|
|
|
246
237
|
local requestComp out directive
|
|
247
238
|
|
|
248
239
|
# Build the command to get completions
|
|
249
|
-
requestComp="${t
|
|
240
|
+
requestComp="${t} complete -- \${words[@]:1}"
|
|
250
241
|
|
|
251
242
|
# Add an empty parameter if the last parameter is complete
|
|
252
243
|
if [[ -z "$cur" ]]; then
|
|
@@ -321,30 +312,26 @@ __${n$1}_complete() {
|
|
|
321
312
|
}
|
|
322
313
|
|
|
323
314
|
# Register completion function
|
|
324
|
-
complete -F __${n
|
|
325
|
-
|
|
326
|
-
}
|
|
327
|
-
function r(e$1, t$1) {
|
|
328
|
-
let n$1 = e$1.replace(/[-:]/g, `_`);
|
|
329
|
-
return `# fish completion for ${e$1} -*- shell-script -*-
|
|
315
|
+
complete -F __${n}_complete ${e}
|
|
316
|
+
`}function c(e,t){let n=e.replace(/[-:]/g,`_`);return`# fish completion for ${e} -*- shell-script -*-
|
|
330
317
|
|
|
331
318
|
# Define shell completion directives
|
|
332
|
-
set -l ShellCompDirectiveError ${
|
|
333
|
-
set -l ShellCompDirectiveNoSpace ${
|
|
334
|
-
set -l ShellCompDirectiveNoFileComp ${
|
|
335
|
-
set -l ShellCompDirectiveFilterFileExt ${
|
|
336
|
-
set -l ShellCompDirectiveFilterDirs ${
|
|
337
|
-
set -l ShellCompDirectiveKeepOrder ${
|
|
338
|
-
|
|
339
|
-
function __${n
|
|
319
|
+
set -l ShellCompDirectiveError ${u.ShellCompDirectiveError}
|
|
320
|
+
set -l ShellCompDirectiveNoSpace ${u.ShellCompDirectiveNoSpace}
|
|
321
|
+
set -l ShellCompDirectiveNoFileComp ${u.ShellCompDirectiveNoFileComp}
|
|
322
|
+
set -l ShellCompDirectiveFilterFileExt ${u.ShellCompDirectiveFilterFileExt}
|
|
323
|
+
set -l ShellCompDirectiveFilterDirs ${u.ShellCompDirectiveFilterDirs}
|
|
324
|
+
set -l ShellCompDirectiveKeepOrder ${u.ShellCompDirectiveKeepOrder}
|
|
325
|
+
|
|
326
|
+
function __${n}_debug
|
|
340
327
|
set -l file "$BASH_COMP_DEBUG_FILE"
|
|
341
328
|
if test -n "$file"
|
|
342
329
|
echo "$argv" >> $file
|
|
343
330
|
end
|
|
344
331
|
end
|
|
345
332
|
|
|
346
|
-
function __${n
|
|
347
|
-
__${n
|
|
333
|
+
function __${n}_perform_completion
|
|
334
|
+
__${n}_debug "Starting __${n}_perform_completion"
|
|
348
335
|
|
|
349
336
|
# Extract all args except the completion flag
|
|
350
337
|
set -l args (string match -v -- "--completion=" (commandline -opc))
|
|
@@ -358,12 +345,12 @@ function __${n$1}_perform_completion
|
|
|
358
345
|
set flag_prefix "--flag="
|
|
359
346
|
end
|
|
360
347
|
|
|
361
|
-
__${n
|
|
362
|
-
__${n
|
|
348
|
+
__${n}_debug "Current token: $current_token"
|
|
349
|
+
__${n}_debug "All args: $args"
|
|
363
350
|
|
|
364
351
|
# Call the completion program and get the results
|
|
365
|
-
set -l requestComp "${t
|
|
366
|
-
__${n
|
|
352
|
+
set -l requestComp "${t} complete -- $args"
|
|
353
|
+
__${n}_debug "Calling $requestComp"
|
|
367
354
|
set -l results (eval $requestComp 2> /dev/null)
|
|
368
355
|
|
|
369
356
|
# Some programs may output extra empty lines after the directive.
|
|
@@ -383,12 +370,12 @@ function __${n$1}_perform_completion
|
|
|
383
370
|
set directive_num 0
|
|
384
371
|
end
|
|
385
372
|
|
|
386
|
-
__${n
|
|
373
|
+
__${n}_debug "Directive: $directive_num"
|
|
387
374
|
|
|
388
375
|
# Process completions based on directive
|
|
389
376
|
if test $directive_num -eq $ShellCompDirectiveError
|
|
390
377
|
# Error code. No completion.
|
|
391
|
-
__${n
|
|
378
|
+
__${n}_debug "Received error directive: aborting."
|
|
392
379
|
return 1
|
|
393
380
|
end
|
|
394
381
|
|
|
@@ -400,23 +387,23 @@ function __${n$1}_perform_completion
|
|
|
400
387
|
# No completions, let fish handle file completions unless forbidden
|
|
401
388
|
if test (count $results) -eq 0
|
|
402
389
|
if test $directive_num -ne $ShellCompDirectiveNoFileComp
|
|
403
|
-
__${n
|
|
390
|
+
__${n}_debug "No completions, performing file completion"
|
|
404
391
|
return 1
|
|
405
392
|
end
|
|
406
|
-
__${n
|
|
393
|
+
__${n}_debug "No completions, but file completion forbidden"
|
|
407
394
|
return 0
|
|
408
395
|
end
|
|
409
396
|
|
|
410
397
|
# Filter file extensions
|
|
411
398
|
if test $directive_num -eq $ShellCompDirectiveFilterFileExt
|
|
412
|
-
__${n
|
|
399
|
+
__${n}_debug "File extension filtering"
|
|
413
400
|
set -l file_extensions
|
|
414
401
|
for item in $results
|
|
415
402
|
if test -n "$item" -a (string sub -s 1 -l 1 -- $item) != "-"
|
|
416
403
|
set -a file_extensions "*$item"
|
|
417
404
|
end
|
|
418
405
|
end
|
|
419
|
-
__${n
|
|
406
|
+
__${n}_debug "File extensions: $file_extensions"
|
|
420
407
|
|
|
421
408
|
# Use the file extensions as completions
|
|
422
409
|
set -l completions
|
|
@@ -433,7 +420,7 @@ function __${n$1}_perform_completion
|
|
|
433
420
|
|
|
434
421
|
# Filter directories
|
|
435
422
|
if test $directive_num -eq $ShellCompDirectiveFilterDirs
|
|
436
|
-
__${n
|
|
423
|
+
__${n}_debug "Directory filtering"
|
|
437
424
|
set -l dirs
|
|
438
425
|
for item in $results
|
|
439
426
|
if test -d "$item"
|
|
@@ -473,26 +460,22 @@ function __${n$1}_perform_completion
|
|
|
473
460
|
return 0
|
|
474
461
|
end
|
|
475
462
|
|
|
476
|
-
# Set up the completion for the ${e
|
|
477
|
-
complete -c ${e
|
|
478
|
-
|
|
479
|
-
}
|
|
480
|
-
function i(e$1, t$1) {
|
|
481
|
-
let n$1 = e$1.replace(/[-:]/g, `_`);
|
|
482
|
-
return `# powershell completion for ${e$1} -*- shell-script -*-
|
|
463
|
+
# Set up the completion for the ${e} command
|
|
464
|
+
complete -c ${e} -f -a "(eval __${n}_perform_completion)"
|
|
465
|
+
`}function l(e,t){let n=e.replace(/[-:]/g,`_`);return`# powershell completion for ${e} -*- shell-script -*-
|
|
483
466
|
|
|
484
467
|
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
|
485
|
-
function __${e
|
|
468
|
+
function __${e}_debug {
|
|
486
469
|
if ($env:BASH_COMP_DEBUG_FILE) {
|
|
487
470
|
"$args" | Out-File -Append -FilePath "$env:BASH_COMP_DEBUG_FILE"
|
|
488
471
|
}
|
|
489
472
|
}
|
|
490
473
|
|
|
491
|
-
filter __${e
|
|
474
|
+
filter __${e}_escapeStringWithSpecialChars {
|
|
492
475
|
$_ -replace '\\s|#|@|\\$|;|,|''|\\{|\\}|\\(|\\)|"|\\||<|>|&','\`$&'
|
|
493
476
|
}
|
|
494
477
|
|
|
495
|
-
[scriptblock]$__${n
|
|
478
|
+
[scriptblock]$__${n}CompleterBlock = {
|
|
496
479
|
param(
|
|
497
480
|
$WordToComplete,
|
|
498
481
|
$CommandAst,
|
|
@@ -503,9 +486,9 @@ function i(e$1, t$1) {
|
|
|
503
486
|
$Command = $CommandAst.CommandElements
|
|
504
487
|
$Command = "$Command"
|
|
505
488
|
|
|
506
|
-
__${e
|
|
507
|
-
__${e
|
|
508
|
-
__${e
|
|
489
|
+
__${e}_debug ""
|
|
490
|
+
__${e}_debug "========= starting completion logic =========="
|
|
491
|
+
__${e}_debug "WordToComplete: $WordToComplete Command: $Command CursorPosition: $CursorPosition"
|
|
509
492
|
|
|
510
493
|
# The user could have moved the cursor backwards on the command-line.
|
|
511
494
|
# We need to trigger completion from the $CursorPosition location, so we need
|
|
@@ -515,21 +498,21 @@ function i(e$1, t$1) {
|
|
|
515
498
|
if ($Command.Length -gt $CursorPosition) {
|
|
516
499
|
$Command = $Command.Substring(0, $CursorPosition)
|
|
517
500
|
}
|
|
518
|
-
__${e
|
|
501
|
+
__${e}_debug "Truncated command: $Command"
|
|
519
502
|
|
|
520
|
-
$ShellCompDirectiveError=${
|
|
521
|
-
$ShellCompDirectiveNoSpace=${
|
|
522
|
-
$ShellCompDirectiveNoFileComp=${
|
|
523
|
-
$ShellCompDirectiveFilterFileExt=${
|
|
524
|
-
$ShellCompDirectiveFilterDirs=${
|
|
525
|
-
$ShellCompDirectiveKeepOrder=${
|
|
503
|
+
$ShellCompDirectiveError=${u.ShellCompDirectiveError}
|
|
504
|
+
$ShellCompDirectiveNoSpace=${u.ShellCompDirectiveNoSpace}
|
|
505
|
+
$ShellCompDirectiveNoFileComp=${u.ShellCompDirectiveNoFileComp}
|
|
506
|
+
$ShellCompDirectiveFilterFileExt=${u.ShellCompDirectiveFilterFileExt}
|
|
507
|
+
$ShellCompDirectiveFilterDirs=${u.ShellCompDirectiveFilterDirs}
|
|
508
|
+
$ShellCompDirectiveKeepOrder=${u.ShellCompDirectiveKeepOrder}
|
|
526
509
|
|
|
527
510
|
# Prepare the command to request completions for the program.
|
|
528
511
|
# Split the command at the first space to separate the program and arguments.
|
|
529
512
|
$Program, $Arguments = $Command.Split(" ", 2)
|
|
530
513
|
|
|
531
|
-
$RequestComp = "& ${t
|
|
532
|
-
__${e
|
|
514
|
+
$RequestComp = "& ${t} complete -- $Arguments"
|
|
515
|
+
__${e}_debug "RequestComp: $RequestComp"
|
|
533
516
|
|
|
534
517
|
# we cannot use $WordToComplete because it
|
|
535
518
|
# has the wrong values if the cursor was moved
|
|
@@ -537,13 +520,13 @@ function i(e$1, t$1) {
|
|
|
537
520
|
if ($WordToComplete -ne "" ) {
|
|
538
521
|
$WordToComplete = $Arguments.Split(" ")[-1]
|
|
539
522
|
}
|
|
540
|
-
__${e
|
|
523
|
+
__${e}_debug "New WordToComplete: $WordToComplete"
|
|
541
524
|
|
|
542
525
|
|
|
543
526
|
# Check for flag with equal sign
|
|
544
527
|
$IsEqualFlag = ($WordToComplete -Like "--*=*" )
|
|
545
528
|
if ( $IsEqualFlag ) {
|
|
546
|
-
__${e
|
|
529
|
+
__${e}_debug "Completing equal sign flag"
|
|
547
530
|
# Remove the flag part
|
|
548
531
|
$Flag, $WordToComplete = $WordToComplete.Split("=", 2)
|
|
549
532
|
}
|
|
@@ -551,7 +534,7 @@ function i(e$1, t$1) {
|
|
|
551
534
|
if ( $WordToComplete -eq "" -And ( -Not $IsEqualFlag )) {
|
|
552
535
|
# If the last parameter is complete (there is a space following it)
|
|
553
536
|
# We add an extra empty parameter so we can indicate this to the go method.
|
|
554
|
-
__${e
|
|
537
|
+
__${e}_debug "Adding extra empty parameter"
|
|
555
538
|
# PowerShell 7.2+ changed the way how the arguments are passed to executables,
|
|
556
539
|
# so for pre-7.2 or when Legacy argument passing is enabled we need to use
|
|
557
540
|
if ($PSVersionTable.PsVersion -lt [version]'7.2.0' -or
|
|
@@ -564,7 +547,7 @@ function i(e$1, t$1) {
|
|
|
564
547
|
}
|
|
565
548
|
}
|
|
566
549
|
|
|
567
|
-
__${e
|
|
550
|
+
__${e}_debug "Calling $RequestComp"
|
|
568
551
|
# First disable ActiveHelp which is not supported for Powershell
|
|
569
552
|
$env:ActiveHelp = 0
|
|
570
553
|
|
|
@@ -578,15 +561,15 @@ function i(e$1, t$1) {
|
|
|
578
561
|
# There is no directive specified
|
|
579
562
|
$Directive = 0
|
|
580
563
|
}
|
|
581
|
-
__${e
|
|
564
|
+
__${e}_debug "The completion directive is: $Directive"
|
|
582
565
|
|
|
583
566
|
# remove directive (last element) from out
|
|
584
567
|
$Out = $Out | Where-Object { $_ -ne $Out[-1] }
|
|
585
|
-
__${e
|
|
568
|
+
__${e}_debug "The completions are: $Out"
|
|
586
569
|
|
|
587
570
|
if (($Directive -band $ShellCompDirectiveError) -ne 0 ) {
|
|
588
571
|
# Error code. No completion.
|
|
589
|
-
__${e
|
|
572
|
+
__${e}_debug "Received error from custom completion go code"
|
|
590
573
|
return
|
|
591
574
|
}
|
|
592
575
|
|
|
@@ -594,7 +577,7 @@ function i(e$1, t$1) {
|
|
|
594
577
|
[Array]$Values = $Out | ForEach-Object {
|
|
595
578
|
# Split the output in name and description
|
|
596
579
|
$Name, $Description = $_.Split("\`t", 2)
|
|
597
|
-
__${e
|
|
580
|
+
__${e}_debug "Name: $Name Description: $Description"
|
|
598
581
|
|
|
599
582
|
# Look for the longest completion so that we can format things nicely
|
|
600
583
|
if ($Longest -lt $Name.Length) {
|
|
@@ -613,13 +596,13 @@ function i(e$1, t$1) {
|
|
|
613
596
|
$Space = " "
|
|
614
597
|
if (($Directive -band $ShellCompDirectiveNoSpace) -ne 0 ) {
|
|
615
598
|
# remove the space here
|
|
616
|
-
__${e
|
|
599
|
+
__${e}_debug "ShellCompDirectiveNoSpace is called"
|
|
617
600
|
$Space = ""
|
|
618
601
|
}
|
|
619
602
|
|
|
620
603
|
if ((($Directive -band $ShellCompDirectiveFilterFileExt) -ne 0 ) -or
|
|
621
604
|
(($Directive -band $ShellCompDirectiveFilterDirs) -ne 0 )) {
|
|
622
|
-
__${e
|
|
605
|
+
__${e}_debug "ShellCompDirectiveFilterFileExt ShellCompDirectiveFilterDirs are not supported"
|
|
623
606
|
|
|
624
607
|
# return here to prevent the completion of the extensions
|
|
625
608
|
return
|
|
@@ -631,7 +614,7 @@ function i(e$1, t$1) {
|
|
|
631
614
|
|
|
632
615
|
# Join the flag back if we have an equal sign flag
|
|
633
616
|
if ( $IsEqualFlag ) {
|
|
634
|
-
__${e
|
|
617
|
+
__${e}_debug "Join the equal sign flag back to the completion value"
|
|
635
618
|
$_.Name = $Flag + "=" + $_.Name
|
|
636
619
|
}
|
|
637
620
|
}
|
|
@@ -642,7 +625,7 @@ function i(e$1, t$1) {
|
|
|
642
625
|
}
|
|
643
626
|
|
|
644
627
|
if (($Directive -band $ShellCompDirectiveNoFileComp) -ne 0 ) {
|
|
645
|
-
__${e
|
|
628
|
+
__${e}_debug "ShellCompDirectiveNoFileComp is called"
|
|
646
629
|
|
|
647
630
|
if ($Values.Length -eq 0) {
|
|
648
631
|
# Just print an empty string here so the
|
|
@@ -656,7 +639,7 @@ function i(e$1, t$1) {
|
|
|
656
639
|
|
|
657
640
|
# Get the current mode
|
|
658
641
|
$Mode = (Get-PSReadLineKeyHandler | Where-Object { $_.Key -eq "Tab" }).Function
|
|
659
|
-
__${e
|
|
642
|
+
__${e}_debug "Mode: $Mode"
|
|
660
643
|
|
|
661
644
|
$Values | ForEach-Object {
|
|
662
645
|
|
|
@@ -681,10 +664,10 @@ function i(e$1, t$1) {
|
|
|
681
664
|
"Complete" {
|
|
682
665
|
|
|
683
666
|
if ($Values.Length -eq 1) {
|
|
684
|
-
__${e
|
|
667
|
+
__${e}_debug "Only one completion left"
|
|
685
668
|
|
|
686
669
|
# insert space after value
|
|
687
|
-
[System.Management.Automation.CompletionResult]::new($($comp.Name | __${e
|
|
670
|
+
[System.Management.Automation.CompletionResult]::new($($comp.Name | __${e}_escapeStringWithSpecialChars) + $Space, "$($comp.Name)", 'ParameterValue', "$($comp.Description)")
|
|
688
671
|
|
|
689
672
|
} else {
|
|
690
673
|
# Add the proper number of spaces to align the descriptions
|
|
@@ -708,7 +691,7 @@ function i(e$1, t$1) {
|
|
|
708
691
|
# insert space after value
|
|
709
692
|
# MenuComplete will automatically show the ToolTip of
|
|
710
693
|
# the highlighted value at the bottom of the suggestions.
|
|
711
|
-
[System.Management.Automation.CompletionResult]::new($($comp.Name | __${e
|
|
694
|
+
[System.Management.Automation.CompletionResult]::new($($comp.Name | __${e}_escapeStringWithSpecialChars) + $Space, "$($comp.Name)", 'ParameterValue', "$($comp.Description)")
|
|
712
695
|
}
|
|
713
696
|
|
|
714
697
|
# TabCompleteNext and in case we get something unknown
|
|
@@ -716,313 +699,12 @@ function i(e$1, t$1) {
|
|
|
716
699
|
# Like MenuComplete but we don't want to add a space here because
|
|
717
700
|
# the user need to press space anyway to get the completion.
|
|
718
701
|
# Description will not be shown because that's not possible with TabCompleteNext
|
|
719
|
-
[System.Management.Automation.CompletionResult]::new($($comp.Name | __${e
|
|
702
|
+
[System.Management.Automation.CompletionResult]::new($($comp.Name | __${e}_escapeStringWithSpecialChars), "$($comp.Name)", 'ParameterValue', "$($comp.Description)")
|
|
720
703
|
}
|
|
721
704
|
}
|
|
722
705
|
|
|
723
706
|
}
|
|
724
707
|
}
|
|
725
708
|
|
|
726
|
-
Register-ArgumentCompleter -CommandName '${e
|
|
727
|
-
|
|
728
|
-
}
|
|
729
|
-
const a = {
|
|
730
|
-
ShellCompDirectiveError: 1,
|
|
731
|
-
ShellCompDirectiveNoSpace: 2,
|
|
732
|
-
ShellCompDirectiveNoFileComp: 4,
|
|
733
|
-
ShellCompDirectiveFilterFileExt: 8,
|
|
734
|
-
ShellCompDirectiveFilterDirs: 16,
|
|
735
|
-
ShellCompDirectiveKeepOrder: 32,
|
|
736
|
-
shellCompDirectiveMaxValue: 64,
|
|
737
|
-
ShellCompDirectiveDefault: 0
|
|
738
|
-
};
|
|
739
|
-
var o = class {
|
|
740
|
-
name;
|
|
741
|
-
variadic;
|
|
742
|
-
command;
|
|
743
|
-
handler;
|
|
744
|
-
constructor(e$1, t$1, n$1, r$1 = !1) {
|
|
745
|
-
this.command = e$1, this.name = t$1, this.handler = n$1, this.variadic = r$1;
|
|
746
|
-
}
|
|
747
|
-
}, s = class {
|
|
748
|
-
value;
|
|
749
|
-
description;
|
|
750
|
-
command;
|
|
751
|
-
handler;
|
|
752
|
-
alias;
|
|
753
|
-
isBoolean;
|
|
754
|
-
constructor(e$1, t$1, n$1, r$1, i$1, a$1) {
|
|
755
|
-
this.command = e$1, this.value = t$1, this.description = n$1, this.handler = r$1, this.alias = i$1, this.isBoolean = a$1;
|
|
756
|
-
}
|
|
757
|
-
}, c = class {
|
|
758
|
-
value;
|
|
759
|
-
description;
|
|
760
|
-
options = /* @__PURE__ */ new Map();
|
|
761
|
-
arguments = /* @__PURE__ */ new Map();
|
|
762
|
-
parent;
|
|
763
|
-
constructor(e$1, t$1) {
|
|
764
|
-
this.value = e$1, this.description = t$1;
|
|
765
|
-
}
|
|
766
|
-
option(e$1, t$1, n$1, r$1) {
|
|
767
|
-
let i$1, a$1, o$1;
|
|
768
|
-
typeof n$1 == `function` ? (i$1 = n$1, a$1 = r$1, o$1 = !1) : typeof n$1 == `string` ? (i$1 = void 0, a$1 = n$1, o$1 = !0) : (i$1 = void 0, a$1 = void 0, o$1 = !0);
|
|
769
|
-
let c$1 = new s(this, e$1, t$1, i$1, a$1, o$1);
|
|
770
|
-
return this.options.set(e$1, c$1), this;
|
|
771
|
-
}
|
|
772
|
-
argument(e$1, t$1, n$1 = !1) {
|
|
773
|
-
let r$1 = new o(this, e$1, t$1, n$1);
|
|
774
|
-
return this.arguments.set(e$1, r$1), this;
|
|
775
|
-
}
|
|
776
|
-
}, l = class extends c {
|
|
777
|
-
commands = /* @__PURE__ */ new Map();
|
|
778
|
-
completions = [];
|
|
779
|
-
directive = a.ShellCompDirectiveDefault;
|
|
780
|
-
constructor() {
|
|
781
|
-
super(``, ``);
|
|
782
|
-
}
|
|
783
|
-
command(e$1, t$1) {
|
|
784
|
-
let n$1 = new c(e$1, t$1);
|
|
785
|
-
return this.commands.set(e$1, n$1), n$1;
|
|
786
|
-
}
|
|
787
|
-
stripOptions(e$1) {
|
|
788
|
-
let t$1 = [], n$1 = 0;
|
|
789
|
-
for (; n$1 < e$1.length;) {
|
|
790
|
-
let r$1 = e$1[n$1];
|
|
791
|
-
if (r$1.startsWith(`-`)) {
|
|
792
|
-
n$1++;
|
|
793
|
-
let t$2 = !1, i$1 = this.findOption(this, r$1);
|
|
794
|
-
if (i$1) t$2 = i$1.isBoolean ?? !1;
|
|
795
|
-
else for (let [, e$2] of this.commands) {
|
|
796
|
-
let n$2 = this.findOption(e$2, r$1);
|
|
797
|
-
if (n$2) {
|
|
798
|
-
t$2 = n$2.isBoolean ?? !1;
|
|
799
|
-
break;
|
|
800
|
-
}
|
|
801
|
-
}
|
|
802
|
-
!t$2 && n$1 < e$1.length && !e$1[n$1].startsWith(`-`) && n$1++;
|
|
803
|
-
} else t$1.push(r$1), n$1++;
|
|
804
|
-
}
|
|
805
|
-
return t$1;
|
|
806
|
-
}
|
|
807
|
-
matchCommand(e$1) {
|
|
808
|
-
e$1 = this.stripOptions(e$1);
|
|
809
|
-
let t$1 = [], n$1 = [], r$1 = null;
|
|
810
|
-
for (let i$1 = 0; i$1 < e$1.length; i$1++) {
|
|
811
|
-
let a$1 = e$1[i$1];
|
|
812
|
-
t$1.push(a$1);
|
|
813
|
-
let o$1 = this.commands.get(t$1.join(` `));
|
|
814
|
-
if (o$1) r$1 = o$1;
|
|
815
|
-
else {
|
|
816
|
-
n$1 = e$1.slice(i$1, e$1.length);
|
|
817
|
-
break;
|
|
818
|
-
}
|
|
819
|
-
}
|
|
820
|
-
return [r$1 || this, n$1];
|
|
821
|
-
}
|
|
822
|
-
shouldCompleteFlags(e$1, t$1) {
|
|
823
|
-
if (t$1.startsWith(`-`)) return !0;
|
|
824
|
-
if (e$1?.startsWith(`-`)) {
|
|
825
|
-
let t$2 = this.findOption(this, e$1);
|
|
826
|
-
if (!t$2) {
|
|
827
|
-
for (let [, n$1] of this.commands) if (t$2 = this.findOption(n$1, e$1), t$2) break;
|
|
828
|
-
}
|
|
829
|
-
return !(t$2 && t$2.isBoolean);
|
|
830
|
-
}
|
|
831
|
-
return !1;
|
|
832
|
-
}
|
|
833
|
-
shouldCompleteCommands(e$1) {
|
|
834
|
-
return !e$1.startsWith(`-`);
|
|
835
|
-
}
|
|
836
|
-
handleFlagCompletion(e$1, t$1, n$1, r$1) {
|
|
837
|
-
let i$1;
|
|
838
|
-
if (n$1.includes(`=`)) {
|
|
839
|
-
let [e$2] = n$1.split(`=`);
|
|
840
|
-
i$1 = e$2;
|
|
841
|
-
} else r$1?.startsWith(`-`) && (i$1 = r$1);
|
|
842
|
-
if (i$1) {
|
|
843
|
-
let t$2 = this.findOption(e$1, i$1);
|
|
844
|
-
if (t$2?.handler) {
|
|
845
|
-
let n$2 = [];
|
|
846
|
-
t$2.handler.call(t$2, (e$2, t$3) => n$2.push({
|
|
847
|
-
value: e$2,
|
|
848
|
-
description: t$3
|
|
849
|
-
}), e$1.options), this.completions = n$2;
|
|
850
|
-
}
|
|
851
|
-
return;
|
|
852
|
-
}
|
|
853
|
-
if (n$1.startsWith(`-`)) {
|
|
854
|
-
let t$2 = n$1.startsWith(`-`) && !n$1.startsWith(`--`), r$2 = n$1.replace(/^-+/, ``);
|
|
855
|
-
for (let [i$2, a$1] of e$1.options) t$2 && a$1.alias && `-${a$1.alias}`.startsWith(n$1) ? this.completions.push({
|
|
856
|
-
value: `-${a$1.alias}`,
|
|
857
|
-
description: a$1.description
|
|
858
|
-
}) : !t$2 && i$2.startsWith(r$2) && this.completions.push({
|
|
859
|
-
value: `--${i$2}`,
|
|
860
|
-
description: a$1.description
|
|
861
|
-
});
|
|
862
|
-
}
|
|
863
|
-
}
|
|
864
|
-
findOption(e$1, t$1) {
|
|
865
|
-
let n$1 = e$1.options.get(t$1);
|
|
866
|
-
if (n$1 || (n$1 = e$1.options.get(t$1.replace(/^-+/, ``)), n$1)) return n$1;
|
|
867
|
-
for (let [n$2, r$1] of e$1.options) if (r$1.alias && `-${r$1.alias}` === t$1) return r$1;
|
|
868
|
-
}
|
|
869
|
-
handleCommandCompletion(e$1, t$1) {
|
|
870
|
-
let n$1 = this.stripOptions(e$1);
|
|
871
|
-
for (let [e$2, r$1] of this.commands) {
|
|
872
|
-
if (e$2 === ``) continue;
|
|
873
|
-
let i$1 = e$2.split(` `);
|
|
874
|
-
i$1.slice(0, n$1.length).every((e$3, t$2) => e$3 === n$1[t$2]) && i$1[n$1.length]?.startsWith(t$1) && this.completions.push({
|
|
875
|
-
value: i$1[n$1.length],
|
|
876
|
-
description: r$1.description
|
|
877
|
-
});
|
|
878
|
-
}
|
|
879
|
-
}
|
|
880
|
-
handlePositionalCompletion(e$1, t$1) {
|
|
881
|
-
let n$1 = e$1.value.split(` `).length, r$1 = Math.max(0, t$1.length - n$1), i$1 = Array.from(e$1.arguments.entries());
|
|
882
|
-
if (i$1.length > 0) {
|
|
883
|
-
let t$2;
|
|
884
|
-
if (r$1 < i$1.length) {
|
|
885
|
-
let [e$2, n$2] = i$1[r$1];
|
|
886
|
-
t$2 = n$2;
|
|
887
|
-
} else {
|
|
888
|
-
let e$2 = i$1[i$1.length - 1][1];
|
|
889
|
-
e$2.variadic && (t$2 = e$2);
|
|
890
|
-
}
|
|
891
|
-
if (t$2 && t$2.handler && typeof t$2.handler == `function`) {
|
|
892
|
-
let n$2 = [];
|
|
893
|
-
t$2.handler.call(t$2, (e$2, t$3) => n$2.push({
|
|
894
|
-
value: e$2,
|
|
895
|
-
description: t$3
|
|
896
|
-
}), e$1.options), this.completions.push(...n$2);
|
|
897
|
-
}
|
|
898
|
-
}
|
|
899
|
-
}
|
|
900
|
-
complete(e$1) {
|
|
901
|
-
this.directive = a.ShellCompDirectiveNoFileComp;
|
|
902
|
-
let t$1 = /* @__PURE__ */ new Set();
|
|
903
|
-
this.completions.filter((e$2) => t$1.has(e$2.value) ? !1 : (t$1.add(e$2.value), !0)).filter((t$2) => {
|
|
904
|
-
if (e$1.includes(`=`)) {
|
|
905
|
-
let [, n$1] = e$1.split(`=`);
|
|
906
|
-
return t$2.value.startsWith(n$1);
|
|
907
|
-
}
|
|
908
|
-
return t$2.value.startsWith(e$1);
|
|
909
|
-
}).forEach((e$2) => console.log(`${e$2.value}\t${e$2.description ?? ``}`)), console.log(`:${this.directive}`);
|
|
910
|
-
}
|
|
911
|
-
parse(e$1) {
|
|
912
|
-
this.completions = [];
|
|
913
|
-
let t$1 = e$1[e$1.length - 1] === ``;
|
|
914
|
-
t$1 && e$1.pop();
|
|
915
|
-
let n$1 = e$1[e$1.length - 1] || ``, r$1 = e$1.slice(0, -1);
|
|
916
|
-
t$1 && (n$1 !== `` && r$1.push(n$1), n$1 = ``);
|
|
917
|
-
let [i$1] = this.matchCommand(r$1), a$1 = r$1[r$1.length - 1];
|
|
918
|
-
if (this.shouldCompleteFlags(a$1, n$1)) this.handleFlagCompletion(i$1, r$1, n$1, a$1);
|
|
919
|
-
else {
|
|
920
|
-
if (a$1?.startsWith(`-`) && n$1 === `` && t$1) {
|
|
921
|
-
let e$2 = this.findOption(this, a$1);
|
|
922
|
-
if (!e$2) {
|
|
923
|
-
for (let [, t$2] of this.commands) if (e$2 = this.findOption(t$2, a$1), e$2) break;
|
|
924
|
-
}
|
|
925
|
-
if (e$2 && e$2.isBoolean) {
|
|
926
|
-
this.complete(n$1);
|
|
927
|
-
return;
|
|
928
|
-
}
|
|
929
|
-
}
|
|
930
|
-
this.shouldCompleteCommands(n$1) && this.handleCommandCompletion(r$1, n$1), i$1 && i$1.arguments.size > 0 && this.handlePositionalCompletion(i$1, r$1);
|
|
931
|
-
}
|
|
932
|
-
this.complete(n$1);
|
|
933
|
-
}
|
|
934
|
-
setup(a$1, o$1, s$1) {
|
|
935
|
-
switch (e(s$1 === `zsh` || s$1 === `bash` || s$1 === `fish` || s$1 === `powershell`, `Unsupported shell`), s$1) {
|
|
936
|
-
case `zsh`: {
|
|
937
|
-
let e$1 = t(a$1, o$1);
|
|
938
|
-
console.log(e$1);
|
|
939
|
-
break;
|
|
940
|
-
}
|
|
941
|
-
case `bash`: {
|
|
942
|
-
let e$1 = n(a$1, o$1);
|
|
943
|
-
console.log(e$1);
|
|
944
|
-
break;
|
|
945
|
-
}
|
|
946
|
-
case `fish`: {
|
|
947
|
-
let e$1 = r(a$1, o$1);
|
|
948
|
-
console.log(e$1);
|
|
949
|
-
break;
|
|
950
|
-
}
|
|
951
|
-
case `powershell`: {
|
|
952
|
-
let e$1 = i(a$1, o$1);
|
|
953
|
-
console.log(e$1);
|
|
954
|
-
break;
|
|
955
|
-
}
|
|
956
|
-
}
|
|
957
|
-
}
|
|
958
|
-
};
|
|
959
|
-
const u = new l();
|
|
960
|
-
|
|
961
|
-
//#endregion
|
|
962
|
-
//#region src/t.ts
|
|
963
|
-
function registerFlag(tc, flagName, def) {
|
|
964
|
-
const normalized = normalizeFlagValue(def);
|
|
965
|
-
const desc = normalized.description ?? "";
|
|
966
|
-
if (normalized.type === Boolean) tc.option(flagName, desc, normalized.short);
|
|
967
|
-
else {
|
|
968
|
-
const handler = normalized.completions?.handler ?? (async () => {});
|
|
969
|
-
tc.option(flagName, desc, handler, normalized.short);
|
|
970
|
-
}
|
|
971
|
-
}
|
|
972
|
-
function registerGlobalFlags(globalFlags, tc) {
|
|
973
|
-
for (const [flagName, def] of Object.entries(globalFlags)) registerFlag(tc, flagName, def);
|
|
974
|
-
}
|
|
975
|
-
function buildTabModel(t$1, globalFlags, commands) {
|
|
976
|
-
registerGlobalFlags(globalFlags, t$1);
|
|
977
|
-
for (const cmd of commands.values()) {
|
|
978
|
-
if (cmd.completions?.show === false) continue;
|
|
979
|
-
let command = t$1;
|
|
980
|
-
if (cmd.name !== "") {
|
|
981
|
-
command = t$1.command(cmd.name, cmd.description ?? "");
|
|
982
|
-
registerGlobalFlags(globalFlags, command);
|
|
983
|
-
}
|
|
984
|
-
cmd.completions?.handler?.(command);
|
|
985
|
-
for (const def of cmd.parameters ?? []) {
|
|
986
|
-
const normalized = normalizeParameterValue(def);
|
|
987
|
-
const { name, isVariadic } = extractParameterInfo(normalized.key);
|
|
988
|
-
command.argument(name, normalized.completions?.handler, isVariadic);
|
|
989
|
-
}
|
|
990
|
-
for (const [flagName, def] of Object.entries(cmd.flags ?? {})) registerFlag(command, flagName, def);
|
|
991
|
-
}
|
|
992
|
-
}
|
|
993
|
-
|
|
994
|
-
//#endregion
|
|
995
|
-
//#region src/index.ts
|
|
996
|
-
const completionsPlugin = () => definePlugin({ setup: (cli) => {
|
|
997
|
-
const t$1 = new l();
|
|
998
|
-
const supportedShellEnum = Types.Enum("zsh", "bash", "fish", "powershell");
|
|
999
|
-
cli.command("completions", "Generate shell completion scripts", {
|
|
1000
|
-
flags: { shell: {
|
|
1001
|
-
description: "Shell type",
|
|
1002
|
-
type: supportedShellEnum
|
|
1003
|
-
} },
|
|
1004
|
-
parameters: [{
|
|
1005
|
-
key: "[shell]",
|
|
1006
|
-
description: "Shell type",
|
|
1007
|
-
type: supportedShellEnum
|
|
1008
|
-
}]
|
|
1009
|
-
}).on("completions", async (ctx) => {
|
|
1010
|
-
const shell = ctx.parameters.shell ?? ctx.flags.shell;
|
|
1011
|
-
if (!shell) throw new Error("Shell type is required. Please provide it via --shell flag or [shell] parameter.");
|
|
1012
|
-
buildTabModel(t$1, cli._globalFlags, cli._commands);
|
|
1013
|
-
t$1.setup(cli._scriptName, cli._scriptName, shell);
|
|
1014
|
-
});
|
|
1015
|
-
cli.command("complete", {
|
|
1016
|
-
help: { show: false },
|
|
1017
|
-
completions: { show: false },
|
|
1018
|
-
parameters: ["--", "[input...]"]
|
|
1019
|
-
}).on("complete", async (ctx) => {
|
|
1020
|
-
buildTabModel(t$1, cli._globalFlags, cli._commands);
|
|
1021
|
-
const { input } = ctx.parameters;
|
|
1022
|
-
t$1.parse(input);
|
|
1023
|
-
});
|
|
1024
|
-
return cli;
|
|
1025
|
-
} });
|
|
1026
|
-
|
|
1027
|
-
//#endregion
|
|
1028
|
-
export { completionsPlugin };
|
|
709
|
+
Register-ArgumentCompleter -CommandName '${e}' -ScriptBlock $__${n}CompleterBlock
|
|
710
|
+
`}const u={ShellCompDirectiveError:1,ShellCompDirectiveNoSpace:2,ShellCompDirectiveNoFileComp:4,ShellCompDirectiveFilterFileExt:8,ShellCompDirectiveFilterDirs:16,ShellCompDirectiveKeepOrder:32,shellCompDirectiveMaxValue:64,ShellCompDirectiveDefault:0};var d=class{name;variadic;command;handler;constructor(e,t,n,r=!1){this.command=e,this.name=t,this.handler=n,this.variadic=r}},f=class{value;description;command;handler;alias;isBoolean;constructor(e,t,n,r,i,a){this.command=e,this.value=t,this.description=n,this.handler=r,this.alias=i,this.isBoolean=a}},p=class{value;description;options=new Map;arguments=new Map;parent;constructor(e,t){this.value=e,this.description=t}option(e,t,n,r){let i,a,o;typeof n==`function`?(i=n,a=r,o=!1):typeof n==`string`?(i=void 0,a=n,o=!0):(i=void 0,a=void 0,o=!0);let s=new f(this,e,t,i,a,o);return this.options.set(e,s),this}argument(e,t,n=!1){let r=new d(this,e,t,n);return this.arguments.set(e,r),this}},m=class extends p{commands=new Map;completions=[];directive=u.ShellCompDirectiveDefault;constructor(){super(``,``)}command(e,t){let n=new p(e,t);return this.commands.set(e,n),n}stripOptions(e){let t=[],n=0;for(;n<e.length;){let r=e[n];if(r.startsWith(`-`)){n++;let t=!1,i=this.findOption(this,r);if(i)t=i.isBoolean??!1;else for(let[,e]of this.commands){let n=this.findOption(e,r);if(n){t=n.isBoolean??!1;break}}!t&&n<e.length&&!e[n].startsWith(`-`)&&n++}else t.push(r),n++}return t}matchCommand(e){e=this.stripOptions(e);let t=[],n=[],r=null;for(let i=0;i<e.length;i++){let a=e[i];t.push(a);let o=this.commands.get(t.join(` `));if(o)r=o;else{n=e.slice(i,e.length);break}}return[r||this,n]}shouldCompleteFlags(e,t){if(t.startsWith(`-`))return!0;if(e?.startsWith(`-`)){let t=this.findOption(this,e);if(!t){for(let[,n]of this.commands)if(t=this.findOption(n,e),t)break}return!(t&&t.isBoolean)}return!1}shouldCompleteCommands(e){return!e.startsWith(`-`)}handleFlagCompletion(e,t,n,r){let i;if(n.includes(`=`)){let[e]=n.split(`=`);i=e}else r?.startsWith(`-`)&&(i=r);if(i){let t=this.findOption(e,i);if(t?.handler){let n=[];t.handler.call(t,(e,t)=>n.push({value:e,description:t}),e.options),this.completions=n}return}if(n.startsWith(`-`)){let t=n.startsWith(`-`)&&!n.startsWith(`--`),r=n.replace(/^-+/,``);for(let[i,a]of e.options)t&&a.alias&&`-${a.alias}`.startsWith(n)?this.completions.push({value:`-${a.alias}`,description:a.description}):!t&&i.startsWith(r)&&this.completions.push({value:`--${i}`,description:a.description})}}findOption(e,t){let n=e.options.get(t);if(n||(n=e.options.get(t.replace(/^-+/,``)),n))return n;for(let[n,r]of e.options)if(r.alias&&`-${r.alias}`===t)return r}handleCommandCompletion(e,t){let n=this.stripOptions(e);for(let[e,r]of this.commands){if(e===``)continue;let i=e.split(` `);i.slice(0,n.length).every((e,t)=>e===n[t])&&i[n.length]?.startsWith(t)&&this.completions.push({value:i[n.length],description:r.description})}}handlePositionalCompletion(e,t){let n=e.value.split(` `).length,r=Math.max(0,t.length-n),i=Array.from(e.arguments.entries());if(i.length>0){let t;if(r<i.length){let[e,n]=i[r];t=n}else{let e=i[i.length-1][1];e.variadic&&(t=e)}if(t&&t.handler&&typeof t.handler==`function`){let n=[];t.handler.call(t,(e,t)=>n.push({value:e,description:t}),e.options),this.completions.push(...n)}}}complete(e){this.directive=u.ShellCompDirectiveNoFileComp;let t=new Set;this.completions.filter(e=>t.has(e.value)?!1:(t.add(e.value),!0)).filter(t=>{if(e.includes(`=`)){let[,n]=e.split(`=`);return t.value.startsWith(n)}return t.value.startsWith(e)}).forEach(e=>console.log(`${e.value}\t${e.description??``}`)),console.log(`:${this.directive}`)}parse(e){this.completions=[];let t=e[e.length-1]===``;t&&e.pop();let n=e[e.length-1]||``,r=e.slice(0,-1);t&&(n!==``&&r.push(n),n=``);let[i]=this.matchCommand(r),a=r[r.length-1];if(this.shouldCompleteFlags(a,n))this.handleFlagCompletion(i,r,n,a);else{if(a?.startsWith(`-`)&&n===``&&t){let e=this.findOption(this,a);if(!e){for(let[,t]of this.commands)if(e=this.findOption(t,a),e)break}if(e&&e.isBoolean){this.complete(n);return}}this.shouldCompleteCommands(n)&&this.handleCommandCompletion(r,n),i&&i.arguments.size>0&&this.handlePositionalCompletion(i,r)}this.complete(n)}setup(t,n,r){switch(e(r===`zsh`||r===`bash`||r===`fish`||r===`powershell`,`Unsupported shell`),r){case`zsh`:{let e=o(t,n);console.log(e);break}case`bash`:{let e=s(t,n);console.log(e);break}case`fish`:{let e=c(t,n);console.log(e);break}case`powershell`:{let e=l(t,n);console.log(e);break}}}};new m;function h(e,t,n){let r=i(n),a=r.description??``;if(r.type===Boolean)e.option(t,a,r.short);else{let n=r.completions?.handler??(async()=>{});e.option(t,a,n,r.short)}}function g(e,t){for(let[n,r]of Object.entries(e))h(t,n,r)}function _(e,t,n){g(t,e);for(let i of n.values()){if(i.completions?.show===!1)continue;let n=e;i.name!==``&&(n=e.command(i.name,i.description??``),g(t,n)),i.completions?.handler?.(n);for(let e of i.parameters??[]){let t=a(e),{name:i,isVariadic:o}=r(t.key);n.argument(i,t.completions?.handler,o)}for(let[e,t]of Object.entries(i.flags??{}))h(n,e,t)}}const v=()=>n({setup:e=>{let n=new m,r=t.Enum(`zsh`,`bash`,`fish`,`powershell`);return e.command(`completions`,`Generate shell completion scripts`,{flags:{shell:{description:`Shell type`,type:r}},parameters:[{key:`[shell]`,description:`Shell type`,type:r}]}).on(`completions`,async t=>{let r=t.parameters.shell??t.flags.shell;if(!r)throw Error(`Shell type is required. Please provide it via --shell flag or [shell] parameter.`);_(n,e._globalFlags,e._commands),n.setup(e._scriptName,e._scriptName,r)}),e.command(`complete`,{help:{show:!1},completions:{show:!1},parameters:[`--`,`[input...]`]}).on(`complete`,async t=>{_(n,e._globalFlags,e._commands);let{input:r}=t.parameters;n.parse(r)}),e}});export{v as completionsPlugin};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clerc/plugin-completions",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"author": "Ray <i@mk1.io> (https://github.com/so1ve)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Clerc plugin completions",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"@bomb.sh/tab": "^0.0.10",
|
|
45
|
-
"@clerc/core": "1.1.
|
|
45
|
+
"@clerc/core": "1.1.1"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
48
|
"@clerc/core": "*"
|