Skip to content

Support parameterized argument completers #12708

@powercode

Description

@powercode

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:

  1. New abstract class ArgumentCompleterFactoryAttribute with an abstract Create method.
  2. New interface IArgumentCompleterFactory That the attributes implement if they want the factory support.
  3. Combine the above: Have ArgumentCompleterFactoryAttribute implement IArgumentCompleterFactory

Pull request for implementation can be found here: #12605

Metadata

Metadata

Assignees

No one assigned

    Labels

    Committee-ReviewedPS-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 reclassifIssue-Enhancementthe issue is more of a feature request than a bugResolution-FixedThe issue is fixed.WG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions