Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(complete): Fix PowerShell dynamic completion #5848

Merged
merged 1 commit into from
Dec 17, 2024

Conversation

jennings
Copy link
Contributor

@jennings jennings commented Dec 17, 2024

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-String to the pipeline ensures that Invoke-Expression receives the whole script as a single value from the pipeline.

Fixes: #5847

@jennings
Copy link
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
@jennings jennings force-pushed the jennings/push-xolwzyoornps branch from 1d1b412 to 99b6391 Compare December 17, 2024 06:53
@jennings jennings changed the title fix(complete): Update PowerShell dynamic completion script fix(complete): Fix PowerShell dynamic completion Dec 17, 2024
@epage epage merged commit ca89617 into clap-rs:master Dec 17, 2024
25 checks passed
@epage
Copy link
Member

epage commented Dec 17, 2024

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PowerShell dynamic completion script is not valid PowerShell
2 participants