PowerShellOrg / Plaster

Plaster is a template-based file and project generator written in PowerShell.
MIT License
719 stars 116 forks source link

Discussion: Editor Commands in Templates #195

Closed gerane closed 7 years ago

gerane commented 7 years ago

I will be including support for the PowerShell Extension's Editor Commands in my own template/templates. I use them constantly and it has become one of my favorite and most used features of VSCode. I would think it would be a nice option in the default template in hopes to help boost adoption of them.

It has been a little while since I have discussed this with @daviwil so I wanted to get another discussion going on the topic. Originally, the idea would be to add a EditorCommands.ps1 to your module and then add something like this to your psm1

if ($psEditor)
{
    . $PSScriptRoot\EditorCommands.ps1
}

This would offer a standardized way for modules to support built in Editor Commands that would autoload if running in PSES supported editors. I was curious to know if this was still the current model.

Another question would be how the new integrated terminal will impact the read-host work arounds that all of my examples are currently using since those will break. I think that is likely more of an issue for PSES, but I think it is somewhat related if you want to include an example Editor Command in a template.

I literally have a hard time functioning without these anymore, and am always trying to spread my excitement about them. Honestly, it has made trying to use the ISE for anything more than a simple task frustrating for me now.

rkeithhill commented 7 years ago

I would think it would be a nice option in the default template in hopes to help boost adoption of them.

Well, given that the module template for 1.0 is going to be a simple module template, I don't think we want to mix in editor commands. Again, think of the "average" PowerShell user who I'm told doesn't get Git and all these tools. :-) I don't think the majority of those users are going to be interested in editor commands.

That said, we can absolutely ship either a template and/or snippet that shows how to get started with editor commands.

gerane commented 7 years ago

@rkeithhill oops, yeah, I should have clarified this would be for a more advanced template That was why I excluded it from the other Discussion example.

What have you been doing with Snippets so far Keith? Have you shared the ones in your Summit demo anywhere yet? Those looked awesome. Would those ship with the PowerShell Extension when it gets support for Plaster?

That reminds me, I have a little side project I kind of want to run by you regarding VSCode snippets. Maybe you could help me figure out how to handle something with it. Basically, you highlight some code, trigger an Editor Command, and it creates a snippet for you. Just enter your have the snippet tab stops and variables in the code you highlighted to integrate those. Could never settle on a way to update the user's snippet .json properly without blowing out all of their comments etc.

rkeithhill commented 7 years ago

What have you been doing with Snippets so far Keith?

The PowerShell extension for VSCode comes with a decent set of snippets that I worked on but they are generic (for, try, tryf, etc).

you shared the ones in your Summit demo anywhere yet?

Not yet. Perhaps they should be a VSCode Plaster snippet extension. Most folks using the PowerShell extension for VSCode won't be writing Plaster templates but for those that do, it would be nice to be able to pull down a VSCode extension with those snippets.

creates a snippet for you

Cool idea. You could even build this as a VSCode extension for this. Regarding the issue with comments, you could try to always insert just before the closing } (if the file exists). Then you only need to determine if a previous snippet exists to determine whether you need to stick in a comma (don't you just love JSON). You should be able to do that by preprocessing the file contents and using a regex to strip out all the comments. Then load the resulting text via ConvertFrom-Json to see if there are existing snippets.

gerane commented 7 years ago

@rkeithhill yeah, I was trying to work something out with a json library, but I am just not familiar with them enough to know if anyone of them support so form of comments like the voodoo they are doing. I have this working with the built in json cmdlets and newtonsoft, but I just couldn't find anyway to preserver those comments. I was thinking about adding this to my VSCodeExtensions module, but even using a regex replace, I was weary of it breaking some weird case I hadn't thought of.

You can see how it worked at the end of this gif. I had this in a Demo I gave at a user group and used Editor commands to power the demo, so I had a gif made from when I was making that.

editordemo

I tried to make a Helper Extension for it as well, and store the new snippets there, but the problem is snippets from an extension are only loaded at the start of the session. Only the user snippets are loaded immediately on save.

rkeithhill commented 7 years ago

OK got a chance to do some debugging. The PSEditor variable will be available in the constrained runspace IFF the PowerShell host where the template is invoked has PSEditor defined.