BubbleSort
BubbleSort

Reputation: 1539

Programmatically enforce capitalization in Visual Studio 2008?

What is the easiest way to programmatically force capitalization of keywords in Visual Studio 2008?

We work with a proprietary command delimited language (like HTML). We are attempting to migrate from an older editor to Visual Studio 2008. Our coding standards are to capitalize the commands. The old editor is customized to recognize the command begin delimiter and to force capitalization until the end delimiter is typed or the escape key is pressed.

What's the best way to do that in Visual Studio 2008? Can it be done with a macro or an add-in?

(Edited 1-12-2009)

Thank you for the suggestions so far. I don't think they answer my question.

Clarifications:

Upvotes: 3

Views: 868

Answers (3)

BubbleSort
BubbleSort

Reputation: 1539

This may not be the best solution but here is what I came up with.

Use macros to capture Key Press Events.

Here's how:

  1. In Visual Studio go to the Tools->Macros->Macros IDE menu
  2. Double Click "MyMacros" to see the different parts
  3. Double Click "EnvironmentEvents"
  4. Add the following code within the Environment Events module.
  5. Save the file and return to the regular VS IDE to test.

    Private My_AutoCaps As Boolean = False
    Private Sub TextDocumentKeyPressEvents_BeforeKeyPress(ByVal Keypress _
      As String, ByVal Selection As EnvDTE.TextSelection, _
      ByVal InStatementCompletion As Boolean, ByRef CancelKeyPress As Boolean) _
      Handles TextDocumentKeyPressEvents.BeforeKeyPress
         Dim fileName As String = UCase(Selection.DTE.ActiveDocument.Name)
         If ( fileName.EndsWith(".CPI") ) Then
             If (My_AutoCaps) Then
                 'MsgBox(Keypress)
                 If (Keypress = "(" Or Keypress = ":") Then
                     'MsgBox("End of command character pressed.")
                     My_AutoCaps = False
                     Return
                 ElseIf (Keypress >= "a" And Keypress <= "z") Then
                     'MsgBox("Letter pressed.")
                     Selection.Text = UCase(Keypress)
                     CancelKeyPress = True
                 End If
             Else 'AutoCap is not on yet
                 If (Keypress = "^") Then
                     'MsgBox("You pressed the Start Command character.")
                     My_AutoCaps = True
                     Return
                 End If
             End If
         End If
    End Sub
    

This macro is limited to *.CPI files.

I have not figured out how to capture the Esc key yet but this will work for now.

Upvotes: 1

DavGarcia
DavGarcia

Reputation: 18822

Try out StyleCop, available from Microsoft's web site. You might have to adjust the rule-set for your specific coding standards. For the coding standards we use, it was almost perfect out of the box.

Upvotes: 4

Rob Allen
Rob Allen

Reputation: 17749

While time consuming, this SO post shows you how to add tags to the validation setup in VS2005. I don't think the method changed in 2008.

If you are moving from an older version of Visual Studio you may be able to just import your old settings and custom tags.

Upvotes: 1

Related Questions