Is there a way to get Visual Studio to stop indenting names?

Visual Studio tries to backtrack code inside namespaces.

For example:

namespace Foo { void Bar(); void Bar() { } } 

Now, if I cancel it manually, it remains that way. But unfortunately, if I add something right before void Bar(); - for example, a comment - VS will continue to try to retreat.

This is so annoying that mostly because of this one reason, I almost never use namespaces in C ++. I can’t understand why he is trying to backtrack from them (which means indenting 1 or even 5 tabs of the whole file?) Or how to stop it.

Is there any way to stop this behavior? Configuration parameter, add-on, registry setting, hell, even a hack that directly modifies the devenv.exe file.

+54
c ++ namespaces indentation visual-studio
Sep 16 '10 at 14:50
source share
6 answers

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.

+13
Sep 23 '10 at 20:47
source share

As KindDragon points out, Visual Studio 2013 Update 2 has the ability to stop the indentation.

You can uncheck TOOLS β†’ Options β†’ Text Editor β†’ C / C ++ β†’ Formatting β†’ Indents β†’ Content of the indent namespace.

+34
Nov 18 '14 at 13:02
source share

Just do not insert anything before the first line of code. You can try the following approach to insert a zero line of code (it seems to work in VS2005):

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

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

+30
Sep 16 '10 at 18:32
source share

Probably not what you wanted to hear, but many people get around using macros:

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

It sounds silly, but you will be surprised how many system headers use this. (for example, for implantation glibc stl has the value _GLIBCXX_BEGIN_NAMESPACE() ).

I really prefer this path, because I always lean when I see invisible lines following { . It's just me, though.

+12
Sep 16 '10 at 16:06
source share

You can also forward the declaration of your types (or something else) inside the namespace, and then implement it outside as follows:

 namespace test { class MyClass; } class test::MyClass { //... }; 
+5
Jun 15 2018-12-12T00:
source share

I understand the problem when there are nested namespaces. I used to wrap the entire namespace in one line to avoid multiple indentation. He will leave one level, but it is not as bad as many levels. It was so long ago that I used VS, which I hardly remember about those days.

 namespace outer { namespace middle { namespace inner { void Test(); ..... }}} 
+3
Sep 16 '10 at 16:33
source share



All Articles