65.9K
CodeProject is changing. Read more.
Home

Just A Little Visual Studio Time Saver

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (1 vote)

Oct 30, 2013

CPOL

1 min read

viewsIcon

4764

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 was 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 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.)