Click here to Skip to main content
15,885,366 members
Articles / Visual Studio

Just A Little Visual Studio Time Saver

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
30 Oct 2013CPOL1 min read 4.6K   1  
Just a little Visual Studio time saver

Image 1

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

C#
// 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:

VB.NET
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.)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
I graduated from Washington State University in Computer Science (undergrad + masters). I've spent over 10 years in software development for a National Laboratory, a startup engineering company, and a nuclear processing plant. This has provided me with a diverse set of experiences and skills. I have worked extensively with C++ (w/ MFC, STL, & Boost), C#, Python, and Qt. I'm also comfortable with VB.Net, and HTML/XML, as well as tools like Visual Studio, JIRA, and Subversion. I have a strong background in mathematics & graphics and have delved into graph & network theory, information visualization, data analytics, SCADA/HMIs, and artificial intelligence.

Also, I'm not quite as old as I look.

Comments and Discussions

 
-- There are no messages in this forum --