diff --git a/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs b/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs index 091523b8ae..e5f44a7034 100644 --- a/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs +++ b/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs @@ -473,6 +473,16 @@ namespace System.Management.Automation replacementLength = 0; break; + case TokenKind.Semi: + // Handle scenarios such as 'gci | Format-Table @{Label=...;' + if (lastAst is HashtableAst) + { + result = GetResultForHashtable(completionContext); + replacementIndex += 1; + replacementLength = 0; + } + break; + case TokenKind.Number: // Handle scenarios such as Get-Process -Id 5 || Get-Process -Id 5210, 3 || Get-Process -Id: 5210, 3 if (lastAst is ConstantExpressionAst && @@ -537,10 +547,15 @@ namespace System.Management.Automation completionContext.ReplacementLength = replacementLength = 0; result = GetResultForAttributeArgument(completionContext, ref replacementIndex, ref replacementLength); } + else if (lastAst is HashtableAst hashTableAst && !(lastAst.Parent is DynamicKeywordStatementAst) && CheckForPendingAssignment(hashTableAst)) + { + // Handle scenarios such as 'gci | Format-Table @{Label=' if incomplete parsing of the assignment. + return null; + } else { - // - // Handle auto completion for enum/dependson property of DSC resource, + // Handle scenarios such as 'configuration foo { File ab { Attributes =' + // (auto completion for enum/dependson property of DSC resource), // cursor is right after '=', '(' or '@(' // // Configuration config @@ -552,8 +567,7 @@ namespace System.Management.Automation // DependsOn=(| // bool unused; - result = GetResultForEnumPropertyValueOfDSCResource(completionContext, string.Empty, - ref replacementIndex, ref replacementLength, out unused); + result = GetResultForEnumPropertyValueOfDSCResource(completionContext, string.Empty, ref replacementIndex, ref replacementLength, out unused); } break; } diff --git a/test/powershell/Host/TabCompletion/BugFix.Tests.ps1 b/test/powershell/Host/TabCompletion/BugFix.Tests.ps1 index 1c9c3103c2..30429428d1 100644 --- a/test/powershell/Host/TabCompletion/BugFix.Tests.ps1 +++ b/test/powershell/Host/TabCompletion/BugFix.Tests.ps1 @@ -65,4 +65,21 @@ Describe "Tab completion bug fix" -Tags "CI" { $result.CompletionMatches[1].CompletionText | Should -Be 'Ascending' $result.CompletionMatches[2].CompletionText | Should -Be 'Descending' } + + It "'Get-Date | Sort-Object @{Expression=' should work without completion" { + $cmd = "Get-Date | Sort-Object @{Expression=" + $result = TabExpansion2 -inputScript $cmd -cursorColumn $cmd.Length + $result.CompletionMatches.Count | Should -Be 0 + } + + It "Issue#5322 - 'Get-Date | Sort-Object @{Expression=...;' should work" { + $cmd = "Get-Date | Sort-Object @{Expression=...;" + $result = TabExpansion2 -inputScript $cmd -cursorColumn $cmd.Length + $result.CurrentMatchIndex | Should -Be -1 + $result.ReplacementIndex | Should -Be 40 + $result.ReplacementLength | Should -Be 0 + $result.CompletionMatches[0].CompletionText | Should -Be 'Expression' + $result.CompletionMatches[1].CompletionText | Should -Be 'Ascending' + $result.CompletionMatches[2].CompletionText | Should -Be 'Descending' + } }