Skip to content

Add an on snippet event to psEditor API #439

Open
@SeeminglyScience

Description

@SeeminglyScience

It'd be nice to have an event we could hook into when a snippet is used. It would be great if the
the ast of the snippet, the variables, and maybe snippet metadata like name was passed to the handler.
Alternatively (or additionally) allow editor commands to act like snippets (e.g. set "variables" to
tab to, etc.) This would enable dynamic snippets that can expand or shape the snippet based on command
output.

A very simple mock up:

/* VSCode snippet example */
"Comment Help with PowerShell Output": {
    "prefix": "CommentHelp",
    "body": [
        "<#",
        ".SYNOPSIS",
        "    ${Short description}",
        ".DESCRIPTION",
        "    ${Long description}",
        ".NOTES",
        "    Date: ${PowerShell1:Get-Date -Format MM-dd-yyyy}",
        ".LINK",
        "    ${PowerShell2:Get-Command -Module \\$MyInvocation.MyCommand.Module.Name}",
        "#>"
    ]
}
# PowerShell event handler.
$eventHandler = {
    param($snippetVariables)
    foreach ($variable in $snippetVariables) {
        if ($variable.Name.StartsWith('PowerShell')) {
            $script = [scriptblock]::Create($variable.Value)
            $output = & $script
            $psEditor.GetEditorContext().CurrentFile.InsertText($output, $variable.Range)
        }
    }
}
<#
.SYNOPSIS
    Short description
.DESCRIPTION
    Long description
.NOTES
    Date: 04-12-2017
.LINK
    Get-Function1
    Get-Function2
    Get-Function3
#>

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions