powershellpsreadline

Why do parameter tab-autocomplete results show provider (e.g. filesystem) objects by default?


You can define auto-completion results for parameter values in a variety of different ways such as using dynamic parameters, the CompletionResult class within a parameter's ArgumentCompleter attribute declaration, the ValidateSet attribute, etc.

If tab-completion results aren't declared, then PowerShell (by default) will return a contextual list of provider objects/items for parameter value auto-completion suggestions (whether "autocompleted" with Tab or CTRL+SPACE).


For example, pressing CTRL+SPACE after Test -ParamName shows a list of filesystem objects.

enter image description here



I know that CTRL+SPACE is PSReadLine's MenuComplete function at work, but even if PSReadLine is removed, the default behavior of returning contextual provider objects still persists when using Tab to autocomplete parameter values.

Is this default behavior/preference (of returning provider objects) exposed anywhere like an automatic variable, .NET class property/field, .ps1xml file, etc?


Solution

  • This is the default behavior of the TabExpansion2 built-in function, more specifically, it is the default behavior of CommandCompletion.CompleteInput method when there are no completion results it will default to the child items in the current location:

    function Test-Completion {param($Param) }
    # This is the simulated input:
    $statement = 'Test-Completion '
    
    # Should output the child items in the current location:
    [System.Management.Automation.CommandCompletion]::CompleteInput(
        $statement, $statement.Length, $null).
        CompletionMatches
    

    If you would like to check the definition of your TabExpansion2 function / override its default behavior you can use:

    (Get-Command TabExpansion2).Definition