Skip to content
Myr

Make Powershell completion script work in constrained mode (#2196)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Creating CompletionResult objects is not allowed in Powershell constrained mode, so return results as strings if constrained mode is enabled Store results as PsCustomObjects instead of hashtables. This prevents Sort-Object from trying to convert the hashtable to a object, which is blocked in constrained mode. PsCustomObjects are created using New-Object to work around https://github.com/PowerShell/PowerShell/issues/20767

authored by Leonhard Stemplinger · committed by GitHub · parent 3a5efae · Browse files · 1 file+30 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+30 added−5 removed

  • other:+30 added−5 removed

1 file+30 added−5 removed

modified:powershell_completions.go+30 added-5 removed
160 unmodified lines
161 # This is needed because the CompletionResult does not accept an empty string as argument162 if (-Not $Description) {163 $Description = " "164 }165 @{Name="$Name";Description="$Description"}165 New-Object -TypeName PSCustomObject -Property @{166 Name = "$Name"167 Description = "$Description"168 }169 }170171172 $Space = " "
69 unmodified lines
242 if ($Values.Length -eq 1) {243 __%[1]s_debug "Only one completion left"244245 # insert space after value246 $CompletionText = $($comp.Name | __%[1]s_escapeStringWithSpecialChars) + $Space247 if ($ExecutionContext.SessionState.LanguageMode -eq "FullLanguage"){243 [System.Management.Automation.CompletionResult]::new($($comp.Name | __%[1]s_escapeStringWithSpecialChars) + $Space, "$($comp.Name)", 'ParameterValue', "$($comp.Description)")248 [System.Management.Automation.CompletionResult]::new($CompletionText, "$($comp.Name)", 'ParameterValue', "$($comp.Description)")249 } else {250 $CompletionText251 }252253 } else {254 # Add the proper number of spaces to align the descriptions255 while($comp.Name.Length -lt $Longest) {
6 unmodified lines
262 } else {263 $Description = " ($($comp.Description))"264 }265266 $CompletionText = "$($comp.Name)$Description"267 if ($ExecutionContext.SessionState.LanguageMode -eq "FullLanguage"){258 [System.Management.Automation.CompletionResult]::new("$($comp.Name)$Description", "$($comp.Name)$Description", 'ParameterValue', "$($comp.Description)")268 [System.Management.Automation.CompletionResult]::new($CompletionText, "$($comp.Name)$Description", 'ParameterValue', "$($comp.Description)")269 } else {270 $CompletionText271 }272 }273 }274275 # zsh like276 "MenuComplete" {277 # insert space after value278 # MenuComplete will automatically show the ToolTip of279 # the highlighted value at the bottom of the suggestions.280281 $CompletionText = $($comp.Name | __%[1]s_escapeStringWithSpecialChars) + $Space282 if ($ExecutionContext.SessionState.LanguageMode -eq "FullLanguage"){267 [System.Management.Automation.CompletionResult]::new($($comp.Name | __%[1]s_escapeStringWithSpecialChars) + $Space, "$($comp.Name)", 'ParameterValue', "$($comp.Description)")283 [System.Management.Automation.CompletionResult]::new($CompletionText, "$($comp.Name)", 'ParameterValue', "$($comp.Description)")284 } else {285 $CompletionText286 }287 }288289 # TabCompleteNext and in case we get something unknown290 Default {291 # Like MenuComplete but we don't want to add a space here because292 # the user need to press space anyway to get the completion.293 # Description will not be shown because that's not possible with TabCompleteNext294295 $CompletionText = $($comp.Name | __%[1]s_escapeStringWithSpecialChars)296 if ($ExecutionContext.SessionState.LanguageMode -eq "FullLanguage"){275 [System.Management.Automation.CompletionResult]::new($($comp.Name | __%[1]s_escapeStringWithSpecialChars), "$($comp.Name)", 'ParameterValue', "$($comp.Description)")297 [System.Management.Automation.CompletionResult]::new($CompletionText, "$($comp.Name)", 'ParameterValue', "$($comp.Description)")298 } else {299 $CompletionText300 }301 }302 }303304 }
46 unmodified lines