Visual Studio Macro to Indent C/C++ Braces - visual-studio

We have an old project that we maintain that uses brace indenting. Rather than having to change my Visual Studio options every time I switch projects, I'd like to be able to do this quickly and easily.
I'm trying to write a macro in Visual Studio to turn Brace Indenting on and off.
The checkbox is under Text Editor -> C/C++ -> Formatting
Indentation: Indent Braces
This doesn't work:
DTE.Properties("TextEditor", "C/C++").Item("IndentBraces").Value = True
I get "Value does not fall within the expected range."
Any ideas? I haven't been able to find anything on this.

I found the following is the correct macro code to make this work
DTE.Properties("TextEditor", "C/C++ Specific").Item("IndentBraces").Value = True
see: "Determining Names of Property Items in Tools Options Pages" which is only mildly helpful in this situation but gave me enough of a clue to find it.

Related

Visual Studio code style preference csharp_prefer_braces not working

In Visual Studio 2017 I go to Tools > Options > Text Editor > C# > Code Style, and toggle the option "Prefer braces" from yes to no and back.
It does nothing. The preview below the options always has braces regardless of the setting.
Even an explicit .editorconfig file on csharp_prefer_braces = false:none does not change the behavior.
Is this a bug? Or is some other option overriding?
The property after the ":" will decide how this is treated in VS Code. So using none will mean that VSCode doesn't actually enforce this.
The MS docs here describe "none" as:
"None": Do not show anything to the user when this rule is violated. Code generation features generate code in this style, however. Rules with none severity never appear in the Quick Actions and Refactorings menu. In most cases, this is considered "disabled" or "ignored".
Perhaps you could try using csharp_prefer_braces = false:error instead?

Prevent Visual Studio from indenting code after multi-line template

I have recently discovered that Visual Studio likes to indent things after a template which takes up multiple lines, as you can see here:
After the template, in which I like to list parameters on different lines to prevent one huge line, the class declaration is indented by a tab. It indents the whole class declaration, so possibly hundreds of lines end up indented unnecessarily.
This happens every time I use Visual Studio's auto formatting tool, which is rather annoying, as I do want to be able to use auto format (it's very helpful), but just not this specific "feature"(?). I've looked through all the options in Tools -> Options -> Text Editor -> C/C++ -> Formatting, but I have not managed to find one that fixes this issue.
How can I prevent Visual Studio from indenting code after multi-line templates like this?

Visual Studio 2013 auto indent

In Visual Studio 2013, under Tools > Options > Text Editor > File Extension, I have set three file extensions, namely .cginc, .compute, and .shader (these are Unity3D shader files) to use Microsoft Visual C++ for auto formatting.
Sometimes auto indent (for curly braces) works, and sometimes it doesn't...I can't find any rhyme or reason for this. When it doesn't work, it starts every new line flush to the left.
I Google around every couple weeks and can't find an answer.
Does anyone know any setting to make auto indent work consistently?
Try this:
Tools -> Options -> Select the language of your choice (Expand the menu) -> Select Formatting (Expand this sub menu) -> Select Indendation
Once you have selected Indentation, on the right are options displayed - Check/Tick the option: Indent Braces.
Note: You may also format the entire document/file using the shortcut Ctrl+K+D

Stop VS 2010 from Auto Creating Braces After else keyword

I'm using VS 2010, and I'm getting constantly annoyed when I type "else" and then it auto returns line and adds braces. Something like:
else
{
}
I cannot imagine I'm the only one who often puts one-liners after the else and prefers no braces. How do I stop this from happening?
I also found this behavior very annoying.
My first attempt involved opening the Templates Explorer (ReSharper / Templates Explorer...), selecting C#, then unchecking "else". This did get rid of the braces. Unfortunately, hitting enter after "else" now just added a space! I was having to hit enter twice. Not quite what I was hoping for.
But if you double-click on the else template it opens up an editor where you can specify what happens. I found that simply removing the braces gave me the functionality I desired - hitting enter after an else was now putting me on a new line, tabbed over under the else - without braces.
To recap, to fix this you need to open Resharper's Templates Explorer, select the C# scope, then double-click on the else template and remove the braces. Your template "snippet" should look like this (just two lines):
else
$END$
This works in Resharper 6.1 and Visual Studio 2008. I have no idea about other versions of the software.
Okay, finally figured out what the hell is doing this. I have ReSharper installed on this machine, and apparently the "else" keyword is a ReSharper autocomplete keyword. So even after I turned off the braces completion, ReSharper was still kicking in and adding them. Rage. In order to stop this, you have to turn off the ReSharper autocomplete.
I don't think there's any way to disable brace auto-complete for specific keywords. If you go to:
Tools -> Options -> Text Editor -> C# -> Formatting
you'll find the various formatting options. It's possible to disable auto complete in general or affect indentation and such, but I don't see any way to disable brace auto complete for specific keywords.
Are you using Productivity Power Tools? You can turn it off in Tools, Options, "Productivity Power Tools", "Automatic Brace Completion". (I know I do)

Visual Studio 2010 - Curly Braces auto-align has changed, how do I fix this?

Update: Since I see this is one of my most looked at questions, now a few years later I should point out what I found to be the most common cause of this problem: bad syntax elsewhere in your code. 100% of the time I've had this happen, it was because I'd forgotten a curly brace for another block of code, or I had a dangling if, or an earlier line of code I didn't finish. Check for IDE errors first for something like ") or } expected"
Original Question:
I feel like this is an all time dumb question to ask, but I have no idea how to fix this and google is turning up nothing.
In visual studio, when I type:
try {
}
VS would automatically reformat it to
try
{
}
But now it's just leaving it at the first one. I assume I accidentally hit a hotkey or something. Help me fix this please?
Tools->Options
Click Text Editor -> Whatever language -> Formatting
Tools->Options->Text Editor->C# (or whatever language)->Formatting->New Lines
Select the options you want.
If you have Power Commands installed you can then go to the Tools->Options->Power Commands and make sure "Format Document on Save" is selected.
Whenever you save the edited file the Curly Braces will be auto aligned.
In addition to the default Visual Studio settings mentioned, for ReSharper users, this is configured via:
ReSharper [menu] -> Options
In the Options dialog, navigate to Environment -> Editor -> Editor Behavior -> "Auto-format on closing brace". See image:
9 times out of 10 this problem is caused by bad code elsewhere on my document. Either I'm missing a ; on a line, or perhaps a closing }. Visual Studio is unable to figure out what I'm trying to write in code and thus it's unable to format. Check for compiler errors, fix them, and then press Ctrl+K then Ctrl + D to make Visual Studio reformat the current document (your hotkeys may vary, depending on the version of Visual Studio and your settings).

Resources