Opening curly brace fails to indent consistently - c++

I'm transitioning to Visual Studio 2010 from version 2008 and have noticed several differences between the editor. Most I have fixed or found work-arounds for. One feature that I cannot get to work like it did in 2008 is when I type an open curly brace "{" on a non-empty line in a CPP file. Typically, after starting a for, if, while, or similar statement, I'll automatically type the {} on separate lines below the statement so that the curly braces are nicely lined up (it's an OCD thing). VS 2010 does what I want in this situation.
if (varIsTrue)
{
varIsTrue = false; // typed after the initial {} pair.
// do something really innovative here...
}
It's when I forget that initial "{" that the editor no longer wants to help.
if (varIsTrue)
varIsTrue = false;
Oops. Forgot the curly brace (or I simply need to create a code block for additional code.)
Typing a "{" gives
if (varIsTrue)
{varIsTrue = false;
instead of
if (varIsTrue)
{varIsTrue = false;
so that hitting ENTER will properly indent "varIsTrue = false;"
Visual Studio 2008 handled this correctly, but 2010 does not. I've yet to find a way fix this in the editor options, and I assume that an editor extension would be the only other option.
So basically, if there is a way to fix this in the editor, how can I do it? If not, does an extension exist that would allow me to fix the problem?
Interestingly, this problem does not exhibit itself in a C# file, so I am hoping there is a way to fix it for CPP files.
I appreciate any help regarding this.

Related

Format for curly braces in VS Code Snipets for C/C++

I installed the C/C++ Snippets extension in VS Code, and it creates a snippet like this:
for ( )
{
// your code
}
but I want the open brace on the keyword line:
for ( ) {
I found this topic for C/C++ formation in VS Code: "Format Curly Braces on Same Line in C++ VS Code" but it didn't solve the problem (so, the snippet extension doesn't follow the C formatting "rules" in VS Code, I guess).
I can't find any setting in the VS Code settings JSON file for this extension.
Anyone have an idea?
(In the ideal world, the curly brace for functions would remain on the next line and only braces associated with keywords would be on the same line.)
Thank you,
-Vin
I did some intense digging and found the name of the file that holds the snippits, then looked in all the "usual suspect" places and found it:
C:\Users\.vscode\extensions\hars.cppsnippets-0.0.14\snippets\
in this directory, there are two files: c.json, cpp.json
In each there are json entries for each snippit. I edited a couple in the c.json to test and they now format as I want. Thanks Ali! your suggestions got me thinking!

migrating from MFC C++ 6.0 application to VS 2005

I have a big application developed in MFC C++ 6.0 and I want to migrate it to VS2005 and then, to VS2012. I'm looking for solve as much errors as I can, however I run into this situation:
I had a MFC C++ method: BOOL CPaginaModelo::OnRetirar_ItemLista() which returned a boolean value. So in many cpp files I use code like:
if (CPaginaModelo::OnRetirar_ItemLista())
{
<code>
}
For migration reasons, I have to change such method from BOOL to void, so the code should looks like:
CPaginaModelo::OnRetirar_ItemLista();
<code>
Since there are a lot of parts of the project files where this is used, is there any way by using search and replace with regular expressions in VS2005 to replace first expression to look like second expression?
Thanks for your help
Find and replace operation is not dependent on visual studio version. If you want use higher version of VS for this purpose then you just need to open particular VS and in find window replace window, replace parameter of "Look In" from entire solution to folder of your solution. This will allow you apply find and replace operation on certain folder.
To solved your issue, Multiline Search and Replace will help to complete replacement of your code with specific.
You could find and replace text in the Visual Studio editor by using Find and Replace (Ctrl+F or Ctrl+H) or Find/Replace in Files (Ctrl+Shift+F or Ctrl+Shift+H). For more details, I suggest you could refer to the link:https://learn.microsoft.com/en-us/visualstudio/ide/finding-and-replacing-text?view=vs-2019
You could also try to use .NET regular expressions to find and replace text in visual studio. For more details, I suggest you could refer to the link:https://learn.microsoft.com/en-us/visualstudio/ide/using-regular-expressions-in-visual-studio?view=vs-2019
As far as I'm concerned, Replacement couldn't make a symbol disappear, so { } may need to be removed manually.

What's this curly brace comma syntax in Visual Studio?

What is this weird syntax you can use the the watch window (or even to set breakpoints) and where can I find it documented:
{,,test2.exe}<variable name>
When starting the program, the first will translate to my entry point and will break on execution start.
Intuitively I can see what this means, but where is it documented and what are the leading commas for (what stuff can you put in there)?
Why does using the syntax in the watch window help?
As the VS2015 documentation states, that is called a Context Operator, valid only for C++ native applications.
You could put function and source file before the first and the second comma.
For VS2010 and previous versions the documentation is slightly different and more verbose.

Formatting of switch statement

Say I have my cursor position to the right of a right facing case curly brace, like so:
Now, if I press enter, I expect it to auto align the cursor two tabs in, just like the break statement. But what it does is this:
It adds a ridiculous five tabs! Knowing that Visual studio has a metric ton of settings, I navigate to Tools::Settings::Text Editor::C/C++::Formatting::Indentation, and see the following window:
But changing the highlighted options in any combination actually doesn't affect the indentation at all! None of the other options seem to apply to switch statements, so I don't know what to do. How do I make it not indent 5 spaces, without disabling auto formatting?
And I might add, it not only places 5 tabs when I press enter at the end of the curly brace, but when any auto format event takes place. So when I add a semicolon at the end of a line it places 5 tabs even if I had taken them out before.
This is probably a bit late for you now, but in case someone else finds this issue: it seems to be a Visual Studio bug, you're probably running the freshly installed version of the VS2K13 called REL.
Downloading the Update 4 at http://www.microsoft.com/en-us/download/details.aspx?id=44921 helped in my case.
The curly braces ({ and }) are throwing off the auto-indenter, and it's indenting to one tab beyond the brace.
Braces there are not illegal in a switch statement, but they usually don't do you any good. Unless you need it for scoping a variable declaration, just remove the curly braces. You'll get the same code flow, and you won't confuse the auto-indenter.
EDIT
Come to think of it, you can solve this by simply moving the brace to a new line. This isn't necessarily horrible - it highlights that you're using a brace.
case SDLK_g:
{
// etc
break;
}
I went into VS2013 and created a new project and just tried making a really simple switch but it formatted correctly for me, even with the curly braces. Are you able to post that set of code?
The only other thing I can think of is maybe a setting on how braces are set up, but I don't know why that would affect it. (Nor do I think there is really even a setting for that for C++...) Other than that though you could try just not using the braces at all since you are inside a case statement, you don't technically need them.
Other than that something may have happened during installation. So re-installing is an option too.
EDIT:
Also, could just go with it and finish the code and then when finished just highlight the rows and un-indent ([SHIFT]+[TAB]) them back to their correct spot.

Visual Studio Code doesn't format

I want to format my code by pressing Ctrl+K and Ctrl+D.
But after that, the code is still the same. For example:
void func1()
{
}
void func2() {
}
These functions still look the same after formating.
What am I doing wrong?
The C++ text formatting engine is fairly limited when compared to C# or VB.Net. It is pretty much limited to fixing indentation and correcting tabs vs. spaces. It makes no attempt to clean up which line braces appear on. Hence you're doing nothing wrong here, this is just a limitation of the C++ formatting engine
EDIT
As #dalle pointed out Visual Studio 2013 does indeed support limited brace formatting in C++. By default though the formatting is turned off. You need to explicitly enable it for format document to begin formatting your braces.
Tools -> Options
Text Editor -> C/C++ -> Formatting -> New Lines
Select "Move to a new line" under "Position of open braces for functions"
Once this is done format document will begin to correctly position the braces