Skip to content

Commit 626a83b

Browse files
authored
Fix tab completions for a hash table (#6839)
Now working: - Get-Date | Sort-Object @{Expression=<tab> - Get-Date | Sort-Object @{Expression=...;<tab>
1 parent b43d3e5 commit 626a83b

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,16 @@ internal List<CompletionResult> GetResultHelper(CompletionContext completionCont
473473
replacementLength = 0;
474474
break;
475475

476+
case TokenKind.Semi:
477+
// Handle scenarios such as 'gci | Format-Table @{Label=...;<tab>'
478+
if (lastAst is HashtableAst)
479+
{
480+
result = GetResultForHashtable(completionContext);
481+
replacementIndex += 1;
482+
replacementLength = 0;
483+
}
484+
break;
485+
476486
case TokenKind.Number:
477487
// Handle scenarios such as Get-Process -Id 5<tab> || Get-Process -Id 5210, 3<tab> || Get-Process -Id: 5210, 3<tab>
478488
if (lastAst is ConstantExpressionAst &&
@@ -537,10 +547,15 @@ internal List<CompletionResult> GetResultHelper(CompletionContext completionCont
537547
completionContext.ReplacementLength = replacementLength = 0;
538548
result = GetResultForAttributeArgument(completionContext, ref replacementIndex, ref replacementLength);
539549
}
550+
else if (lastAst is HashtableAst hashTableAst && !(lastAst.Parent is DynamicKeywordStatementAst) && CheckForPendingAssignment(hashTableAst))
551+
{
552+
// Handle scenarios such as 'gci | Format-Table @{Label=<tab>' if incomplete parsing of the assignment.
553+
return null;
554+
}
540555
else
541556
{
542-
//
543-
// Handle auto completion for enum/dependson property of DSC resource,
557+
// Handle scenarios such as 'configuration foo { File ab { Attributes ='
558+
// (auto completion for enum/dependson property of DSC resource),
544559
// cursor is right after '=', '(' or '@('
545560
//
546561
// Configuration config
@@ -552,8 +567,7 @@ internal List<CompletionResult> GetResultHelper(CompletionContext completionCont
552567
// DependsOn=(|
553568
//
554569
bool unused;
555-
result = GetResultForEnumPropertyValueOfDSCResource(completionContext, string.Empty,
556-
ref replacementIndex, ref replacementLength, out unused);
570+
result = GetResultForEnumPropertyValueOfDSCResource(completionContext, string.Empty, ref replacementIndex, ref replacementLength, out unused);
557571
}
558572
break;
559573
}

test/powershell/Host/TabCompletion/BugFix.Tests.ps1

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,21 @@ Describe "Tab completion bug fix" -Tags "CI" {
6565
$result.CompletionMatches[1].CompletionText | Should -Be 'Ascending'
6666
$result.CompletionMatches[2].CompletionText | Should -Be 'Descending'
6767
}
68+
69+
It "'Get-Date | Sort-Object @{Expression=<tab>' should work without completion" {
70+
$cmd = "Get-Date | Sort-Object @{Expression="
71+
$result = TabExpansion2 -inputScript $cmd -cursorColumn $cmd.Length
72+
$result.CompletionMatches.Count | Should -Be 0
73+
}
74+
75+
It "Issue#5322 - 'Get-Date | Sort-Object @{Expression=...;' should work" {
76+
$cmd = "Get-Date | Sort-Object @{Expression=...;"
77+
$result = TabExpansion2 -inputScript $cmd -cursorColumn $cmd.Length
78+
$result.CurrentMatchIndex | Should -Be -1
79+
$result.ReplacementIndex | Should -Be 40
80+
$result.ReplacementLength | Should -Be 0
81+
$result.CompletionMatches[0].CompletionText | Should -Be 'Expression'
82+
$result.CompletionMatches[1].CompletionText | Should -Be 'Ascending'
83+
$result.CompletionMatches[2].CompletionText | Should -Be 'Descending'
84+
}
6885
}

0 commit comments

Comments
 (0)