Boxcar Case Macro in Visual Studio 2010 – Video
May 17th, 2010
I get so much use out of this handy dandy little macro that I just had to share it. It is a simple little Visual Studio macro that should work in 2010 or 2008 to change a string like this:
“When writing a unit test”
into this:
When_writing_a_unit_test
And do it in a single keystroke. My dirty little secret? I am a bad typist, so I need little helpers like this.
The following video will walk you through installing the macro and binding it up to a keyboard shortcut. If you haven’t worked with Visual Studio macros before, this might help get there a bit faster.
And here is the macro itself.
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Imports System.Windows.Forms
Imports System.Text.RegularExpressions
Public Module BddMethods
Sub BoxcarTheCurrentLineOfCode()
Dim description As String
Dim regEx As Regex = New Regex("""([^""]+)""")
If DTE.ActiveDocument Is Nothing Then Return
Dim currentLine As TextSelection = GetCurrentLine()
If (currentLine Is Nothing Or currentLine.Text.Length() < 1) Then Return
Dim stringMatch As Match = regEx.Match(currentLine.Text)
For Each stringMatch In regEx.Matches(currentLine.Text)
currentLine.Text = currentLine.Text.Replace(stringMatch.Value, BoxcarAString(stringMatch.Value))
Next
currentLine.EndOfLine()
End Sub
Private Function BoxcarAString(ByVal textSegmentToChange As String) As String
Dim segmentAfterTheChange As String = textSegmentToChange.Trim()
segmentAfterTheChange = segmentAfterTheChange.Replace("""", "")
segmentAfterTheChange = segmentAfterTheChange.Replace(" ", "_")
Return segmentAfterTheChange
End Function
Private Function GetCurrentLine() As TextSelection
Dim currentLine As TextSelection = DTE.ActiveDocument.Selection()
currentLine.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn)
currentLine.EndOfLine(True)
Return currentLine
End Function
End Module



