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 @@ -4581,7 +4581,7 @@ internal static List<CompletionResult> CompleteVariable(CompletionContext contex
var wordToComplete = context.WordToComplete;
var colon = wordToComplete.IndexOf(':');

var lastAst = context.RelatedAsts.Last();
var lastAst = context.RelatedAsts?.Last();
var variableAst = lastAst as VariableExpressionAst;
var prefix = variableAst != null && variableAst.Splatted ? "@" : "$";

Expand Down Expand Up @@ -5998,19 +5998,22 @@ internal static List<CompletionResult> CompleteType(CompletionContext context, s
}

// this is a temporary fix. Only the type defined in the same script get complete. Need to use using Module when that is available.
var scriptBlockAst = (ScriptBlockAst)context.RelatedAsts[0];
var typeAsts = scriptBlockAst.FindAll(ast => ast is TypeDefinitionAst, false).Cast<TypeDefinitionAst>();
foreach (var typeAst in typeAsts.Where(ast => pattern.IsMatch(ast.Name)))
if (context.RelatedAsts != null && context.RelatedAsts.Count > 0)
{
string toolTipPrefix = string.Empty;
if (typeAst.IsInterface)
toolTipPrefix = "Interface ";
else if (typeAst.IsClass)
toolTipPrefix = "Class ";
else if (typeAst.IsEnum)
toolTipPrefix = "Enum ";
var scriptBlockAst = (ScriptBlockAst)context.RelatedAsts[0];
var typeAsts = scriptBlockAst.FindAll(ast => ast is TypeDefinitionAst, false).Cast<TypeDefinitionAst>();
foreach (var typeAst in typeAsts.Where(ast => pattern.IsMatch(ast.Name)))
{
string toolTipPrefix = string.Empty;
if (typeAst.IsInterface)
toolTipPrefix = "Interface ";
else if (typeAst.IsClass)
toolTipPrefix = "Class ";
else if (typeAst.IsEnum)
toolTipPrefix = "Enum ";

results.Add(new CompletionResult(prefix + typeAst.Name + suffix, typeAst.Name, CompletionResultType.Type, toolTipPrefix + typeAst.Name));
results.Add(new CompletionResult(prefix + typeAst.Name + suffix, typeAst.Name, CompletionResultType.Type, toolTipPrefix + typeAst.Name));
}
}

results.Sort((c1, c2) => string.Compare(c1.ListItemText, c2.ListItemText, StringComparison.OrdinalIgnoreCase));
Expand All @@ -6019,7 +6022,10 @@ internal static List<CompletionResult> CompleteType(CompletionContext context, s

private static string GetNamespaceToRemove(CompletionContext context, TypeCompletionBase completion)
{
if (completion is NamespaceCompletion) { return null; }
if (completion is NamespaceCompletion || context.RelatedAsts == null || context.RelatedAsts.Count == 0)
{
return null;
}

var typeCompletion = completion as TypeCompletion;
string typeNameSpace = typeCompletion != null
Expand Down
10 changes: 10 additions & 0 deletions test/powershell/Host/TabCompletion/BugFix.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ Describe "Tab completion bug fix" -Tags "CI" {
$result.CompletionMatches[2].CompletionText | Should -BeExactly "-NoOverwrite"
}

It "Issue#11227 - [CompletionCompleters]::CompleteVariable and [CompletionCompleters]::CompleteType should work" {
$result = [System.Management.Automation.CompletionCompleters]::CompleteType("CompletionComple")
$result.Count | Should -BeExactly 1
$result[0].CompletionText | Should -BeExactly 'System.Management.Automation.CompletionCompleters'

$result = [System.Management.Automation.CompletionCompleters]::CompleteVariable("errorAction")
$result.Count | Should -BeExactly 1
$result[0].CompletionText | Should -BeExactly '$ErrorActionPreference'
}

Context "Issue#3416 - 'Select-Object'" {
BeforeAll {
$DatetimeProperties = @((Get-Date).psobject.baseobject.psobject.properties) | Sort-Object -Property Name
Expand Down