@shell-shock/plugin-completions 0.2.22 → 0.2.24

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.
@@ -3,7 +3,6 @@ const require_helpers_complete_command = require('../helpers/complete-command.cj
3
3
  const require_helpers_completion_directive_constants = require('../helpers/completion-directive-constants.cjs');
4
4
  let _alloy_js_core_jsx_runtime = require("@alloy-js/core/jsx-runtime");
5
5
  let _shell_shock_core_plugin_utils = require("@shell-shock/core/plugin-utils");
6
- let _stryke_path = require("@stryke/path");
7
6
  let _alloy_js_core = require("@alloy-js/core");
8
7
  let _alloy_js_typescript = require("@alloy-js/typescript");
9
8
  let _powerlines_deepkit_vendor_type = require("@powerlines/deepkit/vendor/type");
@@ -11,7 +10,7 @@ let _powerlines_plugin_alloy_core = require("@powerlines/plugin-alloy/core");
11
10
  let _powerlines_plugin_alloy_core_contexts_context = require("@powerlines/plugin-alloy/core/contexts/context");
12
11
  let _powerlines_plugin_alloy_typescript = require("@powerlines/plugin-alloy/typescript");
13
12
  let _powerlines_plugin_alloy_typescript_components_tsdoc = require("@powerlines/plugin-alloy/typescript/components/tsdoc");
14
- let _stryke_string_format_snake_case = require("@stryke/string-format/snake-case");
13
+ let _stryke_path = require("@stryke/path");
15
14
  let _stryke_string_format_camel_case = require("@stryke/string-format/camel-case");
16
15
 
17
16
  //#region src/components/powershell-command.tsx
@@ -20,6 +19,8 @@ let _stryke_string_format_camel_case = require("@stryke/string-format/camel-case
20
19
  */
21
20
  function PowerShellCompletionsCommand() {
22
21
  const context = (0, _powerlines_plugin_alloy_core_contexts_context.usePowerlines)();
22
+ const bin = (0, _alloy_js_core.computed)(() => (0, _shell_shock_core_plugin_utils.getAppBin)(context));
23
+ const name = (0, _alloy_js_core.computed)(() => (0, _stryke_string_format_camel_case.camelCase)(bin.value));
23
24
  return (0, _alloy_js_core_jsx_runtime.createComponent)(_powerlines_plugin_alloy_typescript.TypescriptFile, {
24
25
  get path() {
25
26
  return (0, _stryke_path.joinPaths)(context.entryPath, "completions", "powershell", "command.ts");
@@ -51,7 +52,7 @@ function PowerShellCompletionsCommand() {
51
52
  return _powerlines_deepkit_vendor_type.ReflectionKind.string;
52
53
  },
53
54
  get defaultValue() {
54
- return `${(0, _shell_shock_core_plugin_utils.getAppBin)(context)}-completions.ps1`;
55
+ return `${bin.value}-completions.ps1`;
55
56
  }
56
57
  })];
57
58
  }
@@ -99,20 +100,20 @@ function PowerShellCompletionsCommand() {
99
100
  name: "completions",
100
101
  type: "string",
101
102
  get initializer() {
102
- return _alloy_js_core.code`# powershell completion for ${(0, _shell_shock_core_plugin_utils.getAppTitle)(context)} -*- shell-script -*-
103
+ return _alloy_js_core.code` \`# powershell completion for ${(0, _shell_shock_core_plugin_utils.getAppTitle)(context)} -*- shell-script -*-
103
104
 
104
105
  [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
105
- function __${(0, _stryke_string_format_snake_case.snakeCase)((0, _shell_shock_core_plugin_utils.getAppBin)(context))}_debug {
106
+ function __${name.value}_debug {
106
107
  if ($env:BASH_COMP_DEBUG_FILE) {
107
108
  "$args" | Out-File -Append -FilePath "$env:BASH_COMP_DEBUG_FILE"
108
109
  }
109
110
  }
110
111
 
111
- filter __${(0, _stryke_string_format_camel_case.camelCase)((0, _shell_shock_core_plugin_utils.getAppBin)(context))}_escapeStringWithSpecialChars {
112
- $_ -replace '\\s|#|@|\\$|;|,|''|\\{|\\}|\\(|\\)|"|\\||<|>|&','\`$&'
112
+ filter __${name.value}_escapeStringWithSpecialChars {
113
+ $_ -replace '\\s|#|@|\\$|;|,|''|\\{|\\}|\\(|\\)|"|\\||<|>|&','\\\`$&'
113
114
  }
114
115
 
115
- [scriptblock]$__${(0, _stryke_string_format_camel_case.camelCase)((0, _shell_shock_core_plugin_utils.getAppBin)(context))}CompleterBlock = {
116
+ [scriptblock]$__${name.value}CompleterBlock = {
116
117
  param(
117
118
  $WordToComplete,
118
119
  $CommandAst,
@@ -123,9 +124,11 @@ function PowerShellCompletionsCommand() {
123
124
  $Command = $CommandAst.CommandElements
124
125
  $Command = "$Command"
125
126
 
126
- __${(0, _stryke_string_format_camel_case.camelCase)((0, _shell_shock_core_plugin_utils.getAppBin)(context))}_debug ""
127
- __${(0, _stryke_string_format_camel_case.camelCase)((0, _shell_shock_core_plugin_utils.getAppBin)(context))}_debug "========= starting completion logic =========="
128
- __${(0, _stryke_string_format_camel_case.camelCase)((0, _shell_shock_core_plugin_utils.getAppBin)(context))}_debug "WordToComplete: $WordToComplete Command: $Command CursorPosition: $CursorPosition"
127
+ __${name.value}_debug ""
128
+ __${name.value}_debug "========= starting completion logic =========="
129
+ __${name.value}_debug "WordToComplete: $WordToComplete Command: $Command CursorPosition: $CursorPosition"
130
+
131
+ )}_debug "WordToComplete: $WordToComplete Command: $Command CursorPosition: $CursorPosition"
129
132
 
130
133
  # The user could have moved the cursor backwards on the command-line.
131
134
  # We need to trigger completion from the $CursorPosition location, so we need
@@ -135,7 +138,7 @@ function PowerShellCompletionsCommand() {
135
138
  if ($Command.Length -gt $CursorPosition) {
136
139
  $Command = $Command.Substring(0, $CursorPosition)
137
140
  }
138
- __${(0, _stryke_string_format_camel_case.camelCase)((0, _shell_shock_core_plugin_utils.getAppBin)(context))}_debug "Truncated command: $Command"
141
+ __${name.value}_debug "Truncated command: $Command"
139
142
 
140
143
  $ShellCompDirectiveError=${require_helpers_completion_directive_constants.CompletionDirective.CompletionDirectiveError}
141
144
  $ShellCompDirectiveNoSpace=${require_helpers_completion_directive_constants.CompletionDirective.CompletionDirectiveNoSpace}
@@ -149,10 +152,10 @@ function PowerShellCompletionsCommand() {
149
152
  $Program, $Arguments = $Command.Split(" ", 2)
150
153
 
151
154
  $QuotedArgs = ($Arguments -split ' ' | ForEach-Object { "'" + ($_ -replace "'", "''") + "'" }) -join ' '
152
- __${(0, _stryke_string_format_camel_case.camelCase)((0, _shell_shock_core_plugin_utils.getAppBin)(context))}_debug "QuotedArgs: $QuotedArgs"
155
+ __${name.value}_debug "QuotedArgs: $QuotedArgs"
153
156
 
154
- $RequestComp = "& ${require_helpers_complete_command.exec} complete '--' $QuotedArgs"
155
- __${(0, _stryke_string_format_camel_case.camelCase)((0, _shell_shock_core_plugin_utils.getAppBin)(context))}_debug "RequestComp: $RequestComp"
157
+ $RequestComp = "& ${require_helpers_complete_command.EXEC_COMMAND} complete '--' $QuotedArgs"
158
+ __${name.value}_debug "RequestComp: $RequestComp"
156
159
 
157
160
  # we cannot use $WordToComplete because it
158
161
  # has the wrong values if the cursor was moved
@@ -160,13 +163,13 @@ function PowerShellCompletionsCommand() {
160
163
  if ($WordToComplete -ne "" ) {
161
164
  $WordToComplete = $Arguments.Split(" ")[-1]
162
165
  }
163
- __${(0, _stryke_string_format_camel_case.camelCase)((0, _shell_shock_core_plugin_utils.getAppBin)(context))}_debug "New WordToComplete: $WordToComplete"
166
+ __${name.value}_debug "New WordToComplete: $WordToComplete"
164
167
 
165
168
 
166
169
  # Check for flag with equal sign
167
170
  $IsEqualFlag = ($WordToComplete -Like "--*=*" )
168
171
  if ( $IsEqualFlag ) {
169
- __${(0, _stryke_string_format_camel_case.camelCase)((0, _shell_shock_core_plugin_utils.getAppBin)(context))}_debug "Completing equal sign flag"
172
+ __${name.value}_debug "Completing equal sign flag"
170
173
  # Remove the flag part
171
174
  $Flag, $WordToComplete = $WordToComplete.Split("=", 2)
172
175
  }
@@ -174,20 +177,20 @@ function PowerShellCompletionsCommand() {
174
177
  if ( $WordToComplete -eq "" -And ( -Not $IsEqualFlag )) {
175
178
  # If the last parameter is complete (there is a space following it)
176
179
  # We add an extra empty parameter so we can indicate this to the go method.
177
- __${(0, _stryke_string_format_camel_case.camelCase)((0, _shell_shock_core_plugin_utils.getAppBin)(context))}_debug "Adding extra empty parameter"
180
+ __${name.value}_debug "Adding extra empty parameter"
178
181
  # PowerShell 7.2+ changed the way how the arguments are passed to executables,
179
182
  # so for pre-7.2 or when Legacy argument passing is enabled we need to use
180
183
  if ($PSVersionTable.PsVersion -lt [version]'7.2.0' -or
181
184
  ($PSVersionTable.PsVersion -lt [version]'7.3.0' -and -not [ExperimentalFeature]::IsEnabled("PSNativeCommandArgumentPassing")) -or
182
185
  (($PSVersionTable.PsVersion -ge [version]'7.3.0' -or [ExperimentalFeature]::IsEnabled("PSNativeCommandArgumentPassing")) -and
183
186
  $PSNativeCommandArgumentPassing -eq 'Legacy')) {
184
- $RequestComp="$RequestComp" + ' \`"\`"'
187
+ $RequestComp="$RequestComp" + ' \\\`"\\\`"'
185
188
  } else {
186
189
  $RequestComp = "$RequestComp" + ' ""'
187
190
  }
188
191
  }
189
192
 
190
- __${(0, _stryke_string_format_camel_case.camelCase)((0, _shell_shock_core_plugin_utils.getAppBin)(context))}_debug "Calling $RequestComp"
193
+ __${name.value}_debug "Calling $RequestComp"
191
194
  # First disable ActiveHelp which is not supported for Powershell
192
195
  $env:ActiveHelp = 0
193
196
 
@@ -201,22 +204,26 @@ function PowerShellCompletionsCommand() {
201
204
  # There is no directive specified
202
205
  $Directive = 0
203
206
  }
204
- __${(0, _stryke_string_format_camel_case.camelCase)((0, _shell_shock_core_plugin_utils.getAppBin)(context))}_debug "The completion directive is: $Directive"
207
+ __${name.value}_debug "The completion directive is: $Directive"
205
208
 
206
209
  # remove directive (last element) from out
207
210
  $Out = $Out | Where-Object { $_ -ne $Out[-1] }
208
- __${(0, _stryke_string_format_camel_case.camelCase)((0, _shell_shock_core_plugin_utils.getAppBin)(context))}_debug "The completions are: $Out"
211
+ __${name.value}_debug "The completions are: $Out"
209
212
  if (($Directive -band $ShellCompDirectiveError) -ne 0 ) {
210
213
  # Error code. No completion.
211
- __${(0, _stryke_string_format_camel_case.camelCase)((0, _shell_shock_core_plugin_utils.getAppBin)(context))}_debug "Received error from custom completion go code"
214
+ __${name.value}_debug "Received error from custom completion go code"
215
+ return
216
+ }
217
+
218
+ ${bin.value}_debug "Received error from custom completion go code"
212
219
  return
213
220
  }
214
221
 
215
222
  $Longest = 0
216
223
  [Array]$Values = $Out | ForEach-Object {
217
224
  # Split the output in name and description
218
- $Name, $Description = $_.Split("\`t", 2)
219
- __${(0, _stryke_string_format_camel_case.camelCase)((0, _shell_shock_core_plugin_utils.getAppBin)(context))}_debug "Name: $Name Description: $Description"
225
+ $Name, $Description = $_.Split("\\\`t", 2)
226
+ __${name.value}_debug "Name: $Name Description: $Description"
220
227
 
221
228
  # Look for the longest completion so that we can format things nicely
222
229
  if ($Longest -lt $Name.Length) {
@@ -235,13 +242,13 @@ function PowerShellCompletionsCommand() {
235
242
  $Space = " "
236
243
  if (($Directive -band $ShellCompDirectiveNoSpace) -ne 0 ) {
237
244
  # remove the space here
238
- __${(0, _stryke_string_format_camel_case.camelCase)((0, _shell_shock_core_plugin_utils.getAppBin)(context))}_debug "ShellCompDirectiveNoSpace is called"
245
+ __${name.value}_debug "ShellCompDirectiveNoSpace is called"
239
246
  $Space = ""
240
247
  }
241
248
 
242
249
  if ((($Directive -band $ShellCompDirectiveFilterFileExt) -ne 0 ) -or
243
250
  (($Directive -band $ShellCompDirectiveFilterDirs) -ne 0 )) {
244
- __${(0, _stryke_string_format_camel_case.camelCase)((0, _shell_shock_core_plugin_utils.getAppBin)(context))}_debug "ShellCompDirectiveFilterFileExt ShellCompDirectiveFilterDirs are not supported"
251
+ __${name.value}_debug "ShellCompDirectiveFilterFileExt ShellCompDirectiveFilterDirs are not supported"
245
252
 
246
253
  # return here to prevent the completion of the extensions
247
254
  return
@@ -253,7 +260,7 @@ function PowerShellCompletionsCommand() {
253
260
 
254
261
  # Join the flag back if we have an equal sign flag
255
262
  if ( $IsEqualFlag ) {
256
- __${(0, _stryke_string_format_camel_case.camelCase)((0, _shell_shock_core_plugin_utils.getAppBin)(context))}_debug "Join the equal sign flag back to the completion value"
263
+ __${name.value}_debug "Join the equal sign flag back to the completion value"
257
264
  $_.Name = $Flag + "=" + $_.Name
258
265
  }
259
266
  }
@@ -264,7 +271,7 @@ function PowerShellCompletionsCommand() {
264
271
  }
265
272
 
266
273
  if (($Directive -band $ShellCompDirectiveNoFileComp) -ne 0 ) {
267
- __${(0, _stryke_string_format_camel_case.camelCase)((0, _shell_shock_core_plugin_utils.getAppBin)(context))}_debug "ShellCompDirectiveNoFileComp is called"
274
+ __${name.value}_debug "ShellCompDirectiveNoFileComp is called"
268
275
 
269
276
  if ($Values.Length -eq 0) {
270
277
  # Just print an empty string here so the
@@ -278,7 +285,7 @@ function PowerShellCompletionsCommand() {
278
285
 
279
286
  # Get the current mode
280
287
  $Mode = (Get-PSReadLineKeyHandler | Where-Object { $_.Key -eq "Tab" }).Function
281
- __${(0, _stryke_string_format_camel_case.camelCase)((0, _shell_shock_core_plugin_utils.getAppBin)(context))}_debug "Mode: $Mode"
288
+ __${name.value}_debug "Mode: $Mode"
282
289
 
283
290
  $Values | ForEach-Object {
284
291
 
@@ -303,10 +310,10 @@ function PowerShellCompletionsCommand() {
303
310
  "Complete" {
304
311
 
305
312
  if ($Values.Length -eq 1) {
306
- __${(0, _stryke_string_format_camel_case.camelCase)((0, _shell_shock_core_plugin_utils.getAppBin)(context))}_debug "Only one completion left"
313
+ __${name.value}_debug "Only one completion left"
307
314
 
308
315
  # insert space after value
309
- [System.Management.Automation.CompletionResult]::new($($comp.Name | __${name}_escapeStringWithSpecialChars) + $Space, "$($comp.Name)", 'ParameterValue', "$($comp.Description)")
316
+ [System.Management.Automation.CompletionResult]::new($($comp.Name | __${name.value}_escapeStringWithSpecialChars) + $Space, "$($comp.Name)", 'ParameterValue', "$($comp.Description)")
310
317
 
311
318
  } else {
312
319
  # Add the proper number of spaces to align the descriptions
@@ -330,7 +337,7 @@ function PowerShellCompletionsCommand() {
330
337
  # insert space after value
331
338
  # MenuComplete will automatically show the ToolTip of
332
339
  # the highlighted value at the bottom of the suggestions.
333
- [System.Management.Automation.CompletionResult]::new($($comp.Name | __${name}_escapeStringWithSpecialChars) + $Space, "$($comp.Name)", 'ParameterValue', "$($comp.Description)")
340
+ [System.Management.Automation.CompletionResult]::new($($comp.Name | __${name.value}_escapeStringWithSpecialChars) + $Space, "$($comp.Name)", 'ParameterValue', "$($comp.Description)")
334
341
  }
335
342
 
336
343
  # TabCompleteNext and in case we get something unknown
@@ -338,15 +345,14 @@ function PowerShellCompletionsCommand() {
338
345
  # Like MenuComplete but we don't want to add a space here because
339
346
  # the user need to press space anyway to get the completion.
340
347
  # Description will not be shown because that's not possible with TabCompleteNext
341
- [System.Management.Automation.CompletionResult]::new($($comp.Name | __${name}_escapeStringWithSpecialChars), "$($comp.Name)", 'ParameterValue', "$($comp.Description)")
348
+ [System.Management.Automation.CompletionResult]::new($($comp.Name | __${name.value}_escapeStringWithSpecialChars), "$($comp.Name)", 'ParameterValue', "$($comp.Description)")
342
349
  }
343
350
  }
344
-
345
351
  }
346
352
  }
347
353
 
348
- Register-ArgumentCompleter -CommandName '${(0, _shell_shock_core_plugin_utils.getAppBin)(context)}' -ScriptBlock $__${(0, _stryke_string_format_camel_case.camelCase)((0, _shell_shock_core_plugin_utils.getAppBin)(context))}CompleterBlock
349
- \`); `;
354
+ Register-ArgumentCompleter -CommandName '${bin.value}' -ScriptBlock $__${name.value}CompleterBlock
355
+ \`; `;
350
356
  }
351
357
  }),
352
358
  (0, _alloy_js_core_jsx_runtime.createComponent)(_powerlines_plugin_alloy_core.Spacing, {}),
@@ -376,7 +382,7 @@ Register-ArgumentCompleter -CommandName '${(0, _shell_shock_core_plugin_utils.ge
376
382
  (0, _alloy_js_core_jsx_runtime.memo)(() => _alloy_js_core.code`try {
377
383
  configFileContent = await readFile(configFilePath, "utf8");
378
384
  } catch (error) {
379
- if (error.code === "ENOENT") {
385
+ if (Error.isError(error) && error.code === "ENOENT") {
380
386
  // If the file doesn't exist, we can create it later when writing the completion script.
381
387
  warn(\`Configuration file \${colors.bold(configFilePath)} does not exist. It will be created when the completion script is written.\`);
382
388
  } else {
@@ -394,7 +400,7 @@ Register-ArgumentCompleter -CommandName '${(0, _shell_shock_core_plugin_utils.ge
394
400
  (0, _alloy_js_core_jsx_runtime.createComponent)(_alloy_js_typescript.IfStatement, {
395
401
  condition: _alloy_js_core.code`options.script`,
396
402
  get children() {
397
- return _alloy_js_core.code`const outputPath = options.script === true ? "${(0, _shell_shock_core_plugin_utils.getAppBin)(context)}-completions.powershell" : options.script;
403
+ return _alloy_js_core.code`const outputPath = options.script === true ? "${bin.value}-completions.powershell" : options.script;
398
404
  await writeFile(outputPath, stripAnsi(completions));
399
405
 
400
406
  success(\`${(0, _shell_shock_core_plugin_utils.getAppTitle)(context)} PowerShell completion script has been generated at \${colors.bold(outputPath)}.\`);`;
@@ -1 +1 @@
1
- {"version":3,"file":"powershell-command.d.cts","names":[],"sources":["../../src/components/powershell-command.tsx"],"mappings":";;;;;;iBAgDgB,4BAAA,CAAA,GAA4B,iBAAA,CAAA,QAAA"}
1
+ {"version":3,"file":"powershell-command.d.cts","names":[],"sources":["../../src/components/powershell-command.tsx"],"mappings":";;;;;;iBA+CgB,4BAAA,CAAA,GAA4B,iBAAA,CAAA,QAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"powershell-command.d.mts","names":[],"sources":["../../src/components/powershell-command.tsx"],"mappings":";;;;;;iBAgDgB,4BAAA,CAAA,GAA4B,iBAAA,CAAA,QAAA"}
1
+ {"version":3,"file":"powershell-command.d.mts","names":[],"sources":["../../src/components/powershell-command.tsx"],"mappings":";;;;;;iBA+CgB,4BAAA,CAAA,GAA4B,iBAAA,CAAA,QAAA"}
@@ -1,16 +1,15 @@
1
- import { exec } from "../helpers/complete-command.mjs";
1
+ import { EXEC_COMMAND } from "../helpers/complete-command.mjs";
2
2
  import { CompletionDirective } from "../helpers/completion-directive-constants.mjs";
3
3
  import { createComponent, memo } from "@alloy-js/core/jsx-runtime";
4
4
  import { getAppBin, getAppTitle } from "@shell-shock/core/plugin-utils";
5
- import { joinPaths } from "@stryke/path";
6
- import { code } from "@alloy-js/core";
5
+ import { code, computed } from "@alloy-js/core";
7
6
  import { FunctionDeclaration, IfStatement, InterfaceDeclaration, VarDeclaration } from "@alloy-js/typescript";
8
7
  import { ReflectionKind } from "@powerlines/deepkit/vendor/type";
9
8
  import { Spacing } from "@powerlines/plugin-alloy/core";
10
9
  import { usePowerlines } from "@powerlines/plugin-alloy/core/contexts/context";
11
10
  import { InterfaceMember, TypescriptFile } from "@powerlines/plugin-alloy/typescript";
12
11
  import { TSDoc, TSDocDefaultValue, TSDocRemarks } from "@powerlines/plugin-alloy/typescript/components/tsdoc";
13
- import { snakeCase } from "@stryke/string-format/snake-case";
12
+ import { joinPaths } from "@stryke/path";
14
13
  import { camelCase } from "@stryke/string-format/camel-case";
15
14
 
16
15
  //#region src/components/powershell-command.tsx
@@ -19,6 +18,8 @@ import { camelCase } from "@stryke/string-format/camel-case";
19
18
  */
20
19
  function PowerShellCompletionsCommand() {
21
20
  const context = usePowerlines();
21
+ const bin = computed(() => getAppBin(context));
22
+ const name = computed(() => camelCase(bin.value));
22
23
  return createComponent(TypescriptFile, {
23
24
  get path() {
24
25
  return joinPaths(context.entryPath, "completions", "powershell", "command.ts");
@@ -50,7 +51,7 @@ function PowerShellCompletionsCommand() {
50
51
  return ReflectionKind.string;
51
52
  },
52
53
  get defaultValue() {
53
- return `${getAppBin(context)}-completions.ps1`;
54
+ return `${bin.value}-completions.ps1`;
54
55
  }
55
56
  })];
56
57
  }
@@ -98,20 +99,20 @@ function PowerShellCompletionsCommand() {
98
99
  name: "completions",
99
100
  type: "string",
100
101
  get initializer() {
101
- return code`# powershell completion for ${getAppTitle(context)} -*- shell-script -*-
102
+ return code` \`# powershell completion for ${getAppTitle(context)} -*- shell-script -*-
102
103
 
103
104
  [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
104
- function __${snakeCase(getAppBin(context))}_debug {
105
+ function __${name.value}_debug {
105
106
  if ($env:BASH_COMP_DEBUG_FILE) {
106
107
  "$args" | Out-File -Append -FilePath "$env:BASH_COMP_DEBUG_FILE"
107
108
  }
108
109
  }
109
110
 
110
- filter __${camelCase(getAppBin(context))}_escapeStringWithSpecialChars {
111
- $_ -replace '\\s|#|@|\\$|;|,|''|\\{|\\}|\\(|\\)|"|\\||<|>|&','\`$&'
111
+ filter __${name.value}_escapeStringWithSpecialChars {
112
+ $_ -replace '\\s|#|@|\\$|;|,|''|\\{|\\}|\\(|\\)|"|\\||<|>|&','\\\`$&'
112
113
  }
113
114
 
114
- [scriptblock]$__${camelCase(getAppBin(context))}CompleterBlock = {
115
+ [scriptblock]$__${name.value}CompleterBlock = {
115
116
  param(
116
117
  $WordToComplete,
117
118
  $CommandAst,
@@ -122,9 +123,11 @@ function PowerShellCompletionsCommand() {
122
123
  $Command = $CommandAst.CommandElements
123
124
  $Command = "$Command"
124
125
 
125
- __${camelCase(getAppBin(context))}_debug ""
126
- __${camelCase(getAppBin(context))}_debug "========= starting completion logic =========="
127
- __${camelCase(getAppBin(context))}_debug "WordToComplete: $WordToComplete Command: $Command CursorPosition: $CursorPosition"
126
+ __${name.value}_debug ""
127
+ __${name.value}_debug "========= starting completion logic =========="
128
+ __${name.value}_debug "WordToComplete: $WordToComplete Command: $Command CursorPosition: $CursorPosition"
129
+
130
+ )}_debug "WordToComplete: $WordToComplete Command: $Command CursorPosition: $CursorPosition"
128
131
 
129
132
  # The user could have moved the cursor backwards on the command-line.
130
133
  # We need to trigger completion from the $CursorPosition location, so we need
@@ -134,7 +137,7 @@ function PowerShellCompletionsCommand() {
134
137
  if ($Command.Length -gt $CursorPosition) {
135
138
  $Command = $Command.Substring(0, $CursorPosition)
136
139
  }
137
- __${camelCase(getAppBin(context))}_debug "Truncated command: $Command"
140
+ __${name.value}_debug "Truncated command: $Command"
138
141
 
139
142
  $ShellCompDirectiveError=${CompletionDirective.CompletionDirectiveError}
140
143
  $ShellCompDirectiveNoSpace=${CompletionDirective.CompletionDirectiveNoSpace}
@@ -148,10 +151,10 @@ function PowerShellCompletionsCommand() {
148
151
  $Program, $Arguments = $Command.Split(" ", 2)
149
152
 
150
153
  $QuotedArgs = ($Arguments -split ' ' | ForEach-Object { "'" + ($_ -replace "'", "''") + "'" }) -join ' '
151
- __${camelCase(getAppBin(context))}_debug "QuotedArgs: $QuotedArgs"
154
+ __${name.value}_debug "QuotedArgs: $QuotedArgs"
152
155
 
153
- $RequestComp = "& ${exec} complete '--' $QuotedArgs"
154
- __${camelCase(getAppBin(context))}_debug "RequestComp: $RequestComp"
156
+ $RequestComp = "& ${EXEC_COMMAND} complete '--' $QuotedArgs"
157
+ __${name.value}_debug "RequestComp: $RequestComp"
155
158
 
156
159
  # we cannot use $WordToComplete because it
157
160
  # has the wrong values if the cursor was moved
@@ -159,13 +162,13 @@ function PowerShellCompletionsCommand() {
159
162
  if ($WordToComplete -ne "" ) {
160
163
  $WordToComplete = $Arguments.Split(" ")[-1]
161
164
  }
162
- __${camelCase(getAppBin(context))}_debug "New WordToComplete: $WordToComplete"
165
+ __${name.value}_debug "New WordToComplete: $WordToComplete"
163
166
 
164
167
 
165
168
  # Check for flag with equal sign
166
169
  $IsEqualFlag = ($WordToComplete -Like "--*=*" )
167
170
  if ( $IsEqualFlag ) {
168
- __${camelCase(getAppBin(context))}_debug "Completing equal sign flag"
171
+ __${name.value}_debug "Completing equal sign flag"
169
172
  # Remove the flag part
170
173
  $Flag, $WordToComplete = $WordToComplete.Split("=", 2)
171
174
  }
@@ -173,20 +176,20 @@ function PowerShellCompletionsCommand() {
173
176
  if ( $WordToComplete -eq "" -And ( -Not $IsEqualFlag )) {
174
177
  # If the last parameter is complete (there is a space following it)
175
178
  # We add an extra empty parameter so we can indicate this to the go method.
176
- __${camelCase(getAppBin(context))}_debug "Adding extra empty parameter"
179
+ __${name.value}_debug "Adding extra empty parameter"
177
180
  # PowerShell 7.2+ changed the way how the arguments are passed to executables,
178
181
  # so for pre-7.2 or when Legacy argument passing is enabled we need to use
179
182
  if ($PSVersionTable.PsVersion -lt [version]'7.2.0' -or
180
183
  ($PSVersionTable.PsVersion -lt [version]'7.3.0' -and -not [ExperimentalFeature]::IsEnabled("PSNativeCommandArgumentPassing")) -or
181
184
  (($PSVersionTable.PsVersion -ge [version]'7.3.0' -or [ExperimentalFeature]::IsEnabled("PSNativeCommandArgumentPassing")) -and
182
185
  $PSNativeCommandArgumentPassing -eq 'Legacy')) {
183
- $RequestComp="$RequestComp" + ' \`"\`"'
186
+ $RequestComp="$RequestComp" + ' \\\`"\\\`"'
184
187
  } else {
185
188
  $RequestComp = "$RequestComp" + ' ""'
186
189
  }
187
190
  }
188
191
 
189
- __${camelCase(getAppBin(context))}_debug "Calling $RequestComp"
192
+ __${name.value}_debug "Calling $RequestComp"
190
193
  # First disable ActiveHelp which is not supported for Powershell
191
194
  $env:ActiveHelp = 0
192
195
 
@@ -200,22 +203,26 @@ function PowerShellCompletionsCommand() {
200
203
  # There is no directive specified
201
204
  $Directive = 0
202
205
  }
203
- __${camelCase(getAppBin(context))}_debug "The completion directive is: $Directive"
206
+ __${name.value}_debug "The completion directive is: $Directive"
204
207
 
205
208
  # remove directive (last element) from out
206
209
  $Out = $Out | Where-Object { $_ -ne $Out[-1] }
207
- __${camelCase(getAppBin(context))}_debug "The completions are: $Out"
210
+ __${name.value}_debug "The completions are: $Out"
208
211
  if (($Directive -band $ShellCompDirectiveError) -ne 0 ) {
209
212
  # Error code. No completion.
210
- __${camelCase(getAppBin(context))}_debug "Received error from custom completion go code"
213
+ __${name.value}_debug "Received error from custom completion go code"
214
+ return
215
+ }
216
+
217
+ ${bin.value}_debug "Received error from custom completion go code"
211
218
  return
212
219
  }
213
220
 
214
221
  $Longest = 0
215
222
  [Array]$Values = $Out | ForEach-Object {
216
223
  # Split the output in name and description
217
- $Name, $Description = $_.Split("\`t", 2)
218
- __${camelCase(getAppBin(context))}_debug "Name: $Name Description: $Description"
224
+ $Name, $Description = $_.Split("\\\`t", 2)
225
+ __${name.value}_debug "Name: $Name Description: $Description"
219
226
 
220
227
  # Look for the longest completion so that we can format things nicely
221
228
  if ($Longest -lt $Name.Length) {
@@ -234,13 +241,13 @@ function PowerShellCompletionsCommand() {
234
241
  $Space = " "
235
242
  if (($Directive -band $ShellCompDirectiveNoSpace) -ne 0 ) {
236
243
  # remove the space here
237
- __${camelCase(getAppBin(context))}_debug "ShellCompDirectiveNoSpace is called"
244
+ __${name.value}_debug "ShellCompDirectiveNoSpace is called"
238
245
  $Space = ""
239
246
  }
240
247
 
241
248
  if ((($Directive -band $ShellCompDirectiveFilterFileExt) -ne 0 ) -or
242
249
  (($Directive -band $ShellCompDirectiveFilterDirs) -ne 0 )) {
243
- __${camelCase(getAppBin(context))}_debug "ShellCompDirectiveFilterFileExt ShellCompDirectiveFilterDirs are not supported"
250
+ __${name.value}_debug "ShellCompDirectiveFilterFileExt ShellCompDirectiveFilterDirs are not supported"
244
251
 
245
252
  # return here to prevent the completion of the extensions
246
253
  return
@@ -252,7 +259,7 @@ function PowerShellCompletionsCommand() {
252
259
 
253
260
  # Join the flag back if we have an equal sign flag
254
261
  if ( $IsEqualFlag ) {
255
- __${camelCase(getAppBin(context))}_debug "Join the equal sign flag back to the completion value"
262
+ __${name.value}_debug "Join the equal sign flag back to the completion value"
256
263
  $_.Name = $Flag + "=" + $_.Name
257
264
  }
258
265
  }
@@ -263,7 +270,7 @@ function PowerShellCompletionsCommand() {
263
270
  }
264
271
 
265
272
  if (($Directive -band $ShellCompDirectiveNoFileComp) -ne 0 ) {
266
- __${camelCase(getAppBin(context))}_debug "ShellCompDirectiveNoFileComp is called"
273
+ __${name.value}_debug "ShellCompDirectiveNoFileComp is called"
267
274
 
268
275
  if ($Values.Length -eq 0) {
269
276
  # Just print an empty string here so the
@@ -277,7 +284,7 @@ function PowerShellCompletionsCommand() {
277
284
 
278
285
  # Get the current mode
279
286
  $Mode = (Get-PSReadLineKeyHandler | Where-Object { $_.Key -eq "Tab" }).Function
280
- __${camelCase(getAppBin(context))}_debug "Mode: $Mode"
287
+ __${name.value}_debug "Mode: $Mode"
281
288
 
282
289
  $Values | ForEach-Object {
283
290
 
@@ -302,10 +309,10 @@ function PowerShellCompletionsCommand() {
302
309
  "Complete" {
303
310
 
304
311
  if ($Values.Length -eq 1) {
305
- __${camelCase(getAppBin(context))}_debug "Only one completion left"
312
+ __${name.value}_debug "Only one completion left"
306
313
 
307
314
  # insert space after value
308
- [System.Management.Automation.CompletionResult]::new($($comp.Name | __${name}_escapeStringWithSpecialChars) + $Space, "$($comp.Name)", 'ParameterValue', "$($comp.Description)")
315
+ [System.Management.Automation.CompletionResult]::new($($comp.Name | __${name.value}_escapeStringWithSpecialChars) + $Space, "$($comp.Name)", 'ParameterValue', "$($comp.Description)")
309
316
 
310
317
  } else {
311
318
  # Add the proper number of spaces to align the descriptions
@@ -329,7 +336,7 @@ function PowerShellCompletionsCommand() {
329
336
  # insert space after value
330
337
  # MenuComplete will automatically show the ToolTip of
331
338
  # the highlighted value at the bottom of the suggestions.
332
- [System.Management.Automation.CompletionResult]::new($($comp.Name | __${name}_escapeStringWithSpecialChars) + $Space, "$($comp.Name)", 'ParameterValue', "$($comp.Description)")
339
+ [System.Management.Automation.CompletionResult]::new($($comp.Name | __${name.value}_escapeStringWithSpecialChars) + $Space, "$($comp.Name)", 'ParameterValue', "$($comp.Description)")
333
340
  }
334
341
 
335
342
  # TabCompleteNext and in case we get something unknown
@@ -337,15 +344,14 @@ function PowerShellCompletionsCommand() {
337
344
  # Like MenuComplete but we don't want to add a space here because
338
345
  # the user need to press space anyway to get the completion.
339
346
  # Description will not be shown because that's not possible with TabCompleteNext
340
- [System.Management.Automation.CompletionResult]::new($($comp.Name | __${name}_escapeStringWithSpecialChars), "$($comp.Name)", 'ParameterValue', "$($comp.Description)")
347
+ [System.Management.Automation.CompletionResult]::new($($comp.Name | __${name.value}_escapeStringWithSpecialChars), "$($comp.Name)", 'ParameterValue', "$($comp.Description)")
341
348
  }
342
349
  }
343
-
344
350
  }
345
351
  }
346
352
 
347
- Register-ArgumentCompleter -CommandName '${getAppBin(context)}' -ScriptBlock $__${camelCase(getAppBin(context))}CompleterBlock
348
- \`); `;
353
+ Register-ArgumentCompleter -CommandName '${bin.value}' -ScriptBlock $__${name.value}CompleterBlock
354
+ \`; `;
349
355
  }
350
356
  }),
351
357
  createComponent(Spacing, {}),
@@ -375,7 +381,7 @@ Register-ArgumentCompleter -CommandName '${getAppBin(context)}' -ScriptBlock $__
375
381
  memo(() => code`try {
376
382
  configFileContent = await readFile(configFilePath, "utf8");
377
383
  } catch (error) {
378
- if (error.code === "ENOENT") {
384
+ if (Error.isError(error) && error.code === "ENOENT") {
379
385
  // If the file doesn't exist, we can create it later when writing the completion script.
380
386
  warn(\`Configuration file \${colors.bold(configFilePath)} does not exist. It will be created when the completion script is written.\`);
381
387
  } else {
@@ -393,7 +399,7 @@ Register-ArgumentCompleter -CommandName '${getAppBin(context)}' -ScriptBlock $__
393
399
  createComponent(IfStatement, {
394
400
  condition: code`options.script`,
395
401
  get children() {
396
- return code`const outputPath = options.script === true ? "${getAppBin(context)}-completions.powershell" : options.script;
402
+ return code`const outputPath = options.script === true ? "${bin.value}-completions.powershell" : options.script;
397
403
  await writeFile(outputPath, stripAnsi(completions));
398
404
 
399
405
  success(\`${getAppTitle(context)} PowerShell completion script has been generated at \${colors.bold(outputPath)}.\`);`;