Skip to content

Commit 25895e3

Browse files
daxian-dbwiSazonov
authored andcommitted
Fix NullReferenceException in CompletionCompleters public methods (#11274)
1 parent 5c7b72f commit 25895e3

2 files changed

Lines changed: 29 additions & 13 deletions

File tree

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4592,7 +4592,7 @@ internal static List<CompletionResult> CompleteVariable(CompletionContext contex
45924592
var wordToComplete = context.WordToComplete;
45934593
var colon = wordToComplete.IndexOf(':');
45944594

4595-
var lastAst = context.RelatedAsts.Last();
4595+
var lastAst = context.RelatedAsts?.Last();
45964596
var variableAst = lastAst as VariableExpressionAst;
45974597
var prefix = variableAst != null && variableAst.Splatted ? "@" : "$";
45984598

@@ -6009,19 +6009,22 @@ internal static List<CompletionResult> CompleteType(CompletionContext context, s
60096009
}
60106010

60116011
// this is a temporary fix. Only the type defined in the same script get complete. Need to use using Module when that is available.
6012-
var scriptBlockAst = (ScriptBlockAst)context.RelatedAsts[0];
6013-
var typeAsts = scriptBlockAst.FindAll(ast => ast is TypeDefinitionAst, false).Cast<TypeDefinitionAst>();
6014-
foreach (var typeAst in typeAsts.Where(ast => pattern.IsMatch(ast.Name)))
6012+
if (context.RelatedAsts != null && context.RelatedAsts.Count > 0)
60156013
{
6016-
string toolTipPrefix = string.Empty;
6017-
if (typeAst.IsInterface)
6018-
toolTipPrefix = "Interface ";
6019-
else if (typeAst.IsClass)
6020-
toolTipPrefix = "Class ";
6021-
else if (typeAst.IsEnum)
6022-
toolTipPrefix = "Enum ";
6014+
var scriptBlockAst = (ScriptBlockAst)context.RelatedAsts[0];
6015+
var typeAsts = scriptBlockAst.FindAll(ast => ast is TypeDefinitionAst, false).Cast<TypeDefinitionAst>();
6016+
foreach (var typeAst in typeAsts.Where(ast => pattern.IsMatch(ast.Name)))
6017+
{
6018+
string toolTipPrefix = string.Empty;
6019+
if (typeAst.IsInterface)
6020+
toolTipPrefix = "Interface ";
6021+
else if (typeAst.IsClass)
6022+
toolTipPrefix = "Class ";
6023+
else if (typeAst.IsEnum)
6024+
toolTipPrefix = "Enum ";
60236025

6024-
results.Add(new CompletionResult(prefix + typeAst.Name + suffix, typeAst.Name, CompletionResultType.Type, toolTipPrefix + typeAst.Name));
6026+
results.Add(new CompletionResult(prefix + typeAst.Name + suffix, typeAst.Name, CompletionResultType.Type, toolTipPrefix + typeAst.Name));
6027+
}
60256028
}
60266029

60276030
results.Sort((c1, c2) => string.Compare(c1.ListItemText, c2.ListItemText, StringComparison.OrdinalIgnoreCase));
@@ -6030,7 +6033,10 @@ internal static List<CompletionResult> CompleteType(CompletionContext context, s
60306033

60316034
private static string GetNamespaceToRemove(CompletionContext context, TypeCompletionBase completion)
60326035
{
6033-
if (completion is NamespaceCompletion) { return null; }
6036+
if (completion is NamespaceCompletion || context.RelatedAsts == null || context.RelatedAsts.Count == 0)
6037+
{
6038+
return null;
6039+
}
60346040

60356041
var typeCompletion = completion as TypeCompletion;
60366042
string typeNameSpace = typeCompletion != null

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ Describe "Tab completion bug fix" -Tags "CI" {
3434
$result.CompletionMatches[2].CompletionText | Should -BeExactly "-NoOverwrite"
3535
}
3636

37+
It "Issue#11227 - [CompletionCompleters]::CompleteVariable and [CompletionCompleters]::CompleteType should work" {
38+
$result = [System.Management.Automation.CompletionCompleters]::CompleteType("CompletionComple")
39+
$result.Count | Should -BeExactly 1
40+
$result[0].CompletionText | Should -BeExactly 'System.Management.Automation.CompletionCompleters'
41+
42+
$result = [System.Management.Automation.CompletionCompleters]::CompleteVariable("errorAction")
43+
$result.Count | Should -BeExactly 1
44+
$result[0].CompletionText | Should -BeExactly '$ErrorActionPreference'
45+
}
46+
3747
Context "Issue#3416 - 'Select-Object'" {
3848
BeforeAll {
3949
$DatetimeProperties = @((Get-Date).psobject.baseobject.psobject.properties) | Sort-Object -Property Name

0 commit comments

Comments
 (0)