Boxcar Case Macro in Visual Studio 2010 – Video

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.

image

MP4

 

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

 

10 thoughts on “Boxcar Case Macro in Visual Studio 2010 – Video

  1. I’m working with a client that likes to use lots_of_underscores_for_their_objects, and no ReSharper. I think this macro is going to be pretty useful!

  2. @Matt

    Check out the AutoHotKey script the Kev mentioned and Jimmy plus oned. It’s awesome and doesn’t require R#. I use it daily and it’s so simple to setup – no video required.

    Ctl U to turn it on, Enter or Esc to turn it off.

  3. I’ve used the AutoHotKey technique myself and frankly I think it is a PITA to have YA tool running in the background and have to set that up on multiple machines. Everyone enjoys a different cup of tea. I don’t think it sucks or anything, I just prefer this, myself.

    I included a video simply because macros are so little-used that few people know the basics of working with them. I am recording a lot of videos for Pluralsight lately and I am just getting to the point where showing is easier than telling, ya know?

  4. Is there an easier way to copy source code from this blog? The syntax highlighting is nice, but it’s a giant PITA to paste the code and have to delete all the line numbers.

Comments are closed.

Proudly powered by WordPress | Theme: Code Blog by Crimson Themes.