Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/improve argument completion for PowerShell 7.4+ #12440

Open
1 task done
BinToss opened this issue Dec 18, 2023 · 0 comments
Open
1 task done

Fix/improve argument completion for PowerShell 7.4+ #12440

BinToss opened this issue Dec 18, 2023 · 0 comments
Labels
C: autocomplete Autocompletion in shells (pip completion) good first issue A good item for first time contributors to work on state: awaiting PR Feature discussed, PR is needed type: enhancement Improvements to functionality

Comments

@BinToss
Copy link

BinToss commented Dec 18, 2023

What's the problem this feature will solve?

The current implementation hooks the legacy PowerShell function TabExpansion. This function does not exist in Powershell 7.4 and later.
image
The function was replaced by TabExpansion2. Completion/suggestion implementations are recommended to use the Register-ArgumentCompleter API which is in Windows PowerShell (5.1) and PowerShell Core (6.0 and later).

Describe the solution you'd like

The Register-ArgumentCompleter function can be leveraged like so:

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
    Remove-Item Env:COMP_CWORD
    Remove-Item Env:PIP_AUTO_COMPLETE
}

This can be appended to the user's $PROFILE script as-is, as a string executed by Invoke-Expression, or as a dot-sourced command invoking a script file (e.g. . path/to/pip-completion.ps1).

Unlike the old implementation, this does not override any built-in functions. The currently-unused $cursorPosition parameter can be leveraged for completion/suggestion at the cursor's position instead of only the last word in the CommandAst.

Known Issues

Alternative Solutions

The provided solution works well enough for now. Additional functionality can be implemented later.

Additional context

Improves upon #9025

Code of Conduct

@BinToss BinToss added S: needs triage Issues/PRs that need to be triaged type: feature request Request for a new feature labels Dec 18, 2023
@ichard26 ichard26 added type: enhancement Improvements to functionality state: awaiting PR Feature discussed, PR is needed C: autocomplete Autocompletion in shells (pip completion) good first issue A good item for first time contributors to work on and removed type: feature request Request for a new feature S: needs triage Issues/PRs that need to be triaged labels Apr 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: autocomplete Autocompletion in shells (pip completion) good first issue A good item for first time contributors to work on state: awaiting PR Feature discussed, PR is needed type: enhancement Improvements to functionality
Projects
None yet
Development

No branches or pull requests

2 participants