UPDATED: The code below has been replaced on 2/5/2010. The previous code wasn't working too well in in 2008 when the case of "public sub" changed to "Public Sub". An additional change, if the text on the line does not contain double quotes, the method will exit without changing your code.
The BDD renaming macro from Jean-Paul Boodhoo really reduces the friction in typing BDD style test names. I can't tell you how much I despise hitting _ so much. Anyways, since I work in VB.Net and well as C#, I had to add VB.net support to the macro. Just map this to a hotkey (I use Ctrl+Alt+-), and you're set. Why is this important? It's important because you can generally type "Should return 0 when the input parameter is 0" much faster than Should_return_0_when_the_input_parameter_is_0.
In C#, this:
public void "Throws an exception with invalid input"()
changes to
public void Throws_an_exception_with_invalid_input()
and in VB:
Public Sub "Throws an exception with invalid input"()
changes to
Public Sub Throws_an_exception_with_invalid_input()
The Code:
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
Public Module CodeEditor
Sub ReplaceSpacesInTestNameWithUnderscores()
If DTE.ActiveDocument Is Nothing Then Return
Dim selection As TextSelection = CType(DTE.ActiveDocument.Selection(), EnvDTE.TextSelection)
selection.SelectLine()
If selection.Text = "" Then Return
Dim prefix As String = "public void "
Dim index As Integer = selection.Text.IndexOf(prefix)
If index = -1 Then
prefix = "public sub "
index = selection.Text.IndexOf(prefix, System.StringComparison.CurrentCultureIgnoreCase)
If index = -1 Then
MsgBox("Could not find method identifier")
Return
End If
prefix = selection.Text.Substring(index, prefix.Length)
End If
prefix = selection.Text.Substring(0, index) + prefix
Dim description As String = selection.Text.Replace(prefix, String.Empty)
If description.IndexOf("""") = -1 Then
selection.EndOfLine()
Return
End If
selection.Text = prefix + description.Replace(" ", "_").Replace("'", "_").Replace("""", "")
selection.LineDown()
selection.EndOfLine()
End Sub
End Module
1973aea7-75c6-4caf-a498-78538ad8260e|1|5.0