Skip to content

Tab completion is unreliable in param blocks #15300

@MartinGC94

Description

@MartinGC94

A simple param block like this:

Param
(
    $Param1
)

Will not offer tab completion for Parameters, Attributes, Members and possibly more things inside the param block.
If you add a begin/process/end keyword like this:

Param
(
    $Param1
)
begin

The tab completion will work as you would expect. When I look at the ast the problem appears to be that the whole script is surrounded by an implicit end block unless you explicitly add a begin/process/end block and this causes the "lastAst" https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs#L395 to be a NamedBlockAst instead of an AttributeAst or whatever the various completions expect.

An easy fix that doesn't appear to break anything is to remove the NamedBlockAst from the list if it happens to be the last item in the "RelatedAsts" list but is there a better way? A safer alternative is to add more logic in every location where it causes issues today but do we really want more complexity in the completion code?

Steps to reproduce

Param
(
    $Param1=(Get-ChildItem -<Tab>)
)

Expected behavior

Param
(
    $Param1=(Get-ChildItem -Path)
)

Actual behavior

Param
(
    $Param1=(Get-ChildItem -)
)

Environment data

Name                           Value
----                           -----
PSVersion                      7.2.0-preview.5
PSEdition                      Core
GitCommitId                    7.2.0-preview.5
OS                             Microsoft Windows 10.0.21359
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions