Here at the office we have a published coding standard and one of the most neglected parts of the standard is the file header. It is jokingly referred to as Server Cozy Metadata. As in:
DBA: “These files don’t pass, missing header.”
Grizzled Architect: “What does that mean?”
Young Gun: “I think he means something like this…”
Grizzled Architect: “Christ, next we’ll be knitting server cozies for the SQL boxes.”
Smart Ass: “Don’t forget your server cozy metadata!”
I am awful at complying with this standard. I decided to leverage some tools in VS that I had, to date, chosen not to leverage. I was aware of Code Snippet tools in VS but had not had any luck utilizing them in the past with out using a complicated series of keyboard shortcuts. The documentation said that pressing tab should fill out the data, but all I kept ending up with is the name of the snippet, not the contents. It turns out the key to using Snippets is tab, tab. That is tab twice. I had failed to read that second tab on a number of occasions. I felt stupid.
So here is what our server cozy meta data looks like for a C# file:
//================================================================ // $LastChangedBy: $ // // $HeadURL: $ // // $Revision: $ // // $LastChangedDate: $ // // Copyright (c) 2009 Some Company. All rights reserved. //================================================================
All of the $SomeText: $ fields are SVN Replacement fields that our source control system will update on commit.
And here is what the snippet file that you are going to create and save on your desktop as SVNHeader.snippet looks like:
<?xml version="1.0" encoding="utf-8"?> <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <CodeSnippet Format="1.0.0"> <Header> <Title> SVN AutoFill Header </Title> <Shortcut>svnHeader</Shortcut> <SnippetTypes> <SnippetType>Expansion</SnippetType> </SnippetTypes> </Header> <Snippet> <Code Language="CSharp" Delimiter="^" Kind="any"> <![CDATA[//================================================================ // $LastChangedBy: $ // // $HeadURL: $ // // $Revision: $ // // $LastChangedDate: $ // // Copyright (c) 2009 Some Company. All rights reserved. //================================================================ ]]> </Code> </Snippet> </CodeSnippet> </CodeSnippets>
Making your snippet available:
- In Visual Studio go to Tools > Code Snippet Manger, or if you’d rather press ctrl+k, ctrl + b (the complicated keyboard shortcuts).
- Select C# from your language dropdown.
- Click Import.
- Find the file you just saved, select it and click Open.
- Check the folders you want your snippet to live in (I just chose “My Code Snippets”).
- Click Finish.
To consume your snippet in your C# code file start typing SVNHeader and once it is selected in intellisense, press TAB TWICE.
There is a lot of flexibility and power built into snippets. Check this out to see what else you can do with them.
I have also set up a similar thing in SQL Server Management Studio 2005 with SQL templates for my Stored Procedure, Index and Table scripts so that the Server Cozy Metadata is automatically included and I don’t have to worry about copy and pasting the code or modifying my template on accident.
p.s. I am still planning on finishing the P/Invoke Any CPU article and eventually posting the .Net thread synchronization article. I am talking about the C# P/Invoke 64-bit stuff on 3/12 at the Indy .Net Developers Association C# SIG. Hopefully I’ll finish that article while preparing for that event and be able to post it afterwards.
Comments