From 965dc8f40fd8a42d32ae1589d917f295fa9a7fb3 Mon Sep 17 00:00:00 2001 From: MartinGC94 <42123497+MartinGC94@users.noreply.github.com> Date: Wed, 10 Nov 2021 03:26:14 +0100 Subject: [PATCH] Fix tab completion within the script block specified for the `ValidateScriptAttribute`. (#14550) --- .../engine/CommandCompletion/CompletionAnalysis.cs | 11 ++++++++--- .../Host/TabCompletion/TabCompletion.Tests.ps1 | 8 ++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs b/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs index 76be64188..18bd6cbed 100644 --- a/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs +++ b/src/System.Management.Automation/engine/CommandCompletion/CompletionAnalysis.cs @@ -1813,9 +1813,14 @@ namespace System.Management.Automation } } } - - result = GetResultForAttributeArgument(completionContext, ref replacementIndex, ref replacementLength); - if (result != null) return result; + if (completionContext.TokenAtCursor.TokenFlags == TokenFlags.MemberName) + { + result = GetResultForAttributeArgument(completionContext, ref replacementIndex, ref replacementLength); + if (result is not null) + { + return result; + } + } if ((tokenAtCursor.TokenFlags & TokenFlags.CommandName) != 0) { diff --git a/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 b/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 index 67aa4bb59..6513bd872 100644 --- a/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 +++ b/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 @@ -1097,6 +1097,14 @@ Describe "TabCompletion" -Tags CI { $entry.CompletionText | Should -BeExactly "Mandatory" } + It "Test Attribute scriptblock completion" { + $inputStr = '[ValidateScript({Get-Child})]$Test=ls' + $res = TabExpansion2 -inputScript $inputStr -cursorColumn ($inputStr.IndexOf('}')) + $res.CompletionMatches | Should -HaveCount 1 + $entry = $res.CompletionMatches | Where-Object CompletionText -EQ "Get-ChildItem" + $entry.CompletionText | Should -BeExactly "Get-ChildItem" + } + It "Test completion with line continuation" { $inputStr = @' dir -Recurse `