fix(complete): Fix PowerShell dynamic completion#5848
Merged
epage merged 1 commit intoclap-rs:masterfrom Dec 17, 2024
Merged
Conversation
Contributor
Author
|
I admit I haven't yet been able to test this by actually compiling anything (I am pretty new to Rust). I modified the script in my PowerShell profile to match the change in this PR: Register-ArgumentCompleter -Native -CommandName jj -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)
$prev = $env:COMPLETE
$env:COMPLETE = "powershell"
$results = Invoke-Expression @"
& "C:\\Users\\StephenJennings\\.cargo\\bin\\jj.exe" -- $commandAst
"@
if ($null -eq $prev) {
Remove-Item Env:\COMPLETE
} else {
$env:COMPLETE = $prev
}
$results | ForEach-Object {
$split = $_.Split("`t");
$cmd = $split[0];
if ($split.Length -eq 2) {
$help = $split[1];
}
else {
$help = $split[0];
}
[System.Management.Automation.CompletionResult]::new($cmd, $cmd, 'ParameterValue', $help)
}
}; |
PowerShell does not support inline syntax for assigning environment variables, so we must instead set the value before running the completer and restore it after it exits. The completer will often, if not always, be surrounded by double quotes. To avoid syntax errors, define the argument to Invoke-Expression as a here-string so the quotes don't create a syntax error. Updates the instructions for adding the argument completer to the profile. Piping a native command to Invoke-Expression invokes each line separately. Adding `Out-String` to the pipeline ensures that Invoke-Expression receives the whole script as a single value from the pipeline. Fixes: clap-rs#5847
1d1b412 to
99b6391
Compare
Member
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PowerShell does not support inline syntax for assigning environment variables, so we must instead set the value before running the completer and restore it after it exits.
The completer will often, if not always, be surrounded by double quotes on Windows. To avoid syntax errors, define the argument to Invoke-Expression as a here-string so the quotes don't create a syntax error.
Updates the instructions for adding the argument completer to the profile. Piping a native command to Invoke-Expression invokes each line separately. Adding
Out-Stringto the pipeline ensures that Invoke-Expression receives the whole script as a single value from the pipeline.Fixes: #5847