views:

423

answers:

4

Visual Studio keeps trying to indent the code inside namespaces.

For example:

namespace Test
{
   void Testing();

   void Testing()
   {

   }

}

Now, if I un-indent it manually then it stays that way. But unfortunately if I add something right before void Testing(); - such as a comment - VS will keep trying to indent it.

This is so annoying that basically because of this only reason I almost never use namespaces in C++. I can't understand why it tries to indent them (what's the point in indenting 1 or even 5 tabs the whole file?), or how to make it stop.

Is there a way to stop this behavior? A config option, an add-in, a registry setting, hell even a hack that modifies devenv.exe directly.

+6  A: 

Probably not what you wanted to hear, but a lot of people work around this by using macros:

#define BEGIN_NAMESPACE(x) namespace x {
#define END_NAMESPACE }

Sounds dumb, but you'd be surprised how many system headers use this. (glibc's stl implentation, for instance, has _GLIBCXX_BEGIN_NAMESPACE() for this.)

I actually prefer this way, because I always tend to cringe when I see un-indented lines following a {. That's just me though.

Ken Simon
I think this is more for compatibility with old compilers than because of Visual Studio =)
Andreas Bonini
@andreas I think that way you can change the namespace of the whole lib easily without editing all the source files
David Feurle
+3  A: 

I understand the problem when there are nested namespaces. I used to pack all the namespaces in a single line to avoid the multiple indentation. It will leave one level, but that's not as bad as many levels. It's been so long since I have used VS that I hardly remember those days.

namespace outer { namespace middle { namespace inner {
    void Test();
    .....
}}}
David Rodríguez - dribeas
+10  A: 

Just don't insert anything before the first line of code. You could try the following approach to insert a null line of code (it seems to work in VS2005):

namespace foo
{; // !<---
void Test();
}

This seems to suppress the indentation, but compilers may issue warnings and code reviewers/maintainers may be surprised! (And quite rightly, in the usual case!)

bacar
That's actually a good idea.
Andreas Bonini
I like this too. Seems like even a macro that expands to nothing is enough to suppress the indentation. So you can `#define SUPPRESS_INDENTATION` and then open the namespace like `namespace foo { SUPPRESS_INDENTATION` :)
pgroke
Nice - the intent is somewhat clearer and the compiler is hopefully less likely to raise warnings :)
bacar
+3  A: 

Here is a macro that could help you. It will remove indentation if it detects that you are currently creating a namespace. It is not perfect but 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 is running all the time, you need to make sure you are installing the macro inside in your EnvironmentEvents project item inside MyMacros. You can only access this module in the Macro Explorer (Tools->Macros->Macro Explorer).

One note, it does not currently support "packed" namespaces such as

namespace A { namespace B {
...
}
}

EDIT

To support "packed" namespaces such as the example above and/or support comments after the namespace, such as namespace A { /* Example */, you can try to use the following line instead:

 If System.Text.RegularExpressions.Regex.IsMatch(lines, "^[\s]*(namespace.+)?[\s\{]+$") Then

I haven't had the chance to test it a lot yet, but it seems to be working.

Rod
Thank you very much for your answer. There was an error that I now corrected, but besides that it works perfectly. Grats on the bounty.
Andreas Bonini
Damnit, I'd started spending that bounty already! ;-)
bacar