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
Related
I recently got JetBrains ReSharper and have been using it for C++. However, I have a major pet peeve and cannot seem to find any documentation or customization on this. When I make a multi-line comment using a backslash and asterisks, it inserts asterisks on every line and does not keep the spacing I had, like Visual Studio did before I got ReSharper. Here's what I mean:
ReSharper turns
/*
This on its own line
*/
into
/*
* This on its own line
*/
To add to this, ReSharper also does not keep my spaces from line to line as VS did. For example, in the comment
/*
This is a function, and these features are TODO
> Thing 1
> Thing 2
> Thing 3
*/
vanilla VS would not make me manually press space for the > Thing x text, but would instead keep the indentation from line to line. In ReSharper, I have to not only clear the annoying * at the start of every comment but then manually press space for my indentation with every single line.
My question is how to change this to let VS handle my comment formatting since this is very annoying, especially with larger comments. And for anyone concerned with a style guide, my class has a VERY strict style guide; I would be counted off points on assignments for letting ReSharper keep its default formatting.
Thank you for any help that can be provided. I'm using Visual Studio 2019 with the newest version of ReSharper (as of December 2, 2019).
Sorry, I'm not entirely sure what you mean by "ReSharper turns ... into ...". If you don't want R++ to insert '*' after Enter in multi-line comments, turn off "Insert * on Enter after /*" in ReSharper | Options | Environment | Editor | Behavior.
I agree with your second comment that this behavior should be fixed, I've filed a tracking request - https://youtrack.jetbrains.com/issue/RSCPP-28345. As a workaround, disable "Smart indent on enter" on the same options page.
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.
How do I turn off the "spell-checker like" feature in CodeBlocks on windows?
I also just now realized that if I add a "\" (back-slash) to the end of my comment, the next line if code is also commented. Has this always been standard for c++?
Mine was underlining all my comments and strings, too. Turns out when I downloaded codeblocks, the language wasn't set to English. If you look in the bottom right corner of the codeblocks window, there is a little flag. You can right click it and select the correct language. Hope this helps!
Open Code::Blocks.
Go to plugins -> Manage Plugins
Select Spell Checker and disable it.
Has this always been standard for c++?
Well, rather for the C preprocessor (which C++ uses exhaustively). Yes, it's a documented feature: the backslash-newline sequence acts as a line continuation marker (i. e., the backslash "invalidates", escapes the newline, effectively making the preprocessor treat the consecutive lines separated by backslashes as one line).
The falsely underlined words, might be caused by not having a dictionary selected. This is how I fixed it.
Click Settings->Editor->Spell Checker(on left of dialog) then under Language select a dictionary in the drop down.
I often find myself in situations where I would like to indent preprocessor directives like the rest of the code (e.g. #if indented like if). It seems legal, it's common sense that it's sometimes a good thing, but Visual won't make it easy.
Is there a way to prevent the Visual C++ 2010 editor from sticking the code to the left each time I innocently press #? And to not break preprocessor directives indentation with auto-indent (CTRL+K, CTRL+F)? Or even better, to handle preprocessor directives like everything else?
My approach is to keep the # in the first column and indent the subsequent word, as in:
#ifdef FIRST
# include "first.h"
#else
# include "second.h"
#endif
At some point visualstudio (checked in vs2015) acquired options > text editor > c/c++ > formatting > indentation > position of preprocessor directives. Choose 'leave indented'. The sample doesn't look exactly like what you want, but it works, just try it.
In the Visual Studio 2010 options (Tools->Options)
Go to Text Editor -> C/C++ -> Tabs
Under indenting select Block instead of smart.
This will prevent the # key from forcing you to the start of the line, however if you use Ctrl+K Ctrl+F it will still apply smart formatting and remove the tabs.
Edit: This will also disable automatic indenting/unindenting in other places. Beware.
Workaround: When you first type the # and Visual Studio removes your indentation, press ctrl+z to undo the auto formatting.
I'm trying to get emacs to correctly format the "for each" construct in c++.
I want the braces to be lined up with the f in for in both of the following examples:
for each(Type a in b)
{ //^c^s shows substatement-open
//... do stuff
}
for( ; ; )
{ //^c^s shows substatement-open
//... do stuff
}
In both cases, using [Ctrl-c Ctrl-s] (or [Ctrl-c Ctrl-o]) shows that both opening { characters are of type substatement-open and when viewing the c-offsets-alist I see that substatement-open is equal to 0.
How can I make emacs indent the for each() command similar to how it indents the for(;;) command?
Presumably emacs is not recognising "for each" as c++ syntax (I don't either. Is this a microsoft extension? A preprocessor hack? New for the upcoming standard?). So there is no wonder that it is not formatting it "right".
You could hack the mode, or ask the maintainer of the same (I wouldn't expect a possitive response, though, unless this is going to be proper c++ Very Soon Now).
From the comments The answers to my parenthetical questions are:
The precise form the OP asks about is a Microsoft managed-c++ extension.
There is BOOST_FOREACH available now
The C++0x stand will include a container-ranged loop with a syntax like for( <type> <loopcounter> : <container> )
and from slink's answer it appears that the mode maintainer is supporting the Microsoft syntax in the latest versions...
For me--under Emacs 23.0.91--the c++-mode indents the for each block as it was a normal for loop.