KEMBAR78
Provide base class or extend interface for Argument Completers · Issue #25033 · PowerShell/PowerShell · GitHub
Skip to content

Provide base class or extend interface for Argument Completers #25033

@ArmaanMcleod

Description

@ArmaanMcleod

Summary of the new feature / enhancement

Related to PowerShell/PowerShell-RFC#269

We should provide a base class for Argument completers, so it is easier for users to create completers in C#.

Current State

  • User implements IArgumentCompleter interface.
  • User implements CompleteArgument method.
  • User has to write custom matching logic and handle quotes themselves.
  • Internal PowerShell completers need to call like static methods CompletionCompleters.GetMatchingResults, which is also repeated in every completer. This method's behaviour is also still in the internal API and not exposed for general use.

Proposed State

  • User implements derived Argument completer class from base abstract class ArgumentCompleter.
  • User only needs to implement abstract method GetPossibleCompletionValues(). Has the option to also override ToolTipMapping, ListItemTextMapping or CompletionResultType.
  • Abstract class implements CompleteArgument method. This cannot be overridden since it sets properties on base class and calls GetMatchingResults, which can be overridden since its behaviour can change.
  • Make sure base class is public and can be consumed by anyone writing completers in C#. I am not sure what assembly this can go into but it makes sense for base class to remain in core code so our internal completers can use it as well.
  • Internal PowerShell completers can also derive from this base class and become simplified.

Other considerations

  • Current base class example I've created does not include broad support for escaping; we'd probably need a way to include that in base class since its very cumbersome to do that yourself.
  • Ensure base class does not become fragile, but I suspect this won't be an issue if it exposes the right things and allows core completion functionality to be overridden in derived classes. I'd expect that once the base class is finalised and has flexibility with options it would probably not require much change.
  • Extend interface to include this functionality and provide default methods.

Proposed technical implementation details (optional)

Proposed Base Class - Option 1

using System.Collections;
using System.Collections.Generic;
using System.Management.Automation.Language;

namespace System.Management.Automation
{
    public abstract class ArgumentCompleter : IArgumentCompleter
    {
        // Readonly properties which are visible in derived classes
        protected string CommandName { get; private set; }
        protected string ParameterName { get; private set; }
        protected string WordToComplete { get; private set; }
        protected CommandAst CommandAst { get; private set; }
        protected IDictionary FakeBoundParameters { get; private set; }
        
        // User can optionally override these
        protected virtual bool ShouldComplete { get; set; } = true;
        protected virtual CompletionResultType CompletionResultType { get; set; } = CompletionResultType.Text;
        protected virtual Func<string, string> ToolTipMapping { get; set; }
        protected virtual Func<string, string> ListItemTextMapping { get; set; }
        protected virtual bool EscapeGlobbingPath { get; set; }

        // User must implement this
        protected abstract IEnumerable<string> GetPossibleCompletionValues();

        // Default CompleteArgument implementation
        // Cannot be overriden since it sets properties on base class and calls GetMatchingResults which can be overriden
        public IEnumerable<CompletionResult> CompleteArgument(
            string commandName,
            string parameterName,
            string wordToComplete,
            CommandAst commandAst,
            IDictionary fakeBoundParameters)
        {
            CommandName = commandName;
            ParameterName = parameterName;
            WordToComplete = wordToComplete;
            CommandAst = commandAst;
            FakeBoundParameters = fakeBoundParameters;
            return ShouldComplete ? GetMatchingResults(wordToComplete) : [];
        }

        // Default matching with quote handling implementation encapsulated in base class
        // Can be overriden if default behaviour needs to change
        protected virtual IEnumerable<CompletionResult> GetMatchingResults(string wordToComplete)
        {
            string quote = CompletionCompleters.HandleDoubleAndSingleQuote(ref wordToComplete);

            foreach (string value in GetPossibleCompletionValues())
            {
                if (value.StartsWith(wordToComplete, StringComparison.OrdinalIgnoreCase))
                {
                    string completionText = QuoteCompletionText(completionText: value, quote, EscapeGlobbingPath);

                    string toolTip = ToolTipMapping?.Invoke(value) ?? value;
                    string listItemText = ListItemTextMapping?.Invoke(value) ?? value;

                    yield return new CompletionResult(completionText, listItemText, CompletionResultType, toolTip);
                }
            }
        }

        // Quote escaping which is probably fine to start out with but can be expanded later
        private static string QuoteCompletionText(string completionText, string quote, bool escapeGlobbingPath)
        {
            if (CompletionCompleters.CompletionRequiresQuotes(completionText, escapeGlobbingPath))
            {
                string quoteInUse = string.IsNullOrEmpty(quote) ? "'" : quote;

                completionText = quoteInUse == "'"
                    ? completionText.Replace("'", "''")
                    : completionText.Replace("`", "``").Replace("$", "`$");

                if (escapeGlobbingPath)
                {
                    completionText = quoteInUse == "'"
                        ? completionText.Replace("[", "`[").Replace("]", "`]")
                        : completionText.Replace("[", "``[").Replace("]", "``]");
                }

                return quoteInUse + completionText + quoteInUse;
            }

            return quote + completionText + quote;
        }
    }
}

