Skip to content

Using System.CommandLine for powershell completers #1220

Open
@powercode

Description

@powercode

When writing a completer for an existing executable, the PowerShell API requires the return of an IEnumerable<CompletionResult>. It is a somewhat richer model, where you can provide more info than just a string, such as tooltips.

For example, completing git a gives me documentation as a tooltip.
image

However, the suggestion APIs are using string as its hard-coded data type, making this kind of rich completion more difficult.

Sure, I can encode the info as json, or have null-separated fields, but it adds complexity.

What do you think of adding a more generic api, a IEnumerable<T> GetSuggestion<T>()?

Edit:
More info.

In PowerShell, Native commands are completed by a script like this:

{
    param($wordToComplete, $commandAst, $cursorPosition)

   [SampleCompleter]::CompleteInput($wordToComplete, $commandAst, $cursorPosition)
}
public static class SampleCompleter{
   public static IEnumerable<CompletionResult> CompleteInput(string wordToComplete, CommandAst commandAst, int cursorPosition) {
       return GetOptions().Where(o=>o.CompletionText.StartsWith(wordTocomplete);
   }

   IEnumerable<CompletionResult> GetOptions(){
       yield return new CompletionResult("-a", "-a", CompletionResultType.ParameterName, "tooltip for a");
       yield return new CompletionResult("-b", "-b", CompletionResultType.ParameterName, "tooltip for b");
   }
}

The nice thing would be to use System.CommandLine to get the suggestions, but as CompletionResult instead of string.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-CompletionsRelated to support for tab completionenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions