From 7d29d39195f3e7b6d96bd8f84b8e35b34983057f Mon Sep 17 00:00:00 2001 From: MartinGC94 Date: Sun, 19 Feb 2023 15:36:03 +0100 Subject: [PATCH 1/2] Add property assignment completion for enums --- .../CommandCompletion/CompletionAnalysis.cs | 24 +++++++++++++++++++ .../TabCompletion/TabCompletion.Tests.ps1 | 5 ++++ 2 files changed, 29 insertions(+) diff --git a/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs b/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs index cc7ba18284a..57f78d1df3a 100644 --- a/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs +++ b/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs @@ -1349,6 +1349,24 @@ private static Tuple GetHashEntryContainsCursor( return keyValuePairWithCursor; } + private static List 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; + } + // Pulls the variable out of an assignment's LHS expression // Also brings back the innermost type constraint if there is one private static VariableExpressionAst GetVariableFromExpressionAst( @@ -1452,6 +1470,12 @@ bool TryGetResultForSet(Type typeConstraint, ValidateSetAttribute setConstraint, 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 diff --git a/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 b/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 index 3ed99228d7a..4fedc40b5bf 100644 --- a/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 +++ b/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 @@ -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: ' -TestCases @( @{ inputStr = '$ErrorActionPreference = '; filter = ''; doubleQuotes = $false } @{ inputStr = '$ErrorActionPreference='; filter = ''; doubleQuotes = $false } From 936fa694a2942b759b4934e0c8e0399845b70417 Mon Sep 17 00:00:00 2001 From: MartinGC94 Date: Wed, 22 Feb 2023 16:55:28 +0100 Subject: [PATCH 2/2] Move method --- .../CommandCompletion/CompletionAnalysis.cs | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs b/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs index 57f78d1df3a..9315880138a 100644 --- a/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs +++ b/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs @@ -1349,24 +1349,6 @@ private static Tuple GetHashEntryContainsCursor( return keyValuePairWithCursor; } - private static List 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; - } - // Pulls the variable out of an assignment's LHS expression // Also brings back the innermost type constraint if there is one private static VariableExpressionAst GetVariableFromExpressionAst( @@ -1439,6 +1421,24 @@ private static bool TryGetTypeConstraintOnVariable( return typeConstraint != null || setConstraint != null; } + private static List 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,