Add property assignment completion for enums (#19178)

* Add property assignment completion for enums

* Move method
This commit is contained in:
MartinGC94
2023-02-23 13:42:57 -08:00
committed by GitHub
parent 6055a82f6b
commit 0a11f799dc
2 changed files with 29 additions and 0 deletions
@@ -1421,6 +1421,24 @@ namespace System.Management.Automation
return typeConstraint != null || setConstraint != null;
}
private static List<CompletionResult> CompletePropertyAssignment(MemberExpressionAst memberExpression, CompletionContext context)
{
if (SafeExprEvaluator.TrySafeEval(memberExpression, context.ExecutionContext, out var evalValue))
{
if (evalValue is null)
{
return null;
}
Type type = evalValue.GetType();
if (type.IsEnum)
{
return GetResultForEnum(type, context);
}
}
return null;
}
private static bool TryGetCompletionsForVariableAssignment(
CompletionContext completionContext,
AssignmentStatementAst assignmentAst,
@@ -1452,6 +1470,12 @@ namespace System.Management.Automation
return false;
}
if (assignmentAst.Left is MemberExpressionAst member)
{
completions = CompletePropertyAssignment(member, completionContext);
return completions is not null;
}
completions = null;
// Try to get the variable from the assignment, plus any type constraint on it
@@ -378,6 +378,11 @@ using `
$res.CompletionMatches[0].CompletionText | Should -BeExactly '${PSVersionTable}'
}
It 'Should work for property assignment of enum type:' {
$res = TabExpansion2 -inputScript '$psstyle.Progress.View="Clas'
$res.CompletionMatches[0].CompletionText | Should -Be '"Classic"'
}
It 'Should work for variable assignment of enum type: <inputStr>' -TestCases @(
@{ inputStr = '$ErrorActionPreference = '; filter = ''; doubleQuotes = $false }
@{ inputStr = '$ErrorActionPreference='; filter = ''; doubleQuotes = $false }