Skip to content

Commit

Permalink
Merge pull request #5848 from jennings/jennings/push-xolwzyoornps
Browse files Browse the repository at this point in the history
fix(complete): Fix PowerShell dynamic completion
  • Loading branch information
epage authored Dec 17, 2024
2 parents ed2360f + 99b6391 commit ca89617
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 3 additions & 1 deletion clap_complete/src/env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@
//!
//! Powershell
//! ```powershell
//! echo "COMPLETE=powershell your_program | Invoke-Expression" >> $PROFILE
//! $env:COMPLETE = "powershell"
//! echo "your_program | Out-String | Invoke-Expression" >> $PROFILE
//! Remove-Item Env:\COMPLETE
//! ```
//!
//! Zsh
Expand Down
14 changes: 13 additions & 1 deletion clap_complete/src/env/shells.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,25 @@ impl EnvCompleter for Powershell {
let completer =
shlex::try_quote(completer).unwrap_or(std::borrow::Cow::Borrowed(completer));

// `completer` may or may not be surrounded by double quotes, enclosing
// the expression in a here-string ensures the whole thing is
// interpreted as the first argument to the call operator
writeln!(
buf,
r#"
Register-ArgumentCompleter -Native -CommandName {bin} -ScriptBlock {{
param($wordToComplete, $commandAst, $cursorPosition)
$results = Invoke-Expression "{var}=powershell &{completer} -- $($commandAst.ToString())";
$prev = $env:{var};
$env:{var} = "powershell";
$results = Invoke-Expression @"
& {completer} -- $commandAst
"@;
if ($null -eq $prev) {{
Remove-Item Env:\{var};
}} else {{
$env:{var} = $prev;
}}
$results | ForEach-Object {{
$split = $_.Split("`t");
$cmd = $split[0];
Expand Down

0 comments on commit ca89617

Please sign in to comment.