Skip to content

Commit ca5fc9c

Browse files
AndrewGasparlzybkr
authored andcommitted
Fix tab completion of native parameters (#2919)
Native argument completers were not invoked when the argument was a single dash. The fix is to treat unbound command parameters as command arguments for the purposes of parameter/argument completion.
1 parent a042dc1 commit ca5fc9c

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,10 +566,11 @@ internal static List<CompletionResult> CompleteCommandParameter(CompletionContex
566566

567567
PseudoBindingInfo pseudoBinding = new PseudoParameterBinder()
568568
.DoPseudoParameterBinding(commandAst, null, parameterAst, PseudoParameterBinder.BindingType.ParameterCompletion);
569-
// The command cannot be found or it's not a cmdlet, not a script cmdlet, not a function
569+
// The command cannot be found or it's not a cmdlet, not a script cmdlet, not a function.
570+
// Try completing as if it the parameter is a command argument for native command completion.
570571
if (pseudoBinding == null)
571572
{
572-
return result;
573+
return CompleteCommandArgument(context);
573574
}
574575

575576
switch (pseudoBinding.InfoType)

test/powershell/Language/Parser/ExtensibleCompletion.Tests.ps1

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,38 @@ Describe "Test extensible completion of native commands" -Tags "CI" {
229229
} | Get-CompletionTestCaseData | Test-Completions
230230
}
231231

232+
Describe "Test completion of parameters for native commands" -Tags "CI" {
233+
Register-ArgumentCompleter -Native -CommandName foo -ScriptBlock {
234+
Param($wordToComplete)
235+
236+
@("-dir", "-verbose", "-help", "-version") |
237+
Where-Object {
238+
$_ -match "$wordToComplete*"
239+
} |
240+
ForEach-Object {
241+
[CompletionResult]::new($_, $_, [CompletionResultType]::ParameterName, $_)
242+
}
243+
}
244+
245+
@{
246+
ExpectedResults = @(
247+
@{CompletionText = "-version"; ResultType = "ParameterName"}
248+
@{CompletionText = "-verbose"; ResultType = "ParameterName"}
249+
@{CompletionText = "-dir"; ResultType = "ParameterName"}
250+
@{CompletionText = "-help"; ResultType = "ParameterName"}
251+
)
252+
TestInput = 'foo -'
253+
} | Get-CompletionTestCaseData | Test-Completions
254+
255+
@{
256+
ExpectedResults = @(
257+
@{CompletionText = "-version"; ResultType = "ParameterName"}
258+
@{CompletionText = "-verbose"; ResultType = "ParameterName"}
259+
)
260+
TestInput = 'foo -v'
261+
} | Get-CompletionTestCaseData | Test-Completions
262+
}
263+
232264
Describe "Test extensible completion of using namespace" -Tags "CI" {
233265
@{
234266
ExpectedResults = @(

0 commit comments

Comments
 (0)