Skip to content

Commit 0a11f79

Browse files
authored
Add property assignment completion for enums (PowerShell#19178)
* Add property assignment completion for enums * Move method
1 parent 6055a82 commit 0a11f79

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,24 @@ private static bool TryGetTypeConstraintOnVariable(
14211421
return typeConstraint != null || setConstraint != null;
14221422
}
14231423

1424+
private static List<CompletionResult> CompletePropertyAssignment(MemberExpressionAst memberExpression, CompletionContext context)
1425+
{
1426+
if (SafeExprEvaluator.TrySafeEval(memberExpression, context.ExecutionContext, out var evalValue))
1427+
{
1428+
if (evalValue is null)
1429+
{
1430+
return null;
1431+
}
1432+
1433+
Type type = evalValue.GetType();
1434+
if (type.IsEnum)
1435+
{
1436+
return GetResultForEnum(type, context);
1437+
}
1438+
}
1439+
return null;
1440+
}
1441+
14241442
private static bool TryGetCompletionsForVariableAssignment(
14251443
CompletionContext completionContext,
14261444
AssignmentStatementAst assignmentAst,
@@ -1452,6 +1470,12 @@ bool TryGetResultForSet(Type typeConstraint, ValidateSetAttribute setConstraint,
14521470
return false;
14531471
}
14541472

1473+
if (assignmentAst.Left is MemberExpressionAst member)
1474+
{
1475+
completions = CompletePropertyAssignment(member, completionContext);
1476+
return completions is not null;
1477+
}
1478+
14551479
completions = null;
14561480

14571481
// Try to get the variable from the assignment, plus any type constraint on it

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,11 @@ using `
378378
$res.CompletionMatches[0].CompletionText | Should -BeExactly '${PSVersionTable}'
379379
}
380380

381+
It 'Should work for property assignment of enum type:' {
382+
$res = TabExpansion2 -inputScript '$psstyle.Progress.View="Clas'
383+
$res.CompletionMatches[0].CompletionText | Should -Be '"Classic"'
384+
}
385+
381386
It 'Should work for variable assignment of enum type: <inputStr>' -TestCases @(
382387
@{ inputStr = '$ErrorActionPreference = '; filter = ''; doubleQuotes = $false }
383388
@{ inputStr = '$ErrorActionPreference='; filter = ''; doubleQuotes = $false }

0 commit comments

Comments
 (0)