Here is a macro that can help you. It will remove the indent if it detects that you are currently creating a namespace
. This is not perfect, but it seems to work so far.
Public Sub aftekeypress(ByVal key As String, ByVal sel As TextSelection, ByVal completion As Boolean) _ Handles TextDocumentKeyPressEvents.AfterKeyPress If (Not completion And key = vbCr) Then 'Only perform this if we are using smart indent If DTE.Properties("TextEditor", "C/C++").Item("IndentStyle").Value = 2 Then Dim textDocument As TextDocument = DTE.ActiveDocument.Object("TextDocument") Dim startPoint As EditPoint = sel.ActivePoint.CreateEditPoint() Dim matchPoint As EditPoint = sel.ActivePoint.CreateEditPoint() Dim findOptions As Integer = vsFindOptions.vsFindOptionsMatchCase + vsFindOptions.vsFindOptionsMatchWholeWord + vsFindOptions.vsFindOptionsBackwards If startPoint.FindPattern("namespace", findOptions, matchPoint) Then Dim lines = matchPoint.GetLines(matchPoint.Line, sel.ActivePoint.Line) ' Make sure we are still in the namespace {} but nothing has been typed If System.Text.RegularExpressions.Regex.IsMatch(lines, "^[\s]*(namespace[\s\w]+)?[\s\{]+$") Then sel.Unindent() End If End If End If End If End Sub
Since it works all the time, you need to make sure that you install the macro inside your EnvironmentEvents
inside MyMacros, you can only access this module in the macro explorer (Tools-> Macros-> Macro Explorer).
One note, it currently does not support βpackagedβ namespaces such as
namespace A { namespace B { ... } }
EDIT
To support "packed" namespaces, such as the example above and / or support comments after the namespace, for example namespace A { /* Example */
, you can try the following line:
If System.Text.RegularExpressions.Regex.IsMatch(lines, "^[\s]*(namespace.+)?[\s\{]+$") Then
I have not yet had the opportunity to test it, but it works.
Rod Sep 23 '10 at 20:47 2010-09-23 20:47
source share