@@ -57,7 +57,7 @@ export function generate(
5757 # Make sure the $Command is longer then the $CursorPosition before we truncate.
5858 # This happens because the $Command does not include the last space.
5959 if ($Command.Length -gt $CursorPosition) {
60- $Command= $Command.Substring(0,$CursorPosition)
60+ $Command = $Command.Substring(0, $CursorPosition)
6161 }
6262 __${ name } _debug "Truncated command: $Command"
6363
@@ -68,115 +68,109 @@ export function generate(
6868 $ShellCompDirectiveFilterDirs=${ ShellCompDirectiveFilterDirs }
6969 $ShellCompDirectiveKeepOrder=${ ShellCompDirectiveKeepOrder }
7070
71- # Original exec value
72- $originalExec = "${ exec } "
73-
74- # Split the exec string by spaces to handle each part
75- $execParts = $originalExec -split ' '
76-
77- __${ name } _debug "Value of exec: ${ exec } "
78-
7971 # Prepare the command to request completions for the program.
8072 # Split the command at the first space to separate the program and arguments.
81- $Program,$Arguments = $Command.Split(" ",2)
73+ $Program, $Arguments = $Command.Split(" ", 2)
8274
8375 $RequestComp = "& ${ exec } complete -- $Arguments"
8476 __${ name } _debug "RequestComp: $RequestComp"
8577
86- # We cannot use $WordToComplete because it
78+ # we cannot use $WordToComplete because it
8779 # has the wrong values if the cursor was moved
8880 # so use the last argument
8981 if ($WordToComplete -ne "" ) {
9082 $WordToComplete = $Arguments.Split(" ")[-1]
9183 }
9284 __${ name } _debug "New WordToComplete: $WordToComplete"
9385
86+
9487 # Check for flag with equal sign
9588 $IsEqualFlag = ($WordToComplete -Like "--*=*" )
9689 if ( $IsEqualFlag ) {
9790 __${ name } _debug "Completing equal sign flag"
9891 # Remove the flag part
99- $Flag,$WordToComplete = $WordToComplete.Split("=",2)
92+ $Flag, $WordToComplete = $WordToComplete.Split("=", 2)
10093 }
10194
10295 if ( $WordToComplete -eq "" -And ( -Not $IsEqualFlag )) {
10396 # If the last parameter is complete (there is a space following it)
104- # We add an extra empty parameter so we can indicate this to the program .
97+ # We add an extra empty parameter so we can indicate this to the go method .
10598 __${ name } _debug "Adding extra empty parameter"
10699 # PowerShell 7.2+ changed the way how the arguments are passed to executables,
107100 # so for pre-7.2 or when Legacy argument passing is enabled we need to use
108- # \`"\`" to pass an empty argument, a "" or '' does not work!!!
109101 if ($PSVersionTable.PsVersion -lt [version]'7.2.0' -or
110102 ($PSVersionTable.PsVersion -lt [version]'7.3.0' -and -not [ExperimentalFeature]::IsEnabled("PSNativeCommandArgumentPassing")) -or
111103 (($PSVersionTable.PsVersion -ge [version]'7.3.0' -or [ExperimentalFeature]::IsEnabled("PSNativeCommandArgumentPassing")) -and
112104 $PSNativeCommandArgumentPassing -eq 'Legacy')) {
113105 $RequestComp="$RequestComp" + ' \`"\`"'
114106 } else {
115- $RequestComp= "$RequestComp" + ' ""'
107+ $RequestComp = "$RequestComp" + ' ""'
116108 }
117109 }
118110
119- __${ name } _debug "Calling: $RequestComp"
120- # First disable ActiveHelp which is not supported for PowerShell
111+ __${ name } _debug "Calling $RequestComp"
112+ # First disable ActiveHelp which is not supported for Powershell
113+ $env:ActiveHelp = 0
121114
122- # Call the command, store the output in $out, and redirect stderr and stdout to null
123- # $Out is an array containing each line per element
115+ # call the command store the output in $out and redirect stderr and stdout to null
116+ # $Out is an array contains each line per element
124117 Invoke-Expression -OutVariable out "$RequestComp" 2>&1 | Out-Null
125118
126- # Get directive from last line
119+ # get directive from last line
127120 [int]$Directive = $Out[-1].TrimStart(':')
128121 if ($Directive -eq "") {
129122 # There is no directive specified
130123 $Directive = 0
131124 }
132125 __${ name } _debug "The completion directive is: $Directive"
133126
134- # Remove directive (last element) from out
127+ # remove directive (last element) from out
135128 $Out = $Out | Where-Object { $_ -ne $Out[-1] }
136129 __${ name } _debug "The completions are: $Out"
137130
138131 if (($Directive -band $ShellCompDirectiveError) -ne 0 ) {
139- # Error code. No completion.
140- __${ name } _debug "Received error from custom completion code"
132+ # Error code. No completion.
133+ __${ name } _debug "Received error from custom completion go code"
141134 return
142135 }
143136
144137 $Longest = 0
145138 [Array]$Values = $Out | ForEach-Object {
146- # Split the output into name and description
147- $Name, $Description = $_.Split("t",2)
139+ # Split the output in name and description
140+ $Name, $Description = $_.Split("\` t", 2)
148141 __${ name } _debug "Name: $Name Description: $Description"
149142
150- # Look for the longest completion to format things nicely
143+ # Look for the longest completion so that we can format things nicely
151144 if ($Longest -lt $Name.Length) {
152145 $Longest = $Name.Length
153146 }
154147
155- # Set the description to a one- space string if none is set
156- # This is needed because CompletionResult does not accept an empty string
148+ # Set the description to a one space string if there is none set.
149+ # This is needed because the CompletionResult does not accept an empty string as argument
157150 if (-Not $Description) {
158151 $Description = " "
159152 }
160- @{Name= "$Name";Description= "$Description"}
153+ @{ Name = "$Name"; Description = "$Description" }
161154 }
162155
156+
163157 $Space = " "
164158 if (($Directive -band $ShellCompDirectiveNoSpace) -ne 0 ) {
165- # Remove the space here
159+ # remove the space here
166160 __${ name } _debug "ShellCompDirectiveNoSpace is called"
167161 $Space = ""
168162 }
169163
170164 if ((($Directive -band $ShellCompDirectiveFilterFileExt) -ne 0 ) -or
171165 (($Directive -band $ShellCompDirectiveFilterDirs) -ne 0 )) {
172- __${ name } _debug "ShellCompDirectiveFilterFileExt and ShellCompDirectiveFilterDirs are not supported"
166+ __${ name } _debug "ShellCompDirectiveFilterFileExt ShellCompDirectiveFilterDirs are not supported"
173167
174- # Return here to prevent the completion of the extensions
168+ # return here to prevent the completion of the extensions
175169 return
176170 }
177171
178172 $Values = $Values | Where-Object {
179- # Filter the result
173+ # filter the result
180174 $_.Name -like "$WordToComplete*"
181175
182176 # Join the flag back if we have an equal sign flag
@@ -186,7 +180,7 @@ export function generate(
186180 }
187181 }
188182
189- # Sort the values in ascending order by name if keep order isn't passed
183+ # we sort the values in ascending order by name if keep order isn't passed
190184 if (($Directive -band $ShellCompDirectiveKeepOrder) -eq 0 ) {
191185 $Values = $Values | Sort-Object -Property Name
192186 }
@@ -195,43 +189,45 @@ export function generate(
195189 __${ name } _debug "ShellCompDirectiveNoFileComp is called"
196190
197191 if ($Values.Length -eq 0) {
198- # Just print an empty string here so the shell does not start to complete paths
199- # We cannot use CompletionResult here because it does not accept an empty string
192+ # Just print an empty string here so the
193+ # shell does not start to complete paths.
194+ # We cannot use CompletionResult here because
195+ # it does not accept an empty string as argument.
200196 ""
201197 return
202198 }
203199 }
204200
205201 # Get the current mode
206- $Mode = (Get-PSReadLineKeyHandler | Where-Object {$_.Key -eq "Tab" }).Function
202+ $Mode = (Get-PSReadLineKeyHandler | Where-Object { $_.Key -eq "Tab" }).Function
207203 __${ name } _debug "Mode: $Mode"
208204
209205 $Values | ForEach-Object {
210206
211- # Store temporary because switch will overwrite $_
207+ # store temporary because switch will overwrite $_
212208 $comp = $_
213209
214210 # PowerShell supports three different completion modes
215- # - TabCompleteNext (default Windows style)
211+ # - TabCompleteNext (default windows style - on each key press the next option is displayed )
216212 # - Complete (works like bash)
217213 # - MenuComplete (works like zsh)
218- # Set the mode with Set-PSReadLineKeyHandler -Key Tab -Function <mode>
214+ # You set the mode with Set-PSReadLineKeyHandler -Key Tab -Function <mode>
219215
220216 # CompletionResult Arguments:
221- # 1) CompletionText: text to be used as the auto- completion result
222- # 2) ListItemText: text to be displayed in the suggestion list
223- # 3) ResultType: type of completion result
224- # 4) ToolTip: text for the tooltip with details about the object
217+ # 1) CompletionText text to be used as the auto completion result
218+ # 2) ListItemText text to be displayed in the suggestion list
219+ # 3) ResultType type of completion result
220+ # 4) ToolTip text for the tooltip with details about the object
225221
226222 switch ($Mode) {
227223
228- # Bash- like
224+ # bash like
229225 "Complete" {
230226
231227 if ($Values.Length -eq 1) {
232228 __${ name } _debug "Only one completion left"
233229
234- # Insert space after value
230+ # insert space after value
235231 [System.Management.Automation.CompletionResult]::new($($comp.Name | __${ name } _escapeStringWithSpecialChars) + $Space, "$($comp.Name)", 'ParameterValue', "$($comp.Description)")
236232
237233 } else {
@@ -251,29 +247,26 @@ export function generate(
251247 }
252248 }
253249
254- # Zsh- like
250+ # zsh like
255251 "MenuComplete" {
256- # Insert space after value
257- # MenuComplete will automatically show the ToolTip
252+ # insert space after value
253+ # MenuComplete will automatically show the ToolTip of
254+ # the highlighted value at the bottom of the suggestions.
258255 [System.Management.Automation.CompletionResult]::new($($comp.Name | __${ name } _escapeStringWithSpecialChars) + $Space, "$($comp.Name)", 'ParameterValue', "$($comp.Description)")
259256 }
260257
261- # Default and TabCompleteNext
258+ # TabCompleteNext and in case we get something unknown
262259 Default {
263- # Like MenuComplete but without adding a space
264- # Description will not be shown as it's not possible with TabCompleteNext
260+ # Like MenuComplete but we don't want to add a space here because
261+ # the user need to press space anyway to get the completion.
262+ # Description will not be shown because that's not possible with TabCompleteNext
265263 [System.Management.Automation.CompletionResult]::new($($comp.Name | __${ name } _escapeStringWithSpecialChars), "$($comp.Name)", 'ParameterValue', "$($comp.Description)")
266264 }
267265 }
268266
269267 }
270268}
271269
272- $Values | ForEach-Object {
273- __${ name } _debug "Completion suggestion: $($_.Name)"
274- }
275-
276-
277270Register-ArgumentCompleter -CommandName '${ name } ' -ScriptBlock $__${ nameForVar } CompleterBlock
278271` ;
279272}
0 commit comments