Posted in Editor, Powershell, Snippets, vscode

Distraction for Today – Snippets for VSCode

Current project for the PowerShell VSCode project is opening the Snippets up to the community and for the community. The was announced in the VSCode channel in the Slack group, a resource I strongly recommend.

I have taken the great base snippet file Keith Hill provided in the early days of using VSCode as my PowerShell editor and converted my ISE snippets from the XML file. I can’t complain as I had a useful collection of code snippets for ISE but looking at the format and capabilities that VSCode provided, I quickly started adding my snippets to the base collection Keith Hill provided.

I have one that I like to include in my End statement of a script. As it stood it was usable but was lacking. Watching the community post snippets I saw a chance to take this little nugget and make it better. The code simply starts a remove-variable so I can clean up variables before the script finishes and  then initiate a garbage collection via .Net. Below is what the snippet looked like when I used in my code:

 END {
    Remove-Variable -Name varA, varB, anotherVar
    [System.GC]::Collect()
}

I call the snippet and the code is added, including the tabs and carriage returns. The text area after the Name parameter is where the cursor goes because of  ${1:variablestoberemoved} but that is far as the Snippet automation would go. So I created an empty JSON file and started with the snippet as I had entered it. Then I progressed with fixing and adding some pieces to the snippet. I would then take the new version of just that snippet and replaced the one in the PowerShell.json file, then in a blank script file I would use the prefix and check the progress.

// Original
“Initiate Garbage Collection – Original”: {
            “prefix”: “freememory”,
            “body”:[
                “Remove-Variable -Name \\$variablestoberemoved\r”,
                “[System.GC]::Collect()\r”
            ],
            “description”: “Free up memory when script completes”
    },
    // Now let’s write that with a “tabable”
    “Initiate Garbage Collection – Next”: {
            “prefix”: “freememory”,
            “body”:[
                “Remove-Variable -Name \\${1:variablestoberemoved}\r”,
                “[System.GC]::Collect()\r”
            ],
            “description”: “Free up memory when script completes”
    // That sort of worked but after I enter the variables I want to table
    // tap out of the code because this is done
    },
    // What I finally came up with
    “Initiate Garbage Collection”: {
        “prefix”: “freememory”,
        “body”:[
            “Remove-Variable -Name ${1:variablestoberemoved}\r”,
            “[System.GC]::Collect()\r”,
            “$0”
        ],
        “description”: “Free up memory when script completes”
    }
}
So through the progression testing, I can call the snippet, it will take me to the “variablestoberemoved” enter the variables, comma seperated, that I know will still exist, then I can hit tab will take me to the line after my snippet text. Looks like:
2018-04-26_09-23-33
Quick use of a PowerShell snippet in Visual Studio Code

Author:

USMC, CVID, Technology, PowerShell... just general ramblings

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s