Add PowerShell autocompletion feature

This commit is contained in:
Florian Blanchet 2023-07-28 22:33:37 +03:00
parent 2ac251faa1
commit 99c693df39
3 changed files with 28 additions and 4 deletions

View file

@ -615,7 +615,7 @@ opposed to this idea. Here's what `eval "$(pyenv init -)"` actually does:
2. **Installs autocompletion.** This is entirely optional but pretty 2. **Installs autocompletion.** This is entirely optional but pretty
useful. Sourcing `$(pyenv root)/completions/pyenv.bash` will set that useful. Sourcing `$(pyenv root)/completions/pyenv.bash` will set that
up. There are also completions for Zsh and Fish. up. There are also completions for Zsh, Fish and PowerShell.
3. **Rehashes shims.** From time to time you'll need to rebuild your 3. **Rehashes shims.** From time to time you'll need to rebuild your
shim files. Doing this on init makes sure everything is up to shim files. Doing this on init makes sure everything is up to

17
completions/pyenv.pwsh Normal file
View file

@ -0,0 +1,17 @@
$scriptblock = {
param($wordToComplete, $commandAst, $cursorPosition)
$words = $commandAst.ToString()
if ( $wordToComplete ) {
$matches = (($words[0..$cursorPosition] -join '') | Select-String -Pattern "\s+" -AllMatches).Matches
if ( $matches ) {
$cursorPosition = $matches[-1].Index - 1
}
}
$words = $words[0..$cursorPosition] -join '' -split "\s+"
if ( $words.Count -ge 2 ) {
pyenv completions $words[1] | where { $_ -match $wordToComplete }
} else {
pyenv commands | where { $_ -match $wordToComplete }
}
}
Register-ArgumentCompleter -Native -CommandName pyenv -ScriptBlock $scriptblock

View file

@ -281,7 +281,14 @@ function print_env() {
function print_completion() { function print_completion() {
completion="${root}/completions/pyenv.${shell}" completion="${root}/completions/pyenv.${shell}"
if [ -r "$completion" ]; then if [ -r "$completion" ]; then
case "$shell" in
pwsh )
echo "iex (gc $completion -Raw)"
;;
* )
echo "source '$completion'" echo "source '$completion'"
;;
esac
fi fi
} }