|
I blog when I have time and have something important to share, but I don't always have time to blog everything important I have to share (that's what articles and books are for I guess)...sometimes I blog just to hook you up to content references for events I've been part of...but today...I'm just blogging to vent my frustration with code snippet management in Visual Studio 2005. Aaarrrrghhhh...
Don't get me wrong, code snippets rock for productivity, but creating your own and getting them to freaking appear in the right place seems like a feat of great energy and time that I really do not have...sigh...but I took the time anyways because I figure if I just spend 2 hours (or 7200 seconds) now to get it working, then spend and extra 5 minutes (or 300 seconds) creating snippets for my repeatable code...it will pay off after I use about 200 snippets. Assuming I use about 3 code snippets per code sample, and that I create several code samples per day for my WCF book, not to mention other code samples and vertical slices for clients...I should be able to recover this time about 20 days time or less. Of course, I'm not factoring in time to write this blog...
So here goes my experience creating code snippets:
Creating new XML config snippets, no problem at all. Just copied an existing code snippet sample and away we go! Off to a good start.
Adding the code snippet to the Code Snippet Manager. Well first of all, figuring out what Add... versus Import... mean in that dialog is a pain. No, I didn't take the time to read an article about it...and frankly I expected it to be pretty easy to figure out. Well it seemed like sometimes Add worked, sometimes it didn't, but I seem to have finally figured out that you are supposed to select a directory that holds the code snippet type for the language you are adding folders to.
So, I created a WCFSnippets folder with two subdirectories:
\WCFCode -> holds my C# WCF code snippets
\WCFConfig -> holds my XML WCF code snippets for configuration
Then, from the code snippet manager, you select XML for the language and click Add. What you are supposed to do is find the folder with your XML snippets, so I navigate to \WCFConfig...but don't just navigate to that directory, drill all the way into the directory where the snippets are stored before you select the directory. Here are the screenshots: wrong, right and result...respectively.



For C# or VB.NET snippets, just select the right language, and drill into the directory where those are located.
Extra crap in my code snippets intellisense...make no "sense". I know the above seems really super easy...and it is once you know where to navigate and how to organize your stuff. Still, I had all kinds of extra crap left over in my snippet manager, and in my code snippet intellisense...let me explain.
After trying Add/Import, I was left with extra folders in my code snippet manager, that somehow didn't reflect all the snippets I was creating...it was nuts...I was going nuts...maybe I am nuts...anyways it turns out that Import is not really necessary if you have added your snippets folder according to the Add method I explained. Any new snippets you create in those custom folders will now appear in the magical snippets list. But, if you want to copy (import) a snippet from somewhere and put it into one of your folders (or, one of the predefined snippet folders for VS) you can Import.
If I navigate to my WCF configuration snippets folder and select a snippet, I will be presented with the already existing XML folders (preexisting or resulting from Add). You decide where you want the snippet to go and a copy is made there. Watch out for dupes!


The bugger of it is, you can't remove snippets. You can add them, but they will never go away...that is unless you delete the actual snippet from the file folder. So much for snippet management. You can remove folders, just not those living in the VS snippet folder, that's too official to remove...
Things to watch out for.
1. Beware not to add your snippet directories more than once. Or not to add the parent directory, only to decide on the child directory as in my WCFSnippets and WCFConfiguration example earlier. What happens? Well, somewhere out in snippet configuration land (a lost land that I cannot find) there is memory of every (wrong) directory you ever added using Add...and you can't freaking get rid of it. Well...I'm sure you can, but I care not spend more time to figure out why...perhaps someone else can tell me!!!

2. Make sure you put the correct language in your snippet code. If you call it XML but it's really C# (spelled csharp in snippet terms) you won't see it appear in your C# snippets after all...another ghost I chased...for a short while.
Sample snippet...for your time...more to come :)
<?xml version="1.0" encoding="utf-8" ?> <CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <Header> <Title>system.serviceModel.diagnostics</Title> <Author>Michele Leroux Bustamante</Author> <Shortcut>serviceModel.diagnostics</Shortcut> <Description>Sample system.serviceModel.diagnostics section.</Description> <SnippetTypes> <SnippetType>Expansion</SnippetType> </SnippetTypes> </Header> <Snippet> <Declarations> <Literal> <ID>perfCounters</ID> <Default>true</Default> <ToolTip>Enable performance counters: true|false</ToolTip> </Literal> <Literal> <ID>wmiProvider</ID> <Default>true</Default> <ToolTip>Enable WMI provider: true|false</ToolTip> </Literal> <Literal> <ID>logEntire</ID> <Default>false</Default> <ToolTip>Log entire message content (beware! production performance hit): true|false</ToolTip> </Literal> <Literal> <ID>logMalformed</ID> <Default>true</Default> <ToolTip>Log malformed messages (or, provide your own logging mechanism, don't do both!): true|false</ToolTip> </Literal> <Literal> <ID>logServiceLevel</ID> <Default>false</Default> <ToolTip>Log messages at service level: true|false</ToolTip> </Literal> <Literal> <ID>logTransportLevel</ID> <Default>true</Default> <ToolTip>Log messages at transport level: true|false</ToolTip> </Literal> <Literal> <ID>maxLog</ID> <Default>100000</Default> <ToolTip>Maximum message log count (runs out fast, must backup the file!)</ToolTip> </Literal> </Declarations> <Code Language="XML" Format="CData"> <![CDATA[ <diagnostics performanceCountersEnabled="$perfCounters$" wmiProviderEnabled="$wmiProvider$" > <messageLogging logEntireMessage="$logEntire$" logMalformedMessages="$logMalformed$" logMessagesAtServiceLevel="$logServiceLevel$" logMessagesAtTransportLevel="$logTransportLevel$" maxMessagesToLog="$maxLog$" /> </diagnostics>]]> </Code> </Snippet> </CodeSnippet>
|