Visual Studio Productivity Macros.

Tags: vs2010, visual studio, productivity, macro, refactoring

As I blogged about, I love the concept of productivity tools but find the practicalities impossible to live with.  So I started to build the features I find particularly useful as Visual Studio Macros.

Download VS2010 Macro Project.

All of the macros are available for download here as a VS Macro solution (which includes source), I have stopped providing text only versions but if you want those back let me know via a comment on the blog...


I have created these for my own personal use and while I hope they work for you I don't promise anything.  They are tested for Visual Studio 2010, C# using the 1TBS brace positioning style I personally use. If you use another language / style etc. then please let me know any feedback but dont be shocked to the core if they dont work as intended.  

If you want to take a look at the new ones I'm working on check the development page.


Macro: Current Class To New File.

“CurrentClassToNewFile” is a refactoring macro which extracts the class at the cursor position and places it into a new file in the solution.  The new file will be created with the filename of the class.  If a file with this name already exists then the file will have a number post fixed on the end until it is unique.

Since version the macro will work on interfaces and will take with it any doccomments that are above the class/interface in the source file.

Known Limitations:

Does not add using statements to the new file.  (Workaround using ctrl-.)
Does not place the new file in the same position in the heirachy.  (Workaround by dragging the file)


Interface support added, thanks to Kev.
Will now take doccomments with the class in question.


Macro: Duplicate Line At Cursor.

“DuplicateLineAtCursor” is a macro to duplicate the current line where the cursor is.  The line is duplicated (i.e. not affecting the clipboard) to the line before the current position of the cursor.

Macro: Auto Validate Strings In Current Method

“AutoValidateStringsInCurrentMethod” is a macro to place validation into the start of the current method, as defined by the cursor position.  Parameters which are string types will have checks for null placed in a region within the code.  These null checks will throw a ArgumentNull exception with the name of the parameter if the parameter is null. 

The “AutoValidateStringsInCurrentMethodWithLength” macro is identical in functionality except that it also includes a secondary check for the length of the string not being zero.  This is not done with IsNullOrEmpty because I personally like to throw a null exception and an out of range exception separately.

Known Limitations:

Re-running the macros on the same method will cause duplicate validation stages.