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
ArgumentCompleterFactoryAttribute with an abstract Create method.
- New interface
IArgumentCompleterFactory That the attributes implement if they want the factory support.
- Combine the above: Have
ArgumentCompleterFactoryAttribute implement IArgumentCompleterFactory
Pull request for implementation can be found here: #12605
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
ArgumentCompleterFactoryAttributegets called on a method with a signature likeThis 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
An sample usage could look like this:
Proposed technical implementation details (optional)
There are a couble of ways to go about this:
ArgumentCompleterFactoryAttributewith an abstractCreatemethod.IArgumentCompleterFactoryThat the attributes implement if they want the factory support.ArgumentCompleterFactoryAttributeimplementIArgumentCompleterFactoryPull request for implementation can be found here: #12605