Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static Tuple<Ast, Token[], IScriptPosition> MapStringInputToParsedInput(s
/// <returns></returns>
public static CommandCompletion CompleteInput(string input, int cursorIndex, Hashtable options)
{
if (input == null)
if (input == null || input.Length == 0)
{
return s_emptyCommandCompletion;
}
Expand Down Expand Up @@ -126,6 +126,11 @@ public static CommandCompletion CompleteInput(Ast ast, Token[] tokens, IScriptPo
throw PSTraceSource.NewArgumentNullException(nameof(positionOfCursor));
}

if (ast.Extent.Text.Length == 0)
{
return s_emptyCommandCompletion;
}

return CompleteInputImpl(ast, tokens, positionOfCursor, options);
}

Expand All @@ -141,7 +146,7 @@ public static CommandCompletion CompleteInput(Ast ast, Token[] tokens, IScriptPo
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "powershell")]
public static CommandCompletion CompleteInput(string input, int cursorIndex, Hashtable options, PowerShell powershell)
{
if (input == null)
if (input == null || input.Length == 0)
{
return s_emptyCommandCompletion;
}
Expand Down Expand Up @@ -231,6 +236,11 @@ public static CommandCompletion CompleteInput(Ast ast, Token[] tokens, IScriptPo
throw PSTraceSource.NewArgumentNullException(nameof(powershell));
}

if (ast.Extent.Text.Length == 0)
{
return s_emptyCommandCompletion;
}

// If we are in a debugger stop, let the debugger do the command completion.
var debugger = powershell.Runspace?.Debugger;
if ((debugger != null) && debugger.InBreakpoint)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ internal static AstAnalysisContext ExtractAstContext(Ast inputAst, Token[] input
ast => IsCursorWithinOrJustAfterExtent(positionForAstSearch, ast.Extent),
searchNestedScriptBlocks: true).ToList();

if (relatedAsts.Count == 0)
{
relatedAsts.Add(inputAst);
}

Comment thread
MartinGC94 marked this conversation as resolved.
// If the last ast is an unnamed block that starts with "param" the cursor is inside a param block.
// To avoid adding special handling to all the completers that look at the last ast, we remove it here because it's not useful for completion.
if (relatedAsts[^1].Extent.Text.StartsWith("param", StringComparison.OrdinalIgnoreCase)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4044,6 +4044,7 @@ internal void ImportCmdletsFromAssembly(Assembly assembly, PSModuleInfo module)
[OutputType([System.Management.Automation.CommandCompletion])]
Param(
[Parameter(ParameterSetName = 'ScriptInputSet', Mandatory = $true, Position = 0)]
[AllowEmptyString()]
[string] $inputScript,

[Parameter(ParameterSetName = 'ScriptInputSet', Position = 1)]
Expand Down Expand Up @@ -4081,7 +4082,7 @@ internal void ImportCmdletsFromAssembly(Assembly assembly, PSModuleInfo module)
<#options#> $options)
}
}
";
";

/// <summary>
/// This is the default function to use for clear-host.
Expand Down
8 changes: 8 additions & 0 deletions test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1997,6 +1997,14 @@ dir -Recurse `
param($inputStr, $expected)
$inputStr | Should -Throw -ErrorId $expected
}

It "Should not throw errors in tab completion with empty input string" {
{[System.Management.Automation.CommandCompletion]::CompleteInput("", 0, $null)} | Should -Not -Throw
}

It "Should not throw errors in tab completion with empty input ast" {
{[System.Management.Automation.CommandCompletion]::CompleteInput({}.Ast, @(), {}.Ast.Extent.StartScriptPosition, $null)} | Should -Not -Throw
}
}

Context "DSC tab completion tests" {
Expand Down