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
It would make a lot more sense to write a ReSharper context action for this sort of thing.
Perhaps, but then it requires ReSharper. A lot more people don’t have R# than you think.
Is it not much easier to use AutoHotKey for this?
JP has had an entry on his blog about it for some time
http://blog.jpboodhoo.com/BDDAutoHotKeyScriptUpdateTake2.aspx
1 on AutoHotKey, it plays much nicer with IntelliSense, which tends to pop up and annoy me with a macro-based approach.
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!
@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.
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?
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.
I’ll omit the line numbers. Sorry 🙂
Nice macro, people using ViEmu on VS can use the command => ‘%s/ /_/g|s/”//g’ to get the same effect.