Visual Studio 2010 indentation after for loop - c++

Why am I getting this behavior right after the if block? Am I missing something?
for (;;)
if (/*...*/)
{
// statements
}
// statements indented to match the if indentation instead of the for loop;

Visual Studio 2010 appears to be riddled with editor bugs. Indentation is particularly hosed.
Just wait until it starts moving your cursor to the beginning of the line every time you type a ':'.
If you close the file and reopen it that sometimes fixes the issue...for a little while anyway.

About the only way to keep VS doing indentation reasonably is to always use a block to enclose the statement controlled by a for, if, while, etc. In your case that would mean:
for (;;)
{
if (/* ... */)
{
// ...
}
}
// further statements here indented to match for loop.

Related

broken indentation formatting when if/else chain is over 100 lines in length - visual studio 2017 C++

While using Visual Studio 2017 v15.9.35
I noticed that for functions with long if/else chains the indentation would break at some point. After some experimentation I was able to figure out why; when the if/else chain is over 100 lines in length (from the first if() line) the formatting breaks.
//under 100 lines with expected behavior
void test()
{
int x = 10;
if(x == 10)
{
x = 11;
}
else //when you press enter twice the closing bracket is in the correct position
{
x = 12;
}
}
//>= 100 lines
void test()
{
int x = 10;
if(x == 10)
{
x = 11;
x = 11;
... repeat 95 more times (97 in total)
}
else //when you press enter twice the formatting breaks
{
x=12;
}
}
After this happens all new lines of code are misaligned by 1 tab which gets really annoying. The example I gave is unrealistic but easy to reproduce, in a real scenario just reaching 100+ lines cumulatively (not just in a single block) causes the same behavior. I was wondering if this is a bug or if there is some setting I can adjust or plugin I can install to get the correct behavior. I'm aware that refactoring the code by moving the block code into separate functions to reduce the overall length is an option too, but this is not always the most desirable solution. I was going to report this on https://developercommunity.visualstudio.com/ as well, but I'm not sure if 2017 is still supported.
Also just to reiterate, the question is: Is this a bug or is there some setting I can adjust or plugin I can install to get the correct behavior.
After reporting this issue to the Microsoft team it was noted that this is a known issue. This confirms that it is indeed a bug and not something settings related.
https://developercommunity.visualstudio.com/t/Inconsistent-auto-tabulation-behavior-wh/1429071
While looking through some other reports that were also flagged as duplicates I found this partial solution for VS 2019.
https://developercommunity2.visualstudio.com/t/Visual-Studio-2019-and-C:-Indenting-on/986698
The only way I’ve found to stop this in the Options is to change the
Tabs->Indent from Smart to Block. But unfortunately this also disables
other options that I do want, such as formatting a section when I type
“}” or “;”.

clang-format won't attach brace if there is a newline

I got a problem regarding clang-format:
What I want to enforce is that braces at the start of blocks are always attached to the function head / control sequence ...
This means that instead of
int f()
{
return 1;
}
or
if(o < 1)
{
return -1;
}
clang-format should always attach the opening brace like this:
int f() {
return 1;
}
and
if(o < 1) {
return -1;
}
While this works for the first case (no newline between function head and brace), it does not work if there is a newline between the function head / if etc.
I did not find any option for clang-format that enforces this.
The option closest to what I am looking for is BreakBeforeBraces: Attach, but this did not format the second case properly either. Playing around with this option as well as the options underneath BraceWrapping did not solve the issue as well.
Is there a way to configure clang-format to join lines such as the opening brackets always end up at the end of the last line of code before them?
I played around with online configurators like https://clangformat.com/ or http://cf.monofraps.net/ but could not find an option set that would serve my needs.
I am currently using clang-format version 3.8 .
You could use:
MaxEmptyLinesToKeep: 0
This would cause the empty lines to be deleted, and then the braces would get formatted the way you request.
However, I expect this is not really a good solution for you, because it will delete all empty lines, not just the empty lines before a brace.
Other than this, I don't think this is possible with clang-format 6.0.0. I don't think it has been added in newer versions either - nothing in the documentation seems to relate to this.

CLion inconsistent auto indentation for line comment

There are some weird auto indentation going on in CLion that I don't understand why. Here is an example
int i1(5); // some comments, then I hit Enter
// auto indentation goes here. I hit Enter again
// It goes here
float f = 5; // some comments, then I hit Enter
// then auto indentation goes here. I hit Enter again
// It then goes here.
float f2 = 6;
// If I don't comment on the previous line, there is no weird indentation
It seems like whenever I use = to initialize an variable, and then add comment at the end of the line, it will give me extra indentation on the next line. But in all other instance, it will not give me extra indentation.
I recently installed CLion and it was using default code style. It seems like the the extra indentation is controlled by the continuation indent setting, but I only want this continuation indent setting be application to codes, not comments. Where can I change me setting to disable the continuation indentation for comment when using = for assignment?
This bug has been fixed in CLion 2018.2 EAP (https://www.jetbrains.com/clion/nextversion/ ).
I personally uninstalled CLion 2018.1 because of this bug, since the EAP was stable enough.

break when watched expression becomes true

I'm using visual studio code with the c++ extension and gnu debugger on ubuntu 14.04.
I'm setting a watch expression in my program, which works as expected, but I'd like to trigger the debugger to break when that condition becomes true. How can you do that with visual studio code? The documentation is pretty thin on this subject.
Why don't you add a block that you can break at?
For example, make a condition:
while(true)
{
//stuff is happening
if(condition_happened)
{
std::cout<<"things happened"<<std::endl; //break here
}
}

Make/trick Visual C++ into indenting macros structure properly

Consider these macros
#define BEGIN(Parent) void Process(){
#define ELEMENT(Elem) RegisterElement(Elem);
#define END }
When using them, Visual Studio will not recognize ELEMENT as part of a child code block and thus not indent it properly
BEGIN(ParentClass) <--- hit ENTER here and carret will go below `B` in begin, with no indentation
ELEMENT(m_member)
END
Is there a way to trick Visual Studio, as in a way I could code my macros or maybe some helpful pragmas so that Visual Studio recognizes the opening and closing scope of RegisterElement function and thus indend ELEMENT entries properly? Thanks
Use Code alignment extension for VS 2013/12/10
Here's url: https://visualstudiogallery.msdn.microsoft.com/7179e851-a263-44b7-a177-1d31e33c84fd
From Edit->Code Alignment menu, select 'Align from caret'