Improve dynamic parameter tab completion (#17661)

This commit is contained in:
MartinGC94
2022-07-18 15:32:06 -07:00
committed by GitHub
parent 56bab81fc7
commit 53adf5d5d4
2 changed files with 35 additions and 11 deletions
@@ -1235,21 +1235,32 @@ namespace System.Management.Automation.Language
}
else
{
var dash = _commandElements[commandIndex] as StringConstantExpressionAst;
if (dash != null && dash.Value.Trim().Equals("-", StringComparison.OrdinalIgnoreCase))
object valueToAdd;
ExpressionAst expressionToAdd;
if (_commandElements[commandIndex] is ConstantExpressionAst constant)
{
if (constant.Extent.Text.Equals("-", StringComparison.Ordinal))
{
// A value of "-" is most likely the user trying to tab here,
// and we don't want it be treated as an argument
continue;
}
valueToAdd = constant.Value;
expressionToAdd = constant;
}
else if (_commandElements[commandIndex] is ExpressionAst expression)
{
valueToAdd = expression.Extent.Text;
expressionToAdd = expression;
}
else
{
// "-" is represented by StringConstantExpressionAst. Most likely the user type a tab here,
// and we don't want it be treated as an argument
continue;
}
var expressionArgument = _commandElements[commandIndex] as ExpressionAst;
if (expressionArgument != null)
{
argumentsToGetDynamicParameters?.Add(expressionArgument.Extent.Text);
_arguments.Add(new AstPair(null, expressionArgument));
}
argumentsToGetDynamicParameters?.Add(valueToAdd);
_arguments.Add(new AstPair(null, expressionToAdd));
}
}
}
@@ -510,6 +510,19 @@ ConstructorTestClass(int i, bool b)
$res.CompletionMatches[0].CompletionText | Should -BeExactly Cat
}
it 'Should complete provider dynamic parameters with quoted path' {
$Script = if ($IsWindows)
{
'Get-ChildItem -Path "C:\" -Director'
}
else
{
'Get-ChildItem -Path "/" -Director'
}
$res = TabExpansion2 -inputScript $Script
$res.CompletionMatches[0].CompletionText | Should -BeExactly '-Directory'
}
Context "Format cmdlet's View paramter completion" {
BeforeAll {
$viewDefinition = @'