From 1452374fbf256f507f1c71cf0cb2241c9aa98ea0 Mon Sep 17 00:00:00 2001 From: MartinGC94 Date: Fri, 30 Aug 2024 18:11:07 +0200 Subject: [PATCH] Improve cim ETS member inference completion --- .../engine/parser/TypeInferenceVisitor.cs | 9 ++++++++- .../Host/TabCompletion/TabCompletion.Tests.ps1 | 5 +++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/parser/TypeInferenceVisitor.cs b/src/System.Management.Automation/engine/parser/TypeInferenceVisitor.cs index 96a72d3ff06..47e7357c292 100644 --- a/src/System.Management.Automation/engine/parser/TypeInferenceVisitor.cs +++ b/src/System.Management.Automation/engine/parser/TypeInferenceVisitor.cs @@ -195,7 +195,14 @@ internal IList GetMembersByInferredType(PSTypeName typename, bool isStat // Look in the type table first. if (!isStatic) { - var consolidatedString = new ConsolidatedString(new[] { typename.Name }); + // The Ciminstance type adapter adds the full typename with and without a namespace to the list of type names. + // So if we see one with a full typename we need to also get the types for the short version. + // For example: "CimInstance#root/standardcimv2/MSFT_NetFirewallRule" and "CimInstance#MSFT_NetFirewallRule" + Match match = Regex.Match(typename.Name, "(Microsoft\\.Management\\.Infrastructure\\.CimInstance#)(.+\\/)(.+$)"); + + ConsolidatedString consolidatedString = match.Success + ? new ConsolidatedString(new[] { typename.Name, match.Groups[1].Value + match.Groups[3].Value }) + : new ConsolidatedString(new[] { typename.Name }); results.AddRange(ExecutionContext.TypeTable.GetMembers(consolidatedString)); } diff --git a/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 b/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 index 22ff201c45a..86004f3fba5 100644 --- a/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 +++ b/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 @@ -672,6 +672,11 @@ ConstructorTestClass(int i, bool b) $res.CompletionMatches[0].CompletionText | Should -BeExactly Cat } + It 'Should complete cim ETS member added by shortname' -Skip:(!$IsWindows) { + $res = TabExpansion2 -inputScript '(Get-NetFirewallRule).Nam' + $res.CompletionMatches[0].CompletionText | Should -BeExactly 'Name' + } + It 'Should complete variable assigned with Data statement' { $TestString = 'data MyDataVar {"Hello"};$MyDatav' $res = TabExpansion2 -inputScript $TestString