70

Visual Studio keeps trying to indent the code inside namespaces.

For example:

namespace Foo
{
   void Bar();

   void Bar()
   {

   }

}

Now, if I un-indent it manually then it stays that way. But unfortunately if I add something right before void Bar(); - 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.

Andreas Bonini
  • 44,018
  • 30
  • 122
  • 156
  • You could disable auto-indenting entirely. Or set it to indent by a smaller amount (say, 2 spaces instead of 1 tab) – jalf Sep 16 '10 at 16:48
  • 3
    This (non-configurable indentation settings) is one of many reasons I stopped editing files in Visual Studio years ago... :) – dash-tom-bang Sep 16 '10 at 17:34
  • 2
    I will award the bounty to whoever finds a solution that has no side effects, or as little side effects as possible. For now the best answer, IMO, is bacar's; if no better answers are provided I will award the answer to him. – Andreas Bonini Sep 18 '10 at 18:55
  • That's why I'm using Eclipse CDT ;) It is possible to use VS compiler within Eclipse so it may be worth a try. – mip Sep 23 '10 at 21:56
  • 2
    https://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/3894937-option-to-stop-indenting-namespaces-in-c-code – KindDragon Apr 25 '13 at 13:39

7 Answers7

57

As KindDragon points out, Visual Studio 2013 Update 2 has an option to stop indenting.

You can uncheck TOOLS -> Options -> Text Editor -> C/C++ -> Formatting -> Indentation -> Indent namespace contents.

user64953
  • 723
  • 1
  • 6
  • 10
  • 5
    It seems to have become broken again... At least in Update 4. I hope they fix it. – AzP Jan 29 '15 at 10:30
  • 2
    It's indeed broken. Though you can use clang format and simply work around the mess. – John Leidegren Feb 28 '15 at 20:38
  • 1
    I just tried VS2013 Update 5 CTP; it appears to be working again. Hopefully it stays that way for the official Update 5 release. – jdknight Mar 18 '15 at 04:50
  • 2
    This is the best answer. Works as I expected in VS 2015. – Software Craftsman Apr 26 '17 at 14:58
  • The question is for `C++`, but does anyone know if there is an option for `C#` in `visual-studio-2019`? I looked but cannot find an `Indent namespace contents` checkbox in the `Indentation` options. – Loathing Jan 05 '21 at 04:51
  • For Visual Studio 2019: Tools -> Options -> Text Editor -> C/C++ -> __Code Style__ -> Formatting -> Indentation -> Indent namespace contents. – Matthias C. M. Troffaes Jun 26 '21 at 11:02
  • Works perfectly. This should have been the accepted answer, much simpler than the macro solution that was accepted. – JensB Jul 02 '21 at 15:59
31

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
  • 9,761
  • 11
  • 55
  • 75
  • 11
    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` :) – Paul Groke Sep 22 '10 at 23:06
  • Nice - the intent is somewhat clearer and the compiler is hopefully less likely to raise warnings :) – bacar Sep 23 '10 at 17:41
  • While this has been my favourite solution, I wanted to mention that this trick does not work in VS2013. There is a user configuration setting in VS2013 which prevents this from happening (when it works; see other answer). – jdknight Mar 18 '15 at 04:55
14

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
  • 1,514
  • 8
  • 11
13

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.

Andreas Bonini
  • 44,018
  • 30
  • 122
  • 156
Rod
  • 52,748
  • 3
  • 38
  • 55
  • 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 Sep 24 '10 at 02:32
  • Works fine for typing but not for pasting or e.g. Visual Assist's "Create Implementation" functionality whereas bacar's solution does do the trick there. It is cleaner though. – Adversus Oct 03 '12 at 12:35
  • 1
    The question might seems stupid, but I really cannot find an answer: where are we supposed to paste this macro ? – Aracthor Sep 15 '15 at 13:25
9

You could also forward declare your types (or whatever) inside the namespace then implement outside like this:

namespace test {
    class MyClass;
}

class test::MyClass {
//...
};
Community
  • 1
  • 1
metator
  • 397
  • 3
  • 5
8

Visual Studio 2017+

enter image description here You can get to this "Indent namespace contents" setting under Tools->Options then Text Editor->C/C++->Formatting->Indention. It's deep in the menus but extremely helpful once found.

Michael Hall
  • 81
  • 2
  • 5
3

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
  • 204,818
  • 23
  • 294
  • 489