Add tab completion for $PSBoundParameters.Keys switch cases and access patterns#26483
Conversation
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
|
@MartinGC94 Please review. |
There was a problem hiding this comment.
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
$PSBoundParametersusage 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.
…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>
MartinGC94
left a comment
There was a problem hiding this comment.
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.
…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>
…letionAnalysis.cs Co-authored-by: MartinGC94 <42123497+MartinGC94@users.noreply.github.com>
…letionAnalysis.cs Co-authored-by: MartinGC94 <42123497+MartinGC94@users.noreply.github.com>
There was a problem hiding this comment.
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.
…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>
There was a problem hiding this comment.
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.
- 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.
There was a problem hiding this comment.
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.
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.
| var commandExpressionAst = conditionPipeline.PipelineElements[0] as CommandExpressionAst; | ||
| if (commandExpressionAst == null) | ||
| { | ||
| return null; |
There was a problem hiding this comment.
The same. I see below other such samples.
There was a problem hiding this comment.
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 notpattern matching instead ofaswith null checks - Used
is null/is not nullinstead of== null/!= null
Commit: 4439fbb
…letionAnalysis.cs Co-authored-by: Ilya <darpa@yandex.ru>
|
25.79 |
PR Summary
Add tab completion for parameter names when working with
$PSBoundParametersin various access patterns.PR Context
Fixes #25349
When writing functions that use
$PSBoundParametersto 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
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerDescription
This PR adds tab completion for parameter names in the following
$PSBoundParametersaccess patterns:Supported Patterns
Switch statement on Keys
ContainsKey method
Indexer access
Remove method
Implementation Details
CompleteAgainstSwitchCaseConditionmethod for switch case completionCompleteAgainstPSBoundParametersAccessmethod for ContainsKey, indexer, and Remove patternsFiles Changed
src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs- Added completion logictest/powershell/Host/TabCompletion/TabCompletion.Tests.ps1- Added 8 test casesTesting
Manual Testing
Automated Tests
8 new test cases added covering: