vscode-powershell icon indicating copy to clipboard operation
vscode-powershell copied to clipboard

Need auto splat extractor implemented in base PowerShell vscode extension

Open EklipZgit opened this issue 5 years ago • 6 comments

Summary of the new feature

Should be able to right click (or keyboard shortcut) over a long commandlet call in the editor, and extract the parameters into a splat. This is already implemented in an outside tool, and is one of the most important tools we use in our 100,000 line of PowerShell code codebase. I'm sure you're already familiar with it, implemented by SeeminglyScience over in https://github.com/SeeminglyScience/EditorServicesCommandSuite

To argue my case: one of the main things in PowerShell that is both a blessing and a curse is its verbosity. It is incredibly clear what you are trying to do in any given script both because of its high-level-brevity (one line does what most languages need 10 for), and because of named parameters. However, the named parameters leads to very long lines of code sometimes. This is why splatting is such a necessity. However, when writing a splat from scratch, you don't get ANY autocomplete (as you are simply adding keys to a hashtable), so the only time people would START with a splat is when they are already very familiar with the parameters of the command they are about to call. However when unfamiliar or not 100% confident, you would use autocomplete every time and end up with a long line of parameters. The time comes to commit the code, and you want to clean it up for readability.

Part of every developers workflow at this point OUGHT to be converting these long lines to splats for readability (and later extensability, as well), as there is no performance downside (at least in ps 5.1, cannot speak confidently for newer versions). In practice, this is a lot of work and often people just don't do it. It isn't particularly easy to do quickly by hand in the editor, even if you lifehack it by Ctrl+D'ing on -'s, newlining, and then wrapping with a hashtable.

SeeminglySciences splat tool combined with adding a Ctrl+Shift+C to map to additional powershell commands { "key": "ctrl+shift+c", "command": "PowerShell.ShowAdditionalCommands", "when": "editorLangId == 'powershell'" }, saves the day here on this critical missing feature.

Unfortunately, it breaks all the time due to changes to the VSCode extension. It is hard / confusing to set up (have to edit your VSCode PowerShell profile to auto-import the EditorSuite on load).

I'm baffled that such a critical component to powershell isn't already implemented in the base PowerShell extension. Either auto-complete of parameters in a splat hashtable or extraction of splat from a commandlet call are in my eyes a necessity for the powershell community.

Proposed technical implementation details (optional)

This feature in particular should be integrated into the extension and be accessible via a single keyboard shortcut, if possible. Perhaps ideally it should leave your cursor multi-selecting both the splat variable name and the splat usage (minus the $/@), such that you can rename it from the default quickly just by typing the new name.

EklipZgit avatar Jun 23 '20 17:06 EklipZgit

I'm working on essentially rewriting the dependency (or rather the parts I use) that keeps introducing breaking changes. That should make it significantly more reliable, but it's taking some time. As for difficulty in setting up, yeah it's way too complicated. If you want to see improvements in that area, give #2537 a thumbs up please. Ideally ESCS would just be another extension you install, nothing else.

In case it doesn't go without saying, I have no issues if PSES adds it's own version. That said, there's a lot more that goes into making it actually work well than you'd think. The code in ESCS isn't really portable either, it depends on a significant chunk of ESCS infrastructure that can't really just be ripped out and pasted into PSES. Adding a feature like this could be a pretty sizable amount of extra code that in all likelihood needs to be manually tested every release. I'm not necessarily advocating against it's inclusion, but considering the amount of maintenance it requires it's not unlikely that you'd see a similar level of stability.

SeeminglyScience avatar Jun 23 '20 18:06 SeeminglyScience

Hi is there any update regarding this feature request?

skest3qc avatar Nov 26 '21 13:11 skest3qc

ESCS hasn't broken in a while, so you can still get the feature there. Ideally ESCS could be shipped as an extension which would make getting it set up significantly less cumbersome. For that to happen though, an API would need to be added to vscode-powershell.

Afaik there are no current plans for a separate built in implementation, but it is marked up for grabs.

SeeminglyScience avatar Nov 26 '21 14:11 SeeminglyScience

@SeeminglyScience Oo...that could be a cool future project.

andyleejordan avatar Nov 30 '21 21:11 andyleejordan

@andschwa We talked about platting request like this one on the PsConfEU23 last week.

When writing an script you start by typing commands with IntelliSense and then when the cmdlet line is get really long a option to easily splat the command like right click would be useful. And IntelliSense on a hashtable does not really look like a usefull of easy option to build.

JulienFloris avatar Jun 28 '23 13:06 JulienFloris

@JulienFloris we talked about this today and all agreed it would be an awesome feature to have! Unfortunately there are some challenges to implementation that make it not a simple/quick win....we will keep this as "up for grabs" for now but will consider if we can re-prioritize it

SydneyhSmith avatar Jun 28 '23 18:06 SydneyhSmith