C++ access modifier auto indentation in Visual Studio 2010 slowly driving me crazy - can it be changed?
Asked Answered
T

2

12

When programming C++ in Visual Studio, it insists on giving me these awful indentations on access modifiers - my condolences if anyone actually likes them this way ;) (a joke folks!)

public class MyClass
{
public:
   MyClass();
   ~MyClass();
   int wowAnInt();
}

Needless to say, I want this:

public class MyClass
{
    public:
       MyClass();
       ~MyClass();
       int wowAnInt();
} 


Is there any way to achieve this using anything (I've got ReSharper and Highlighter) or perhaps vanilla VS?

Toner answered 3/5, 2011 at 23:32 Comment(7)
I like them this way! (I would venture that in fact, this style is very common and popular...)Overland
@Oli You might be right, and I MIGHT just have to settle on this... But it just seems weird with the brace and then the text directly below it.. Feels unnatural to a C#er like me! Just thought I'd pop the question. :)Toner
I agree with Oli; however, I also find it irritating when a tool insists on formatting things a particular way and it's not what I'd prefer. Even if my preference isn't in agreement with general usage.Lierne
Type Ctrl+Z after pressing Enter.Subastral
I also agree with Oli. My vim is set up (my friend's set up, don't ask) to indent the access modifiers, and it annoys the hell out of me. I always have to go in and remove the indent.Scarlett
@OliverCharlesworth You'll find that having 2000 lines in a function is very common too; doesn't mean that everyone wants to work with it! That's why almost all the other options are configuration such as starting new line for { etc everyone has their preference!Fist
As a side note: this indentation style seems to be promoted by the inventor of C++ language Bjarne Stroustrup. Read about Stroustrup indetation style.Bullace
D
12

The closest you can get with the built-in Visual Studio editor settings is to change the indenting mode from "Smart" to "Block" (Tools -> Options -> Text Editor -> C/C++ -> Tabs -> Indenting).

When you do this, you can indent anything however you like, you just lose the "automatic indenting." Basically, whenever you press [enter] the new line will be indented the same number of tab stops / spaces as the previous line and it won't automatically reformat lines to get them to line up.

Drenthe answered 4/5, 2011 at 3:12 Comment(4)
But it still seems to reformat things when I copy/pasteLang
Thanks, this works! Ten years later and VS still doesn't have that one tiny extra checkbox for visibility label indentation... injury to insult, found out that unlike C#, with C++ it allows to configure automatic space before block opening (( int fn() {...} vs int fn(){ ... } )) which is something that drives me crazy.Gambia
@ProQ does changing TextEditor > C/C++ > CodeStyle > Formatting >When I Paste to "Do Nothing" help?Gambia
I now use VS Code, so unfortunately I won’t be able to answer thisLang
L
4

There are two options to change that should help get the code looking the way you want. (Coming from Python, it really bothers me if stuff is not indented after a colon.)

I did use James McNellis's answer and change it from "Smart" to "Block", though I'm not sure how much that helped.

There is a setting under Tools -> Options -> Text Editor -> C/C++ -> Formatting -> Indentation -> Indent Access Specifiers which does indent the access specifiers, but doesn't indent the stuff after them.

I also chose "Do nothing" under Tools -> Options -> Text Editor -> C/C++ -> Formatting -> General -> When I paste, so that it doesn't change things just by copy/pasting.

This isn't a perfect solution, but it's at least a little bit closer.

Lang answered 2/6, 2018 at 1:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.