From 90644cf37fefaf2329e14e35d1089584f673bef3 Mon Sep 17 00:00:00 2001 From: hasan Date: Fri, 24 Oct 2025 09:57:52 +0000 Subject: [PATCH] Fix: add Register-ArgumentCompleter support for PowerShell 7.4+ --- src/pip/_internal/cli/autocompletion.py | 40 ++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/pip/_internal/cli/autocompletion.py b/src/pip/_internal/cli/autocompletion.py index f22cd1159a2..a603564c166 100644 --- a/src/pip/_internal/cli/autocompletion.py +++ b/src/pip/_internal/cli/autocompletion.py @@ -111,7 +111,6 @@ def autocomplete() -> None: print(handler_name) else: # show main parser options only when necessary - opts = [i.option_list for i in parser.option_groups] opts.append(parser.option_list) flattened_opts = chain.from_iterable(opts) @@ -182,3 +181,42 @@ def auto_complete_paths(current: str, completion_type: str) -> Iterable[str]: yield comp_file elif os.path.isdir(opt): yield os.path.join(comp_file, "") + + +# --------------------------------------------------------------------------- +# PowerShell 7.4+ completion support using Register-ArgumentCompleter +# Issue: https://github.com/pypa/pip/issues/12440 +# --------------------------------------------------------------------------- + +POWERSHELL_COMPLETION_SCRIPT = r''' +if (Get-Command Register-ArgumentCompleter -ErrorAction SilentlyContinue) { + Register-ArgumentCompleter -Native -CommandName 'pip' -ScriptBlock { + param( + [string]$wordToComplete, + [System.Management.Automation.Language.CommandAst]$commandAst, + $cursorPosition + ) + $Env:COMP_WORDS = $commandAst.ToString() + $Env:COMP_CWORD = $commandAst.ToString().Split().Length - 1 + $Env:PIP_AUTO_COMPLETE = 1 + (& pip).Split() | ForEach-Object { + [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) + } + Remove-Item Env:COMP_WORDS -ErrorAction SilentlyContinue + Remove-Item Env:COMP_CWORD -ErrorAction SilentlyContinue + Remove-Item Env:PIP_AUTO_COMPLETE -ErrorAction SilentlyContinue + } +} +else { + # Fallback for legacy PowerShell versions + function TabExpansion($line, $lastWord) { + $Env:COMP_WORDS = $line + $Env:COMP_CWORD = $line.Split().Length - 1 + $Env:PIP_AUTO_COMPLETE = 1 + (& pip).Split() + Remove-Item Env:COMP_WORDS -ErrorAction SilentlyContinue + Remove-Item Env:COMP_CWORD -ErrorAction SilentlyContinue + Remove-Item Env:PIP_AUTO_COMPLETE -ErrorAction SilentlyContinue + } +} +'''