James Armstead
James Armstead

Reputation: 398

String to Function Name in Visual Studio

I saw in a screencast a while ago (since forgotten which, probably a Kata) where a person was writing out a unit test but wrote something like this:

public "return zero for an all gutter game"

Then they magically turned it into

public returnZeroForAnAllGutterGame

Is there a plugin for this or just a simple way to do a template that gets fired off on a key stroke?

I googled around and just couldn't think of a good way to type in a search to get what I wanted.

Upvotes: 4

Views: 404

Answers (1)

Matt
Matt

Reputation: 14551

I couldn't find the plugin or macro you refer to, but I did create a macro that will work nicely!

First, to install do the following:

  1. Press Alt+F11
  2. Expand MyMacros
  3. Open the EnvironmentEvents module
  4. Past the code into the module (code is found at the end of this post)
  5. Close the macro editor

To use the macro:

  1. Press ` (grave key).
  2. Next press "
  3. Type the words you desire
  4. End by typing "`
  5. Watch the magic happen!

NOTE: You could just start typing a sting value then latter add the grave symbols before and after and it will still work.

The macro will remove spaces and then PascalCase the entire set of words. It also strips out single and double quotes. Lastly, it will convert commas to underscores if you want to use the naming convention suggested by Roy Osherove (The Art of Unit Testing, p. 211):

MethodUnderTest_Scenario_Behavior()


Examples:

public void `"return zero for an all gutter game"`

public void `"LoadMainParts, when materials files are valid, will return a list of parts sorted by sequential item number ascending"`

...will turn into this (after the second ` press):

public void ReturnZeroForAnAllGutterGame    

public void LoadMainParts_WhenMaterialsFilesAreValid_WillReturnAListOfPartsSortedBySequentialItemNumberAscending

The Macro:

...

Imports System.Text.RegularExpressions

...

Private isPascalCaseAndSpaceRemovalEnabled As Boolean

Private Function ConvertToPascalCase(ByVal value As String) As String
    'apply ToUpper on letters preceeded by a space, double quotes, or a comma'
    Dim pattern As String = "[ ,"",\,][a-z]"
    value = Regex.Replace(value, _
                          pattern, _
                          Function(m) m.Value.ToUpper, _
                          RegexOptions.Singleline)

    'replace commas with underscores'
    value = value.Replace(",", "_")

    'remove spaces, graves, double quotes, and single qoutes'
    Dim removalCharacters As String() = {" ", "`", """", "'"}
    For Each character In removalCharacters
        value = value.Replace(character, "")
    Next

    Return value
End Function

Private Sub TextDocumentKeyPressEvents_AfterKeyPress(ByVal Keypress As String, _
                                                     ByVal Selection As EnvDTE.TextSelection, _
                                                     ByVal InStatementCompletion As Boolean) _
                                                     Handles TextDocumentKeyPressEvents.AfterKeyPress

    If isPascalCaseAndSpaceRemovalEnabled AndAlso Keypress = "`" Then
        Selection.SelectLine()

        Dim pattern As String = "`""(.*)""`"
        Dim substringToReplace As String = Regex.Match(Selection.Text, _
                                                       pattern, _
                                                       RegexOptions.Singleline).Value
        Selection.ReplacePattern(pattern, _
                                 ConvertToPascalCase(substringToReplace), _
                                 vsFindOptions.vsFindOptionsRegularExpression)

        Selection.MoveToPoint(Selection.BottomPoint)

        isPascalCaseAndSpaceRemovalEnabled = False
        CancelKeyPress = True

    ElseIf Keypress = "`" Then
        isPascalCaseAndSpaceRemovalEnabled = True
    End If

End Sub

Feel free to tailor the code to your needs.

Upvotes: 2

Related Questions