Fix issue when completing the first command in a script with an empty array expression (#18355) (#18630)

Co-authored-by: MartinGC94 <42123497+MartinGC94@users.noreply.github.com>
This commit is contained in:
Travis Plunk
2022-11-21 10:25:06 -08:00
committed by GitHub
co-authored by MartinGC94
parent 645249b845
commit fb46702bbc
2 changed files with 9 additions and 1 deletions
@@ -1986,7 +1986,10 @@ namespace System.Management.Automation
// Handle completion for a path with variable, such as: $PSHOME\ty<tab>
if (completionContext.RelatedAsts.Count > 0 && completionContext.RelatedAsts[0] is ScriptBlockAst)
{
Ast cursorAst = completionContext.RelatedAsts[0].FindAll(ast => ast.Extent.EndOffset <= tokenAtCursor.Extent.StartOffset, true).LastOrDefault();
Ast cursorAst = completionContext.RelatedAsts[0].FindAll(
ast => ast.Extent.EndOffset <= tokenAtCursor.Extent.StartOffset
&& ast.Extent is not EmptyScriptExtent,
searchNestedScriptBlocks: true).LastOrDefault();
if (cursorAst is not null)
{
@@ -759,6 +759,11 @@ ConstructorTestClass(int i, bool b)
$res.CompletionMatches.CompletionText | Should -Contain "-Path"
}
it 'Should complete command with an empty arrayexpression element' {
$res = TabExpansion2 -inputScript 'Get-ChildItem @()' -cursorColumn 1
$res.CompletionMatches[0].CompletionText | Should -Be "Get-ChildItem"
}
Context "Script name completion" {
BeforeAll {
Setup -f 'install-powershell.ps1' -Content ""