Last active
October 30, 2018 01:10
-
-
Save TylerLeonhardt/5dd03629ee4d531e1baced39a10e7b29 to your computer and use it in GitHub Desktop.
An Editor Command for the PowerShell extension for VSCode that leverages https://gist.github.com/tylerl0706/3d1618572f7a424dab812e9cce5e3a15
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using namespace Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol | |
using namespace Microsoft.PowerShell.EditorServices.Protocol.Messages | |
function Read-InputPrompt { | |
param([string]$Prompt) | |
end { | |
$result = $psEditor. | |
Components. | |
Get([IMessageSender]).SendRequest( | |
[ShowInputPromptRequest]::Type, | |
[ShowInputPromptRequest]@{ | |
Name = $Prompt | |
Label = $Prompt | |
}, | |
$true). | |
Result | |
if (-not $result.PromptCanceled) { | |
$result.ResponseText | |
} | |
} | |
} | |
Register-EditorCommand -Name PowerShell.ConvertToVSCodeSnippet -DisplayName 'Convert selected text to a VSCode snippet' -ScriptBlock { | |
$Body = $psEditor.GetEditorContext().CurrentFile.GetText($psEditor.GetEditorContext().SelectedRange) | |
if ([string]::IsNullOrWhiteSpace($Body)) { | |
Write-Error -Message 'No text selected. Select some text and try again.' | |
return | |
} | |
$Name = '' | |
while ([string]::IsNullOrWhiteSpace($Name)) { | |
$Name = Read-InputPrompt 'What do you want to call your snippet?' | |
if ([string]::IsNullOrWhiteSpace($Name)) { | |
Write-Error -Message '''Name'' is mandatory. Please specify something for ''Name''' | |
} else { | |
break | |
} | |
} | |
$Prefix = Read-InputPrompt 'What would you like the prefix to be? (default is what you put for ''Name'')' | |
if(!$Prefix) { | |
$Prefix = $Name | |
} | |
$Scope = Read-InputPrompt 'What languages would you like this to apply to (comma separated)? (default is ''all languages'')' | |
$Description = Read-InputPrompt 'What is the description of the snippet? (default is none)' | |
$splat = @{ | |
Name = $Name | |
Body = $Body | |
Prefix = $Prefix | |
Scope = $Scope -split ',' | |
Description = $Description | |
} | |
$result = path/to/ConvertTo-VSCodeSnippet.ps1 @splat | |
# $page = @" | |
# // Place the following snippet inside of a `.code-snippets` file. For more information on snippets, please see: | |
# // PLACE_LINK_HERE | |
# $result | |
# "@ | |
# New-EditorFile -Value $page | |
$htmlContentView = New-VSCodeHtmlContentView -Title "'$Name' VSCode Snippet" | |
Set-VSCodeHtmlContentView -HtmlContentView $htmlContentView -HtmlBodyContent @" | |
<h1>VSCode Snippet Generator</h1> | |
<p>Place the following snippet inside of a <code>.code-snippets</code> file. (Between curly braces <code>{}</code> !)</p> | |
<p>For more information on snippets, please see the official docs on <a href="PLACE_LINK_HERE">VSCode snippets</a></p> | |
<br/> | |
<h2>'$Name' VSCode Snippet</h2> | |
<pre> | |
$result | |
</pre> | |
"@ | |
Show-VSCodeHtmlContentView $htmlContentView | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment