Skip to content

Add tab completion for $PSBoundParameters.Keys switch cases and access patterns#26483

Merged
iSazonov merged 20 commits into
PowerShell:masterfrom
yotsuda:feature/switch-psboundparameters-completion
Nov 26, 2025
Merged

Add tab completion for $PSBoundParameters.Keys switch cases and access patterns#26483
iSazonov merged 20 commits into
PowerShell:masterfrom
yotsuda:feature/switch-psboundparameters-completion

Conversation

@yotsuda
Copy link
Copy Markdown
Contributor

@yotsuda yotsuda commented Nov 19, 2025

PR Summary

Add tab completion for parameter names when working with $PSBoundParameters in various access patterns.

PR Context

Fixes #25349

When writing functions that use $PSBoundParameters to check which parameters were bound, it's easy to make typos in parameter names. This PR adds tab completion support to help prevent such errors.

PR Checklist

Description

This PR adds tab completion for parameter names in the following $PSBoundParameters access patterns:

Supported Patterns

  1. Switch statement on Keys

    switch ($PSBoundParameters.Keys) {
        Para<Tab>  # Completes to Param1, Param2, etc.
    }
  2. ContainsKey method

    if ($PSBoundParameters.ContainsKey('<Tab>')) { }  # Completes to 'Param1', 'Param2', etc.
  3. Indexer access

    $value = $PSBoundParameters['<Tab>']  # Completes to 'Param1', 'Param2', etc.
  4. Remove method

    $PSBoundParameters.Remove('<Tab>')  # Completes to 'Param1', 'Param2', etc.

Implementation Details

  • Added CompleteAgainstSwitchCaseCondition method for switch case completion
  • Added CompleteAgainstPSBoundParametersAccess method for ContainsKey, indexer, and Remove patterns
  • Quote style is preserved (single or double quotes) in the completion text
  • Switch case completions use bare words (no quotes) as is standard for switch cases

Files Changed

  • src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs - Added completion logic
  • test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 - Added 8 test cases

Testing

Manual Testing

function Test-Func {
    param(
        [string]$Param1,
        [string]$Param2,
        [int]$Count
    )
    
    # Test 1: Switch case - type 'P' then Tab
    switch ($PSBoundParameters.Keys) {
        P<Tab>  # Should complete to Param1, Param2
    }
    
    # Test 2: ContainsKey - type 'P' then Tab
    if ($PSBoundParameters.ContainsKey('P<Tab>')) { }  # Should complete to 'Param1', 'Param2'
    
    # Test 3: Indexer - type 'P' then Tab
    $value = $PSBoundParameters['P<Tab>']  # Should complete to 'Param1', 'Param2'
    
    # Test 4: Remove - type 'C' then Tab
    $PSBoundParameters.Remove('C<Tab>')  # Should complete to 'Count'
}

Automated Tests

8 new test cases added covering:

  • Switch case completion
  • ContainsKey method completion
  • Indexer access completion
  • Remove method completion
  • Double quote preservation
  • Non-PSBoundParameters variables (negative test)

When working with $PSBoundParameters, tab completion now suggests parameter
names from the enclosing function or scriptblock's param block in the
following scenarios:

- switch ($PSBoundParameters.Keys) { <Tab> }
- $PSBoundParameters.ContainsKey('<Tab>')
- $PSBoundParameters['<Tab>']
- $PSBoundParameters.Remove('<Tab>')

This helps prevent typos when writing code that checks or accesses
bound parameter names.

Fixes PowerShell#25349
@iSazonov
Copy link
Copy Markdown
Collaborator

@MartinGC94 Please review.

@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Nov 19, 2025
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds tab completion for $PSBoundParameters parameter names to help prevent typos when accessing bound parameters. The feature supports switch cases on .Keys, as well as ContainsKey(), indexer access, and Remove() method calls.

Key Changes:

  • Adds two new completion methods that detect $PSBoundParameters usage patterns and provide parameter name completions
  • Integrates these methods into the existing completion pipeline at multiple entry points
  • Preserves quote style (single/double quotes) for quoted string contexts
  • Supports both complete and incomplete (error) switch statement ASTs

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
CompletionAnalysis.cs Adds CompleteAgainstSwitchCaseCondition and CompleteAgainstPSBoundParametersAccess methods with AST analysis logic, integrated into string and identifier completion paths
TabCompletion.Tests.ps1 Adds 8 comprehensive test cases covering switch cases, ContainsKey, indexer, Remove methods, quote preservation, and negative test cases

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs Outdated
Comment thread src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs Outdated
Comment thread src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs Outdated
Comment thread src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs Outdated
yotsuda and others added 4 commits November 19, 2025 16:27
…letionAnalysis.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…letionAnalysis.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…letionAnalysis.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@yotsuda yotsuda marked this pull request as ready for review November 19, 2025 09:52
Copy link
Copy Markdown
Contributor

@MartinGC94 MartinGC94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The overall logic seems fine but replace the "as" and null checks with pattern matching. Also unless something has changed, LINQ should generally be avoided.

Comment thread src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs Outdated
Comment thread src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs Outdated
Comment thread src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs Outdated
Comment thread src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs Outdated
Comment thread src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs Outdated
Comment thread src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs Outdated
Comment thread src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs Outdated
@microsoft-github-policy-service microsoft-github-policy-service Bot added the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Nov 20, 2025
yotsuda and others added 2 commits November 20, 2025 09:28
…letionAnalysis.cs

Co-authored-by: MartinGC94 <42123497+MartinGC94@users.noreply.github.com>
…letionAnalysis.cs

Co-authored-by: MartinGC94 <42123497+MartinGC94@users.noreply.github.com>
@microsoft-github-policy-service microsoft-github-policy-service Bot removed the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Nov 20, 2025
yotsuda and others added 4 commits November 20, 2025 09:28
…letionAnalysis.cs

Co-authored-by: MartinGC94 <42123497+MartinGC94@users.noreply.github.com>
…letionAnalysis.cs

Co-authored-by: MartinGC94 <42123497+MartinGC94@users.noreply.github.com>
…letionAnalysis.cs

Co-authored-by: MartinGC94 <42123497+MartinGC94@users.noreply.github.com>
…letionAnalysis.cs

Co-authored-by: MartinGC94 <42123497+MartinGC94@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

Comments suppressed due to low confidence (1)

src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs:395

  • This assignment to result is useless, since its value is never read.
            var result = new List<CompletionResult>();

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs Outdated
Comment thread src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs Outdated
Comment thread src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs Outdated
yotsuda and others added 3 commits November 20, 2025 21:35
…letionAnalysis.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…letionAnalysis.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…letionAnalysis.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@iSazonov iSazonov requested a review from Copilot November 20, 2025 12:37
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs Outdated
Comment thread src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs Outdated
- Remove useless initialization of result variable in CompleteAgainstPSBoundParametersAccess
- Extract duplicate ParamBlock finding logic into FindNearestParamBlock helper method

This refactoring improves code maintainability by reducing duplication between
CompleteAgainstSwitchCaseCondition and CompleteAgainstPSBoundParametersAccess methods.
- Create CreateParameterCompletionResults helper method to reduce code duplication
- Consolidate completion result generation logic between CompleteAgainstSwitchCaseCondition and CompleteAgainstPSBoundParametersAccess
- Add optional quoteChar parameter to support both quoted and unquoted completion

This addresses the second Copilot code review feedback and improves code maintainability.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1
Addresses review feedback to test all code paths for non-PSBoundParameters
variables. The original single negative test is now split into four
separate tests covering indexer, ContainsKey, Remove, and double-quoted
string access patterns.
Comment thread src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs Outdated
Comment on lines +338 to +341
var commandExpressionAst = conditionPipeline.PipelineElements[0] as CommandExpressionAst;
if (commandExpressionAst == null)
{
return null;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same. I see below other such samples.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review feedback.
I've applied the commit suggestion and made additional changes to use modern C# patterns consistently:

  • Used is/is not pattern matching instead of as with null checks
  • Used is null/is not null instead of == null/!= null

Commit: 4439fbb

@iSazonov iSazonov self-assigned this Nov 26, 2025
@iSazonov iSazonov merged commit 01e3643 into PowerShell:master Nov 26, 2025
36 checks passed
@yotsuda yotsuda deleted the feature/switch-psboundparameters-completion branch November 26, 2025 07:04
SIRMARGIN pushed a commit to SIRMARGIN/PowerShell that referenced this pull request Dec 12, 2025
kilasuit pushed a commit to kilasuit/PowerShell that referenced this pull request Jan 2, 2026
@hamid01706169196
Copy link
Copy Markdown

25.79

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add completion for switch cases for $PSBoundParameters.Keys

5 participants