-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompletion-scripts_.toit
More file actions
315 lines (279 loc) · 10.4 KB
/
completion-scripts_.toit
File metadata and controls
315 lines (279 loc) · 10.4 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Copyright (C) 2026 Toit contributors.
// Use of this source code is governed by an MIT-style license that can be
// found in the package's LICENSE file.
import fs
/**
Extracts the basename from the given $path, stripping any directory components
and the .exe extension on Windows.
*/
basename_ path/string -> string:
name := fs.basename path
// Strip .exe suffix so that completions work on Windows where
// system.program-path includes the extension but users type without it.
if name.ends-with ".exe": name = name[..name.size - 4]
return name
/**
Sanitizes the given $name for use as a shell function name.
Replaces all non-alphanumeric characters with underscores and prefixes
with an underscore if the name starts with a digit.
*/
sanitize-func-name_ name/string -> string:
buffer := []
name.do --runes: | c/int |
if 'a' <= c <= 'z' or 'A' <= c <= 'Z' or '0' <= c <= '9':
buffer.add (string.from-rune c)
else:
buffer.add "_"
result := buffer.join ""
if result.size > 0 and '0' <= result[0] <= '9':
result = "_$result"
return result
/**
Returns a bash completion script for the given $program-path.
*/
bash-completion-script_ --program-path/string -> string:
program-name := basename_ program-path
func-name := sanitize-func-name_ program-name
return """
_$(func-name)_completions() {
local IFS=\$'\\n'
shopt -s extglob 2>/dev/null
local completions
completions=\$("$program-path" __complete -- "\${COMP_WORDS[@]:1:\$COMP_CWORD}")
if [ \$? -ne 0 ]; then
return
fi
local directive_line
directive_line=\$(echo "\$completions" | tail -n 1)
completions=\$(echo "\$completions" | sed '\$d')
directive_line="\${directive_line#:}"
local directive extensions=""
if [[ "\$directive_line" == *:* ]]; then
directive="\${directive_line%%:*}"
extensions="\${directive_line#*:}"
else
directive="\$directive_line"
fi
local candidates=()
while IFS='' read -r line; do
local candidate="\${line%%\$'\\t'*}"
if [ -n "\$candidate" ]; then
candidates+=("\$candidate")
fi
done <<< "\$completions"
local cur_word="\${COMP_WORDS[\$COMP_CWORD]}"
COMPREPLY=(\$(compgen -W "\${candidates[*]}" -- "\$cur_word"))
if [[ \$directive -eq 1 ]]; then
compopt +o default 2>/dev/null
elif [[ \$directive -eq 4 ]]; then
if [[ \${#COMPREPLY[@]} -eq 0 ]]; then
if [[ -n "\$extensions" ]]; then
local ext_pattern=""
IFS=',' read -ra exts <<< "\$extensions"
for ext in "\${exts[@]}"; do
ext="\${ext#.}"
if [[ -n "\$ext_pattern" ]]; then
ext_pattern="\$ext_pattern|\$ext"
else
ext_pattern="\$ext"
fi
done
COMPREPLY=(\$(compgen -f -X "!*.@(\$ext_pattern)" -- "\$cur_word"))
COMPREPLY+=(\$(compgen -d -- "\$cur_word"))
# Remove duplicate directory entries.
COMPREPLY=(\$(printf '%s\\n' "\${COMPREPLY[@]}" | sort -u))
else
compopt -o default 2>/dev/null
fi
fi
elif [[ \$directive -eq 8 ]]; then
if [[ \${#COMPREPLY[@]} -eq 0 ]]; then
compopt -o dirnames 2>/dev/null
fi
fi
}
complete -o default -F _$(func-name)_completions "$program-name\""""
/**
Returns a zsh completion script for the given $program-path.
*/
zsh-completion-script_ --program-path/string -> string:
program-name := basename_ program-path
func-name := sanitize-func-name_ program-name
return """
#compdef $program-name
_$(func-name)() {
local -a completions
local directive_line directive extensions=""
local output
output=\$("$program-path" __complete -- "\${words[@]:1:\$((CURRENT-1))}" 2>/dev/null)
if [ \$? -ne 0 ]; then
return
fi
directive_line=\$(echo "\$output" | tail -n 1)
directive_line="\${directive_line#:}"
if [[ "\$directive_line" == *:* ]]; then
directive="\${directive_line%%:*}"
extensions="\${directive_line#*:}"
else
directive="\$directive_line"
fi
local -a lines
lines=("\${(@f)\$(echo "\$output" | sed '\$d')}")
local -a candidates
for line in "\${lines[@]}"; do
if [[ "\$line" == *\$'\\t'* ]]; then
local val="\${line%%\$'\\t'*}"
local desc="\${line#*\$'\\t'}"
candidates+=("\${val}:\$desc")
else
if [ -n "\$line" ]; then
candidates+=("\$line")
fi
fi
done
if [[ \${#candidates[@]} -gt 0 ]]; then
_describe '' candidates
fi
if [[ \$directive -eq 4 ]]; then
if [[ -n "\$extensions" ]]; then
local -a ext_array
ext_array=(\${(s:,:)extensions})
local glob_parts=""
for ext in "\${ext_array[@]}"; do
if [[ -n "\$glob_parts" ]]; then
glob_parts="\$glob_parts|*\$ext"
else
glob_parts="*\$ext"
fi
done
_files -g "(\$glob_parts)"
else
_files
fi
elif [[ \$directive -eq 8 ]]; then
_directories
fi
}
compdef _$(func-name) "$program-name\""""
/**
Returns a fish completion script for the given $program-path.
*/
fish-completion-script_ --program-path/string -> string:
program-name := basename_ program-path
func-name := sanitize-func-name_ program-name
return """
function __$(func-name)_completions
set -l tokens (commandline -opc)
set -l current (commandline -ct)
set -l output ("$program-path" __complete -- \$tokens[2..] \$current 2>/dev/null)
if test \$status -ne 0
return
end
set -l directive_line (string replace -r '^:(.*)' '\$1' \$output[-1])
set -e output[-1]
set -l directive \$directive_line
set -l extensions ""
if string match -q '*:*' \$directive_line
set directive (string split ':' \$directive_line)[1]
set extensions (string split ':' \$directive_line)[2]
end
for line in \$output
set -l parts (string split \\t \$line)
if test (count \$parts) -gt 1
printf '%s\\t%s\\n' \$parts[1] \$parts[2]
else
if test -n "\$parts[1]"
echo \$parts[1]
end
end
end
if test "\$directive" = "4"
if test -n "\$extensions"
set -l cur (commandline -ct)
set -l ext_list (string split ',' \$extensions)
for f in \$cur*
if test -d "\$f"
echo \$f
continue
end
for ext in \$ext_list
if string match -q "*\$ext" \$f
echo \$f
break
end
end
end
else
__fish_complete_path (commandline -ct)
end
else if test "\$directive" = "8"
__fish_complete_directories (commandline -ct)
end
end
complete -c "$program-name" -f -a '(__$(func-name)_completions)'"""
/**
Returns a PowerShell completion script for the given $program-path.
*/
powershell-completion-script_ --program-path/string -> string:
program-name := basename_ program-path
return """
Register-ArgumentCompleter -Native -CommandName '$program-name' -ScriptBlock {
param(\$wordToComplete, \$commandAst, \$cursorPosition)
\$tokens = \$commandAst.ToString() -split '\\s+'
if (\$tokens.Length -gt 1) {
\$completionArgs = \$tokens[1..(\$tokens.Length - 1)]
} else {
\$completionArgs = @()
}
if (\$completionArgs.Length -eq 0 -or \$completionArgs[-1] -ne \$wordToComplete) {
\$completionArgs += \$wordToComplete
}
\$output = & '$program-path' __complete -- @completionArgs 2>\$null
if (\$LASTEXITCODE -ne 0 -or -not \$output) { return }
\$lines = \$output -split '\\r?\\n'
\$directiveLine = (\$lines[-1] -replace '^:', '')
\$lines = \$lines[0..(\$lines.Length - 2)]
\$directive = \$directiveLine
\$extensions = ''
if (\$directiveLine -match '^(\\d+):(.+)\$') {
\$directive = \$Matches[1]
\$extensions = \$Matches[2]
}
foreach (\$line in \$lines) {
if (-not \$line) { continue }
if (\$line -match '^([^\\t]+)\\t(.+)\$') {
\$value = \$Matches[1]
\$desc = \$Matches[2]
} else {
\$value = \$line
\$desc = \$line
}
if (\$value -notlike "\$wordToComplete*") { continue }
[System.Management.Automation.CompletionResult]::new(
\$value,
\$value,
'ParameterValue',
\$desc
)
}
if (\$directive -eq '4' -or \$directive -eq '8') {
\$completionType = if (\$directive -eq '8') { 'ProviderContainer' } else { 'ProviderItem' }
\$items = Get-ChildItem -Path "\$wordToComplete*" -ErrorAction SilentlyContinue
if (\$directive -eq '8') {
\$items = \$items | Where-Object { \$_.PSIsContainer }
} elseif (\$extensions) {
\$extArray = \$extensions -split ','
\$items = \$items | Where-Object {
\$_.PSIsContainer -or (\$extArray -contains \$_.Extension)
}
}
\$items | ForEach-Object {
[System.Management.Automation.CompletionResult]::new(
\$_.FullName,
\$_.Name,
\$completionType,
\$_.FullName
)
}
}
}"""