Skip to content

Commit 453b8fd

Browse files
committed
Fix: update PowerShell completion test for Register-ArgumentCompleter (#12440)
1 parent f49a2d9 commit 453b8fd

File tree

3 files changed

+57
-37
lines changed

3 files changed

+57
-37
lines changed

news/12440.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix PowerShell completion by switching to Register-ArgumentCompleter (Closes #12440)

src/pip/_internal/commands/completion.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,30 +53,39 @@
5353
complete -fa "(__fish_complete_pip)" -c {prog}
5454
""",
5555
"powershell": """
56-
if ((Test-Path Function:\\TabExpansion) -and -not `
57-
(Test-Path Function:\\_pip_completeBackup)) {{
58-
Rename-Item Function:\\TabExpansion _pip_completeBackup
59-
}}
60-
function TabExpansion($line, $lastWord) {{
61-
$lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart()
62-
if ($lastBlock.StartsWith("{prog} ")) {{
63-
$Env:COMP_WORDS=$lastBlock
64-
$Env:COMP_CWORD=$lastBlock.Split().Length - 1
65-
$Env:PIP_AUTO_COMPLETE=1
66-
(& {prog}).Split()
67-
Remove-Item Env:COMP_WORDS
68-
Remove-Item Env:COMP_CWORD
69-
Remove-Item Env:PIP_AUTO_COMPLETE
70-
}}
71-
elseif (Test-Path Function:\\_pip_completeBackup) {{
72-
# Fall back on existing tab expansion
73-
_pip_completeBackup $line $lastWord
56+
Register-ArgumentCompleter -Native -CommandName '{prog}' -ScriptBlock {{
57+
param($commandName, $commandAst, $cursorPosition)
58+
$commandElements = $commandAst.CommandElements
59+
$command = @(
60+
'{prog}'
61+
for ($i = 1; $i -lt $commandElements.Count; $i++) {{
62+
$element = $commandElements[$i]
63+
if ($element -isnot [StringConstantExpressionAst] -or
64+
$element.StringConstantType -ne [StringConstantType]::BareWord -or
65+
$element.Value.StartsWith('-')) {{
66+
break
67+
}}
68+
$element.Value
69+
}}
70+
) -join ' '
71+
72+
$Env:COMP_WORDS = $command
73+
$Env:COMP_CWORD = $commandElements.Count - 1
74+
$Env:PIP_AUTO_COMPLETE = 1
75+
$completions = & {prog} 2>$null
76+
Remove-Item Env:COMP_WORDS
77+
Remove-Item Env:COMP_CWORD
78+
Remove-Item Env:PIP_AUTO_COMPLETE
79+
80+
if ($completions) {{
81+
$completions.Split() | ForEach-Object {{
82+
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
83+
}}
7484
}}
7585
}}
7686
""",
7787
}
7888

79-
8089
class CompletionCommand(Command):
8190
"""A helper command to be used for command completion."""
8291

tests/functional/test_completion.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,34 @@
6060
(
6161
"powershell",
6262
"""\
63-
if ((Test-Path Function:\\TabExpansion) -and -not `
64-
(Test-Path Function:\\_pip_completeBackup)) {
65-
Rename-Item Function:\\TabExpansion _pip_completeBackup
66-
}
67-
function TabExpansion($line, $lastWord) {
68-
$lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart()
69-
if ($lastBlock.StartsWith("pip ")) {
70-
$Env:COMP_WORDS=$lastBlock
71-
$Env:COMP_CWORD=$lastBlock.Split().Length - 1
72-
$Env:PIP_AUTO_COMPLETE=1
73-
(& pip).Split()
74-
Remove-Item Env:COMP_WORDS
75-
Remove-Item Env:COMP_CWORD
76-
Remove-Item Env:PIP_AUTO_COMPLETE
77-
}
78-
elseif (Test-Path Function:\\_pip_completeBackup) {
79-
# Fall back on existing tab expansion
80-
_pip_completeBackup $line $lastWord
63+
Register-ArgumentCompleter -Native -CommandName 'pip' -ScriptBlock {
64+
param($commandName, $commandAst, $cursorPosition)
65+
$commandElements = $commandAst.CommandElements
66+
$command = @(
67+
'pip'
68+
for ($i = 1; $i -lt $commandElements.Count; $i++) {
69+
$element = $commandElements[$i]
70+
if ($element -isnot [StringConstantExpressionAst] -or
71+
$element.StringConstantType -ne [StringConstantType]::BareWord -or
72+
$element.Value.StartsWith('-')) {
73+
break
74+
}
75+
$element.Value
76+
}
77+
) -join ' '
78+
79+
$Env:COMP_WORDS = $command
80+
$Env:COMP_CWORD = $commandElements.Count - 1
81+
$Env:PIP_AUTO_COMPLETE = 1
82+
$completions = & pip 2>$null
83+
Remove-Item Env:COMP_WORDS
84+
Remove-Item Env:COMP_CWORD
85+
Remove-Item Env:PIP_AUTO_COMPLETE
86+
87+
if ($completions) {
88+
$completions.Split() | ForEach-Object {
89+
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
90+
}
8191
}
8292
}""",
8393
),

0 commit comments

Comments
 (0)