Visual Studio 2022 comment shortcuts - c++

Just starting out with C++ and was wondering if anyone can help - in Visual Studio 2022 I can add a comment with the shortcut ctrl+k, c or toggle with ctrl+/ however this just gives me line comments. Is there a shortcut for multi-line like this /* */ ?
I have had a look through the keymapping options but toggleblockcomment still gives me //. I would expect it to use /* unless I'm misunderstanding something.

I suggest you try ctrl+shift+/, but don't use it for header files, if you do you will still get //.

Related

How to stop ReSharper from formatting multi-line comments in C++

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.

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.

Opening curly brace fails to indent consistently

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.

count comments in IDL file using regex and Visual Studio's Find & Replace window

I'm trying to find an easy way to find the number of comment-lines in an IDL file and I don't want to install another extension just for this (also: I don't have Code Analysis, so no metrics). It occurred to me that Visual Studio supports regex searches in the Find In Files search window. So could I count the number of comment-lines in my IDL file using a straightforward regex search?
Unfortunately I am not fluent in regex at all, so the next question is: what would the regex pattern have to be? And if this is any easier: a line with code and a comment appended should count as code and not as comment. Please also beer in mind that Visual Studio has its own regex standard.
Here are some typical examples of comments in the IDL file:
// comment
//++ comment
UserMode, // comment
/*++
comment
--*/
I think the comments that contain ++ and/or -- are some auto-generated comments.
Its Simple.
Find : /\*.*\*/
Repace : anything
Then give replace All.
You will find the count , in a message box 9 matches replaced. You now got the count.
Undo the entire operation, by ctrl + Z
Edit : Response for comment that it is not working.

Preprocessor directives indentation in Visual C++ 2010

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.