Proposed extension of Interface - Option 2

    public interface IArgumentCompleter
    {
        /// <summary>
        /// Implementations of this function are called by PowerShell to complete arguments.
        /// </summary>
        /// <param name="commandName">The name of the command that needs argument completion.</param>
        /// <param name="parameterName">The name of the parameter that needs argument completion.</param>
        /// <param name="wordToComplete">The (possibly empty) word being completed.</param>
        /// <param name="commandAst">The command ast in case it is needed for completion.</param>
        /// <param name="fakeBoundParameters">
        /// This parameter is similar to $PSBoundParameters, except that sometimes PowerShell cannot or
        /// will not attempt to evaluate an argument, in which case you may need to use <paramref name="commandAst"/>.
        /// </param>
        /// <returns>
        /// A collection of completion results, most like with <see cref="CompletionResult.ResultType"/> set to
        /// <see cref="CompletionResultType.ParameterValue"/>.
        /// </returns>
        IEnumerable<CompletionResult> CompleteArgument(
            string commandName,
            string parameterName,
            string wordToComplete,
            CommandAst commandAst,
            IDictionary fakeBoundParameters)
        {
            CommandName = commandName;
            ParameterName = parameterName;
            WordToComplete = wordToComplete;
            CommandAst = commandAst;
            FakeBoundParameters = fakeBoundParameters;
            return ShouldComplete ? GetMatchingResults(wordToComplete) : [];
        }

        /// <summary>
        /// Gets the name of the command that needs argument completion.
        /// </summary>
        protected static string? CommandName { get; private set; }

        /// <summary>
        /// Gets the name of the parameter that needs argument completion.
        /// </summary>
        protected static string? ParameterName { get; private set; }

        /// <summary>
        /// Gets the word being completed.
        /// </summary>
        protected static string? WordToComplete { get; private set; }

        /// <summary>
        /// Gets the command abstract syntax tree (AST).
        /// </summary>
        protected static CommandAst? CommandAst { get; private set; }

        /// <summary>
        /// Gets the fake bound parameters similar to $PSBoundParameters.
        /// </summary>
        protected static IDictionary? FakeBoundParameters { get; private set; }

        /// <summary>
        /// Gets value indicating whether to perform completion.
        /// </summary>
        protected bool ShouldComplete => true;

        /// <summary>
        /// Gets the type of the completion result.
        /// </summary>
        protected CompletionResultType CompletionResultType => CompletionResultType.Text;

        /// <summary>
        /// Gets the mapping function for tooltips.
        /// </summary>
        protected Func<string, string>? ToolTipMapping => null;

        /// <summary>
        /// Gets the mapping function for list item texts.
        /// </summary>
        protected Func<string, string>? ListItemTextMapping => null;

        /// <summary>
        /// Gets value indicating whether to escape globbing paths.
        /// </summary>
        protected bool EscapeGlobbingPath => false;

        /// <summary>
        /// Gets the possible completion values.
        /// </summary>
        protected IEnumerable<string> PossibleCompletionValues => [];

        /// <summary>
        /// Matches the possible completion values against the word to complete.
        /// </summary>
        /// <param name="wordToComplete">The word to complete, which is used as a pattern for matching possible values.</param>
        /// <returns>An <see cref="IEnumerable{CompletionResult}"/> containing the matching completion results.</returns>
        /// <remarks>This method handles different variations of completions, including considerations for quotes and escaping globbing paths.</remarks>
        protected IEnumerable<CompletionResult> GetMatchingResults(string wordToComplete)
        {
            string quote = CompletionCompleters.HandleDoubleAndSingleQuote(ref wordToComplete);

            foreach (string value in PossibleCompletionValues)
            {
                if (value.StartsWith(wordToComplete, StringComparison.OrdinalIgnoreCase))
                {
                    string completionText = QuoteCompletionText(completionText: value, quote, EscapeGlobbingPath);

                    string toolTip = ToolTipMapping?.Invoke(value) ?? value;
                    string listItemText = ListItemTextMapping?.Invoke(value) ?? value;

                    yield return new CompletionResult(completionText, listItemText, CompletionResultType, toolTip);
                }
            }
        }

        /// <summary>
        /// Quotes the completion text.
        /// </summary>
        /// <param name="completionText">The text to complete.</param>
        /// <param name="quote">The quote to use.</param>
        /// <param name="escapeGlobbingPath">True if the globbing path needs to be escaped; otherwise, false.</param>
        /// <returns>
        /// A quoted string if quoting is necessary.
        /// </returns>
        private static string QuoteCompletionText(string completionText, string quote, bool escapeGlobbingPath)
        {
            if (CompletionCompleters.CompletionRequiresQuotes(completionText, escapeGlobbingPath))
            {
                string quoteInUse = string.IsNullOrEmpty(quote) ? "'" : quote;

                completionText = quoteInUse == "'"
                    ? completionText.Replace("'", "''")
                    : completionText.Replace("`", "``").Replace("$", "`$");

                if (escapeGlobbingPath)
                {
                    completionText = quoteInUse == "'"
                        ? completionText.Replace("[", "`[").Replace("]", "`]")
                        : completionText.Replace("[", "``[").Replace("]", "``]");
                }

                return quoteInUse + completionText + quoteInUse;
            }

            return quote + completionText + quote;
        }

    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    In-PRIndicates that a PR is out for the issueIssue-Enhancementthe issue is more of a feature request than a bugNeeds-TriageThe issue is new and needs to be triaged by a work group.WG-Enginecore PowerShell engine, interpreter, and runtimeWG-NeedsReviewNeeds a review by the labeled Working Group

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions