-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Closed
Labels
Committee-ReviewedPS-Committee has reviewed this and made a decisionPS-Committee has reviewed this and made a decisionIssue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifIssue-Enhancementthe issue is more of a feature request than a bugthe issue is more of a feature request than a bugResolution-FixedThe issue is fixed.The issue is fixed.WG-Enginecore PowerShell engine, interpreter, and runtimecore PowerShell engine, interpreter, and runtime
Description
Add support for ArgumentCompletionAttributes with parameters
ArgumentCompleters are in the current implementation created by type, using a default constructor.
This makes it very hard to provide parameters to the completed, limiting their usefulness.
I propose that we add "Factory" support to the completion system, so that an attribute derived from ArgumentCompleterFactoryAttribute gets called on a method with a signature like
IArgumentCompleter Create();This would allow the derived attribute to use attribute parameters to create the actual argument completer.
Deriving from ArgumentCompleterFactoryAttribute makes it possible to create generic completers, like
[DirectoryCompleter(ContainingFile="pswh.exe", Depth=2)]
[DateCompleter(WeekDay='Monday', From="LastYear")]
[GitCommits(Branch='release')]An sample usage could look like this:
/// <summary>
/// Creates new number completions
/// </summary>
public class NumberCompletionsAttribute : ArgumentCompleterFactoryAttribute
{
private readonly int _from;
private readonly int _to;
private readonly int _step;
public NumberCompletionsAttribute(int from, int to, int step = 1)
{
_from = @from;
_to = to;
_step = step;
}
public IArgumentCompleter Create() => new NumberCompleter(_from, _to, _step);
}
public class NumberCompleter : IArgumentCompleter
private readonly int _from;
private readonly int _to;
private readonly int _step;
public NumberCompleter(int from, int to, int step)
{
_from = @from;
_to = to;
_step = step;
}
public IEnumerable<CompletionResult> CompleteArgument(string commandName, string parameterName, string wordToComplete, CommandAst commandAst, IDictionary fakeBoundParameters)
{
/// complete using the passed parameters
}
}Proposed technical implementation details (optional)
There are a couble of ways to go about this:
- New abstract class
ArgumentCompleterFactoryAttributewith an abstractCreatemethod. - New interface
IArgumentCompleterFactoryThat the attributes implement if they want the factory support. - Combine the above: Have
ArgumentCompleterFactoryAttributeimplementIArgumentCompleterFactory
Pull request for implementation can be found here: #12605
SeeminglyScience, SteveL-MSFT and ThomasNieto
Metadata
Metadata
Assignees
Labels
Committee-ReviewedPS-Committee has reviewed this and made a decisionPS-Committee has reviewed this and made a decisionIssue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifIssue-Enhancementthe issue is more of a feature request than a bugthe issue is more of a feature request than a bugResolution-FixedThe issue is fixed.The issue is fixed.WG-Enginecore PowerShell engine, interpreter, and runtimecore PowerShell engine, interpreter, and runtime