Skip to content
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ tests/data/common_wheels/

# Profiling related artifacts
*.prof
pip-dev-env/
1 change: 1 addition & 0 deletions news/12440.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix PowerShell completion by switching to Register-ArgumentCompleter (Closes #12440)
49 changes: 31 additions & 18 deletions src/pip/_internal/commands/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,37 @@
complete -fa "(__fish_complete_pip)" -c {prog}
""",
"powershell": """
if ((Test-Path Function:\\TabExpansion) -and -not `
(Test-Path Function:\\_pip_completeBackup)) {{
Rename-Item Function:\\TabExpansion _pip_completeBackup
}}
function TabExpansion($line, $lastWord) {{
$lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart()
if ($lastBlock.StartsWith("{prog} ")) {{
$Env:COMP_WORDS=$lastBlock
$Env:COMP_CWORD=$lastBlock.Split().Length - 1
$Env:PIP_AUTO_COMPLETE=1
(& {prog}).Split()
Remove-Item Env:COMP_WORDS
Remove-Item Env:COMP_CWORD
Remove-Item Env:PIP_AUTO_COMPLETE
}}
elseif (Test-Path Function:\\_pip_completeBackup) {{
# Fall back on existing tab expansion
_pip_completeBackup $line $lastWord
Register-ArgumentCompleter -Native -CommandName '{prog}' -ScriptBlock {{
param($commandName, $commandAst, $cursorPosition)
$commandElements = $commandAst.CommandElements
$command = @(
'{prog}'
for ($i = 1; $i -lt $commandElements.Count; $i++) {{
$element = $commandElements[$i]
if ($element -isnot [StringConstantExpressionAst] -or
$element.StringConstantType -ne`
[StringConstantType]::BareWord -or
$element.Value.StartsWith('-')) {{
break
}}
$element.Value
}}
) -join ' '

$Env:COMP_WORDS = $command
$Env:COMP_CWORD = $commandElements.Count - 1
$Env:PIP_AUTO_COMPLETE = 1
$completions = & {prog} 2>$null
Remove-Item Env:COMP_WORDS
Remove-Item Env:COMP_CWORD
Remove-Item Env:PIP_AUTO_COMPLETE

if ($completions) {{
$completions.Split() | ForEach-Object {{
[System.Management.Automation.CompletionResult]::new(
$_, $_, 'ParameterValue', $_
)
}}
}}
}}
""",
Expand Down
54 changes: 35 additions & 19 deletions tests/functional/test_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,42 @@
(
"powershell",
"""\
if ((Test-Path Function:\\TabExpansion) -and -not `
(Test-Path Function:\\_pip_completeBackup)) {
Rename-Item Function:\\TabExpansion _pip_completeBackup
}
function TabExpansion($line, $lastWord) {
$lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart()
if ($lastBlock.StartsWith("pip ")) {
$Env:COMP_WORDS=$lastBlock
$Env:COMP_CWORD=$lastBlock.Split().Length - 1
$Env:PIP_AUTO_COMPLETE=1
(& pip).Split()
Remove-Item Env:COMP_WORDS
Remove-Item Env:COMP_CWORD
Remove-Item Env:PIP_AUTO_COMPLETE
}
elseif (Test-Path Function:\\_pip_completeBackup) {
# Fall back on existing tab expansion
_pip_completeBackup $line $lastWord
# pip powershell completion start
Register-ArgumentCompleter -Native -CommandName 'pip' -ScriptBlock {
param($commandName, $commandAst, $cursorPosition)
$commandElements = $commandAst.CommandElements
$command = @(
'pip'
for ($i = 1; $i -lt $commandElements.Count; $i++) {
$element = $commandElements[$i]
if ($element -isnot [StringConstantExpressionAst] -or
$element.StringConstantType -ne`
[StringConstantType]::BareWord -or
$element.Value.StartsWith('-')) {
break
}
$element.Value
}
) -join ' '

$Env:COMP_WORDS = $command
$Env:COMP_CWORD = $commandElements.Count - 1
$Env:PIP_AUTO_COMPLETE = 1
$completions = & pip 2>$null
Remove-Item Env:COMP_WORDS
Remove-Item Env:COMP_CWORD
Remove-Item Env:PIP_AUTO_COMPLETE

if ($completions) {
$completions.Split() | ForEach-Object {
[System.Management.Automation.CompletionResult]::new(
$_, $_, 'ParameterValue', $_
)
}
}
}""",
}
# pip powershell completion end
""",
),
)

Expand Down
Loading