Just A Little Visual Studio Time Saver


If you use the Visual Studio wizard for the UnitTest framework, you’ll find your test code littered with these helpful reminders:

// TODO: Initialize to an appropriate value

And by “littered” I mean there are hundreds, if not thousands, of these comments cluttering your code – several on every test method. I quickly tired of manually removing them one by one, but they are useful reminders so I don’t want to do a global search-and-delete. Instead, I wrote a macro to to make it a one-click job. Unfortunately, this will only work on VS2010 or earlier, since the Microsoft boys decided to remove macros from 2012 & 2013. 😦

Here’s the script:

Sub Delete_TODO_Text()
  'save the current location, to return here if text not found
  Dim objActive As VirtualPoint = DTE.ActiveDocument.Selection.ActivePoint
  Dim iCol As Integer = objActive.DisplayColumn
  Dim iLine As Integer = objActive.Line

  'search for text on current line only
  DTE.ActiveDocument.Selection.EndOfLine(True)
  DTE.Find.FindWhat = "// TODO: Initialize to an appropriate value"
  DTE.Find.Action = vsFindAction.vsFindActionFind
  DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocumentSelection
  If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then
    DTE.ActiveDocument.Selection.Collapse()
    DTE.ActiveDocument.Selection.MoveTo(iLine, iCol, False) 'return to original position
  Else
    DTE.ActiveDocument.Selection.Delete()
  End If
  DTE.StatusBar.Clear()
End Sub

After adding the script to a module in MyMacros, I assigned it to a keyboard shortcut. Now all I have to do is navigate to each line on which the comment appears, initialize the corresponding object appropriately, then click my keyboard shortcut to activate the macro, deleting the comment from the end of the line. (The macro has some additional logic to avoid any side-effects if I accidentally activate it while on a line that doesn’t contain the comment. I had to do some selection trickery to search for the comment text only on the current line.)

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s