Visual Studio: code snippets

As at work we might stop using ReSharper, I’ll assume that and come back to how you do Code Snippets, Visual Studio-style.

There’s this from MSDN which shows you how to create one.

This is how you can use it of course: you start to type letters and after some pre-defined pause it assumes you want to see the snippet library, in this case for all snippets starting with [p]:

VSSnippets01

Where has this come from?

VSSnippets02

VSSnippets03

Now interesting here is that e.g. the [propa] and [propdp] snippets are not in that list, so presumably it at least aggregates from a few different sources under the [C:\Program Files (x86)\Microsoft Visual Studio 14.0] area. So if I do try to find e.g. propdp,

VSSnippets05

, which expands to this promising code:VSSnippets04

I need to know the location and extension of a snippet, so from the Manager, I can get the location, giving this:

VSSnippets06

Notice that [Snippets] is below [VC#], rather than the other way round, which seems a bit mad to me. Regardless,I now know where to look for the propdp snippet:

C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC#\Snippets\1033\NetFX30

VSSnippets09

VSSnippets08

So that too is under the VC# umbrella, so maybe it is just grouping the C# bits together.

More MSDN [List of common C# snippets (.e.g. for properties)] [Create a New Snippet with Replacements] [Distribute Code Snippets]

Now my pet rant on C# code snippets vs ReSharper was the fact that you can’t do any on-the-fly substitution (see my YouTube elsewhere on what I mean by that) on macro variables. Fine, with a few extra keystrokes, you can achieve the necessary with the standard snippet [propfull]:

PropFull01 PropFull02

and in simple uses, this is probably enough (propg):

Propg01

Might be helpful to show what underpins the fuller one, propfull:

snippett01

Code

Advertisements

Snippets: C# console app

If I want to play with some ideas in C# that don’t involve a GUI but where you want some console output, then that’s obviously a use-case for the Console Application template in Visual Studio… but it’s not quite as I like. THIS is as I like:

ConsoleApp01

Pastebin

That then gives me a clear place in the RunTheTest method to new up stuff, and a ready-made prompt so the console doesn’t just run away after execution.

While I’m on it, this will save me a few keystrokes * 1000, as it’s easier to remove stuff doing this crude form of debugging than it is to add it. I always like to surround arguments in those square brackets for easy visual delimitation, better too many than too few:

ConsoleSnip01

Of course I should anyway go back to my Resharper Template notes here, and the related YouTube video here.

Editors: Visual Studio Code

One day, without recalling I had installed it, I saw I had Visual Studio Code on my box. It strikes me as a bit like Brackets, mentioned elsewhere in this blog.

VSCode01

and they also support Emmet:

VSCode02

My interest right now is in JavaScript, and while I’m at it snippets/templates to avoid typing more than I need to.

Some docs around this herehere and here.

Actually on snippets, it starts to get quite interesting – in Code, if you go File/Preferences/User Snippets, and then select HTML as the language, you get this:

UserSnippets01

So fairly self-documented. If I now remove the comments…

UserSnippets02

If in a piece of HTML, I now do ctrl-space to bring up the suggestions, this is what I see…

UserSnippets03

, and if I hit return I see this…

UserSnippets04

Hm, but in fact that is obviously JavaScript, so that is not a fat lot of help. But in fact it turns out that ALL the template files (e.g. for Docker as well, just as a random punt) have the same starting content.

So those templates anyway give us the principle, so let’s have a punt at that. I’ve hacked up this, which is the simplest possible HTML, but with a few variables thrown in, which get replaced at the point you use them (using ctrl-space to bring up the set):

UserSnippets05

If we now start out with a blank html page, thus…

UserSnippets06

, then do ctrl-space to get this…

UserSnippets07

, hit return to get this, with the cursor resting on ‘Title goes here’,

UserSnippets08

, start typing to replace that text, then hit tab to get to the next replacement (based on the $ variable you put in template), then you end up with this (but bear in mind because Emmet seems to be on by default, you can’t just press tab willy nilly)

UserSnippets08

These are the edits I used:

UserSnippets10

In Chrome with F12, you see this…

UserSnippets09

, which is fine in that a) the files don’t exist, but b) it has replaced the title with what I wanted. Also note that I have AutoSave on in Code, so upsides and downsides I guess, but it suits me.

And maybe finally, it is easy enough to escape double quotes:

UserSnippets11