PowerShellEditorServices icon indicating copy to clipboard operation
PowerShellEditorServices copied to clipboard

Feature Request: Editor Commands with parameter input

Open gerane opened this issue 8 years ago • 5 comments

These would hopefully work similar to the ext commands.

"command param" and then it auto populates intellisense autocomplete in the options. This would be something I would see as an end goal. At first, it would be nice to see positional parameters sort of like this:

"command param1 param2 param3"

You could go for these working similar to powershell, or make them more limited similar to the ext commands, where they take only 1 param, but give you intellisense style choices.

gerane avatar May 13 '16 01:05 gerane

I like this idea, will look into how to make it happen.

daviwil avatar May 13 '16 02:05 daviwil

The way that $host.ui.PromptForChoice works is another potential way for this to work. If using that specific method, I would suggest building tooling around it so it is straight forward and easy to use.

I wanted to add my sort of rough example based on Trevor Sullivan's emoji module.

function Get-CommandChoice
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory=$true)]
        [Array]$Array,

        [Parameter(Mandatory=$true)]
        [string]$caption,

        [Parameter(Mandatory=$true)]
        [string]$Message,

        [int]$DefaultChoice = 0
    )

    $Choices = [System.Management.Automation.Host.ChoiceDescription[]] @($Array)
    $Choice = $host.ui.PromptForChoice($Caption,$Message, $Choices,$DefaultChoice)

    Return $Array[$Choice]
}

function Invoke-EmojiList {
    param([Microsoft.PowerShell.EditorServices.Extensions.EditorContext]$context)
    $Names = (Get-Command -name Get-Emoji).Parameters['Name'].Attributes.ValidValues
    $caption = 'Please select an Emoji'
    $message = ' '

    $Name = Get-CommandChoice -Array $Names -caption $Caption -Message $Message

    $Emoji = Get-Emoji -name $Names[$Name]
    $context.CurrentFile.InsertText($Emoji, $context.CursorPosition)
}

Register-EditorCommand `
    -Name 'Emojis.EmojiFromList' `
    -DisplayName 'Inserts an Emoji from List' `
    -Function Invoke-EmojiList

If helper functions or apis are provided, it should bring more adoption because there will be less of a learning curve and less work to add the integration. I think it is important to focus on making this as friendly as possible since a large number of Powershell user's do not have developer backgrounds. PowerShell might be their introduction to these sorts of developer tools, as it was for me(well, batch files were my first taste).

There could be potential to set up a sort of framework for certain types of functions to just be designated to be auto integrated. Have some form of Editor export command that designates the commands to be auto converted as an Editor Command. Pull messages from command help or use the command + param name as the messages and intellisense options from Get-Command ValidValues or similar areas of Get-Command. This could either be done by only supporting single param functions, or by looping through each param 1 at a time.

My example won't work currently due to a bug, but wanted to expand on a few of my ideas here.

gerane avatar May 13 '16 19:05 gerane

@daviwil I was looking at my older issues and saw this one. Are you still planning on adding a more built in way to handle this, or sticking with prompt for choice sort of like how it is now.

gerane avatar Jan 31 '17 16:01 gerane

Not sure, I'll have to think about it some more when I've got more time to look at it.

daviwil avatar Jan 31 '17 16:01 daviwil

Understood. I know we discussed some of this in the past, was just interested if you had put anymore thought into it since then.

gerane avatar Jan 31 '17 16:01 gerane