Wednesday, October 29, 2014

Creating a custom snippet in Visual Studio 2013

Bleh. Checklist-simple procedure that I'm tired of re-researching.

First, reference material from MSDN

Altering an Existing Snippet

If you want to alter an existing snippet to make it line up better with your workflow:

  1. Ctrl+K, Ctrl+B (or Tools >Code Snippets Manager)
  2. Find the target snippet
  3. Highlight the "Location:" field above and copy to clipboard.
  4. Open >File...
  5. Paste the snippet location
  6. File >Save (filename) as...
  7. Browse to your snippet repo (you do have someplace safe to store these as 'source' documents right? these are productivity enablers after all)
  8. Name it appropriately (Custom usually works well)
  9. Proceed to the next section.

Build Your Own

If you're creating something from (mostly) whole cloth... well, don't reinvent the wheel. Follow the steps above but for the target select an existing snippet that does the same kind of thing you want (scaffolding with fill-in templating like the 'if' snippet, or pure expansion, or whatever). That way you have all the details already written and existing syntax to build from. XML documents can be particular and there's no reason to waste your time learning all the special syntax for code snippets. You've more productive things to do than become a productivity tool expert.

Editing

A few words on editing your custom snippet XML. 
Header is where you'll put metadata like author, description, etc. Make sure these accurately reflect what you're doing, and don't try to blame "Microsoft Corporation" for your hacked-together XML drivel.
Double check sections like and because you don't want your snippet to accidentally import libraries that the final code won't require (the 'testm' snippet for example references some unit test libraries that won't be required if you aren't creating unit test code)
Declarations is where you'll set up user input fields and IDE-ready type references (testm is a good snippet to see how these work)
Indents are quite tricky in my experience. They seem to be literal with the first line beginning at the start or the shortcut text in the editor. You'll probably have to play around with it (see importing below). I can't seem to get it to properly indent based on the indent level the shortcut begins at. However it works properly for the built-in 'if' snippet so clearly it's possible. It must be a personal failing.

Importing

  1. Open the snippet manager again (Ctrl+K, Ctrl+B)
  2. Click Import...
  3. Browse to the XML file and select it.
  4. Pick a folder to drop it in. May I suggest the pre-existing "My Code Snippets" folder?
  5. Hit OK and OK.
  6. Test it. Inevitably it's broken in some way. Identify the problem (google can be your friend but more likely for something this esoteric the MSDN doc above will be more useful).
  7. Make the necessary edits and re-import. When you're asked how to handle the conflict just overwrite.
I thought the last time I did this there was a way to edit the snippet in place but I've forgotten how I did that. Possibly by opening the snippet in something other than VS (it's pure XML so most XML editing programs should handle it just fine) but you might have to re-open VS before the changes take effect. Frankly I'm not going to devote any more time to it right now; I'll update the post if I learn better later.

No comments: