Spread the Word

One of the biggest time drains I experience when creating SharePoint solutions is the constant creation of new GUIDs, especially when creating site columns (Fields).  Each time I wanted to create a  new field I would typically copy the previous line and make the appropriate changes.

When I code I want to do everything with keystrokes and not touch my mouse; it must be the old school developer in me.

There are two major problems as I see it:

1. When you double click a section of a GUID, either in XML or in code, the intelligent highlighter only picks up the current segment of the GUID and doesn’t traverse the dashes or braces.  FAIL

2.  Even if we used the keyboard or mouse to select the entire GUID in code there’s no way with the keyboard to create and insert a new GUID. /sigh FAILure yet again.

We can overcome this using any number of resources in Visual Studio but in this case I prefer to use a Macro.  The Visual Studio Macro IDE is available to you by pressing ALT-F11 from the VS IDE.

Note: I could also do this with ReSharper (a MUST have), and somewhat through normal code snippets.

You can either create a new Macro project or place the code in any existing macro project.  For this example we’ll be working in the ‘MyMacros’ project and just put the code into the existing Module1.

Problem #1 – Selecting all characters wrapped in quotes.

We’ll create a new subroutine and name it ‘SelectToQuotes()’:

   1: Public Sub SelectToQuotes()

Our first issue has to do with Visual Studio’s poor implementation of selecting characters inside of quotes.  I find myself using this macro all the time in XML files since every attribute is wrapped in quotes.

To accomplish this we’ll be using the DTE.ActiveDocument property which represents the current document you have opened in the editor.

The first thing we’ll do is figure out where our cursor is in the document by grabbing the document.Selection property.  Don’t worry – even if you' don’t have anything selected we can still use this property.

   1: Dim objSel As TextSelection = DTE.ActiveDocument.Selection
Once we have our TextSelection  we’ll traverse the document backwards until we find a double quote:
 
   1: If objSel.FindText("""", vsFindOptions.vsFindOptionsBackwards) Then

 

Once we locate that quote we’ll set the selection to start at the next character to the right:

   1: objSel.CharRight(False)

Then we simple traverse right until we find hit another quote:

   1: REM -- Move selection down until we find end quote
   2: While Right(objSel.Text, 1) <> """"
   3:     objSel.CharRight(True)
   4: End While

Once we find the quote we backtrack by one character so the trailing quote isn’t selected.  Here’s the complete subroutine:

   1: Public Sub SelectToQuotes()
   2:     Dim objSel As TextSelection = DTE.ActiveDocument.Selection
   3:     If objSel.FindText("""", vsFindOptions.vsFindOptionsBackwards) Then
   4:         objSel.CharRight(False)
   5:         REM -- Move selection down until we find end quote
   6:         While Right(objSel.Text, 1) <> """"
   7:             objSel.CharRight(True)
   8:         End While
   9:         objSel.CharLeft(True)
  10:     End If
  11: End Sub

Now it’s just a matter of wiring this up to a keystroke chord combination inside of Visual Studio!  Just hop into your Visual Studio options (tools/options) and go to the Keyboard configuration section.

By typing the subroutine name into the filter box you’ll see the macro you just created.  Here you can see I’m assigning it to the Ctrl-K,Ctrl-Q keyboard combination:

 image

 

Problem #2 – Inserting a fresh GUID

Now that we can easily select all content wrapped inside of quotes regardless of white spaces or special characters we can write a simple macro that will insert a GUID.

Just as before we’ll add a new subroutine and call this one “InsertGuid()”.

This routine will call out to SelectToQuotes() so we don’t have to hit four keyboard combinations just to insert the GUID.

   1: Public Sub InsertGuid()
   2:     SelectToQuotes()

Once we have the content selected we simply generate a new GUID via code and apply it to the selection.  I’m using the String.Format method and including the wrapping braces:

   1: Dim NewGUID As String = String.Format("{{{0}}}", System.Guid.NewGuid())
   2: objSel.Insert(NewGUID, vsInsertFlags.vsInsertFlagsContainNewText)

After we’ve applied our text to selection we’ll set the selection to null and be done.  Here’s the complete subroutine:

   1: Public Sub InsertGuid()
   2:         SelectToQuotes()
   3:         Dim objSel As TextSelection = DTE.ActiveDocument.Selection
   4:         Dim NewGUID As String = String.Format("{{{0}}}", System.Guid.NewGuid())
   5:         objSel.Insert(NewGUID, vsInsertFlags.vsInsertFlagsContainNewText)
   6:         objSel = Nothing
   7:     End Sub

Assign this routine to the keyboard combination of your choice (I use Ctrl-K,Ctrl-G) and Enjoy!

© Copyright 2010 G. Scott Singleton | Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.