String for function name in Visual Studio

I saw in screencast some time ago (since I forgot that it was probably Kata), where the person wrote 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 an easy way to make a template that starts when a key is pressed?

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

+4
source share
1 answer

I could not find the plugin or macro that you are referencing, but I created a macro that will work beautifully!

First, complete the following steps to install:

  • Press Alt + F11
  • Expand MyMacros
  • Open the EnvironmentEvents module.
  • Read the code in the module (the code is at the end of this message)
  • Close Macro Editor

To use a macro:

  • Press ` ( serious key).
  • Then click "
  • Enter the desired words.
  • End by typing "`
  • Watch the magic!

NOTE. . You can just start typing the screening value, and then add gravestone characters before and after, and it will work anyway.

The macro will remove the spaces and then PascalCase the entire set of words. It also highlights single and double quotes. Finally, it converts commas to underscores if you want to use the naming convention proposed by Roy Osherov (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 ` click):

 public void ReturnZeroForAnAllGutterGame public void LoadMainParts_WhenMaterialsFilesAreValid_WillReturnAListOfPartsSortedBySequentialItemNumberAscending 

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 = "[ ,"",\,][az]" 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 adapt the code to your needs.

+2
source

All Articles