Preprocessor directives indentation in Visual C++ 2010 - c++

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.

Related

clang-format: disable formatting for macros?

I am using clang-format as the auto-formatting tool for my codebase. But some of its features are bugging me.
For instance, I don't want it to format my macro definitions, since most of the time, it's more clear to just formatting them manually. But I don't know how to disable it in clang-format.
Another minor issue is pointer alignment. Sometimes, it's clear to make it aligned left, sometimes it's right. So I'd rather do it by hand. But disable it from clang-format seems impossible?
Any help on these issues?
You can wrap your macros in
// clang-format off
#define ... \
...
// clang-format on
To escape manually editing of each file you can use the regex
search: ^([ \t]*#[ \t]*define[ \t]+.+?\\\r?\n(?:.*?\\\r?\n)*.*?\r?\n)
replace: // clang-format off\r\n$1// clang-format on\r\n
for instance, in Notepad++, Ctrl+Shift+F - "Find in Files - "Replace in Files".
To date (up to v11) there is no way to disable pointer alignment. You can either set the style, or derive the style (clang-format will analyze a file for the most common alignment of & and * and will use it).

How to find all the #if conditions that surround a given line of C++ code?

I'm trying to untangle some code I didn't write. There are a lot of #if statements nested throughout this long file. I'd like a way to quickly identify all the #if statements that surround a given piece of code. Trying to search the Web for "#if" is hard since that either gets hashtags or ignores the punctuation, and "conditional preprocessor directives" didn't turn up anything other than a description of what they are.
Are there any commandline tools that already exist to find all the #ifs affecting a given line?
I'm using Visual Studio 2015 Pro. Are there any extensions that do that?
Not a proper answer but you can use sublime text editor.
In sublime you can select #ifby pressing ctrl + Done by one.
and if you want to find all press ctrl + f and type #if it will select all.
it want take too long to download it. you can download it from here. it is only about 8 mb in windows.

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

Why does CodeBlocks 12.11, on windows, underline my comments with red zigzag lines, and how do I turn this feature off?

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.

Easily comment (C++) code in vim

I have looked at the following question:
How to comment out a block of Python code in Vim
But that does not seem to work for me. How do I comment code easily without resorting to plugins/scripts?
Use ctrl-V to do a block selection and then hit I followed by //[ESC].
Alternatively, use shift-V to do a line-based select and then type :s:^://[Enter]. The latter part could easily go into a mapping. eg:
:vmap // :s:^://<CR>
Then you just shift-V, select the range, and type // (or whatever you bind it to).
You can add this to your .vimrc file
map <C-c> :s/^/\/\//<Enter>
Then when you need to comment a section just select all lines (Shift-V + movement) and then press CtrlC.
To un-comment you can define in a similar way
map <C-u> :s/^\/\///<Enter>
that removes a // at begin of line from the selected range when pressing CtrlU.
You can use the NERD commenter plugin for vim, which has support for a whole bunch of languages (I'm sure C++ is one of them). With this installed, to comment/uncomment any line, use <Leader>ci. To do the same for a block of text, select text by entering the visual mode and use the same command as above.
There are other features in this such as comment n lines by supplying a count before the command, yank before comment with <Leader>cy, comment to end of line with <Leader>c$, and many others, which you can read about in the link. I've found this plugin to be extremely useful and is one of my 'must have' plugins.
There's always #ifdef CHECK_THIS_LATER ... #endif which has the advantage of not causing problems with nested C-style comments (if you use them) and is easy to find and either uncomment or remove completely later.