Why Does VS 2010 'Comment' Keyboard Shortcut Change in C++? - visual-studio-2010

For me, Visual Studio's Ctrl + K, Ctrl + C keyboard shortcut is used to comment-out the selected lines. When editing C++, this sometimes uses block comments (/* */) and sometimes uses line comments (//). Why does it change? How does it decide which to use when?

A couple other discussions on the topic:
Visual studio feature - commenting code Ctrl K - Ctrl C
visual studio C++ toggle comment ? comment while not whole line is selected?
Based on my own tinkerings, and what was said in those articles...
It's based on the start/end of the selection. It seems to use double slashes // whenever you start your selection at the beginning of the line AND end it at the end of a line.
It will use /* */ notation whenever the selection occurs midway through lines.
IE:
If I have the code
int main () {
return 0;
}
and highlight only int main, it will convert it to /*int main*/.
If I highlight the entire code section, starting after the indent tab, it will convert it to
/*int main () {
return 0;
}*/
But if I highlight the section starting before the indent tab, it converts it to
//int main () {
// return 0;
//}

Summary of links under Zhais' answer. Because following links is hard!
Selecting entire lines (including leading whitespace) will use //
Selecting at least one partial line
If a // comment is included, will use //
Otherwise, will use /* */

Related

Ace editor - how do I remove/reset indent?

I want to change the behavior of the editor such that when the user presses enter on an empty list bullet, their cursor position is reset to the start of the line (rather than leaving them at the indented amount).
I've tried:
aceEdit.moveCursorTo(rowToUpdate, 0)
aceEdit.getSession().indentRows(rowToUpdate, rowToUpdate, "")
aceEdit.getSession().replace(range(rowToUpdate, 0, rowToUpdate, 0), "")
However, all three of these leave the cursor at the previous indent level. How do I reset the indent level for the line?
Update: adding example.
* list
* list
* list
* <- user presses enter here
_
Cursor is where I placed the underscore above, and can't be reset programmatically to the start of the line using what I listed above. (User can backspace the indents to get back to the start.)
You can use editor.setOption("enableAutoIndent", false) to disable automatic indentation.
If you want a way to keep autoIndent in all cases except the list, try creating an issue on ace's github page
If you want to remove the indentation on particular line you can do
var range = ace.Range
var cursor = editor.getCursorPosition()
var line = editor.session.getLine(cursor.row)
var match = /^\s*\*\s*/
if (match) {
editor.session.replace(new Range(cursor.row, 0, cursor.row, cursor.column), "")
}
If you are making a custom mode, you can give it a custom $outdent and related methods - see MatchingBraceOutdent, for example. I think the full set of indentation-related Mode methods is:
getNextLineIndent
checkOutdent
autoOutdent

Visual Studio "Toggle Line Comment" Not Adding // To Already Commented Out Code

I want to comment out a block of Scss code with single line comments (multiline comment will not work b/c I need it to not be processed). In most editors you can select a block of code, and then use a shortcut to simply add // to the beginning of every line. The problem is that in Visual Studio 19 (version 16.7.7), the "Toggle Line Comment" (ctrl+k, ctrl+/) tries to be smart and does not add an extra // to the beginning of a line that already begins with a comment. That's a problem b/c when I toggle the comments off, it then removes comments that were originally there.
This seems really silly that it works this way. Is there some setting or way to change this behavior?
You can use the following command with my Visual Commander extension to add "//" to every selected line:
public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
{
EnvDTE.TextSelection ts = DTE.ActiveDocument.Selection as EnvDTE.TextSelection;
EnvDTE.TextDocument doc = DTE.ActiveDocument.Object("TextDocument") as EnvDTE.TextDocument;
EnvDTE.EditPoint p = doc.CreateEditPoint();
for (int i = ts.TopLine; i <= ts.BottomLine; ++i)
{
p.MoveToLineAndOffset(i, 1);
p.Insert("//");
}
}
The shortcut you need is Ctrl+Shift+/
It comments selected lines by adding // before the first character. Toggles back if pressed again.

Comment Opening and Closing Brackets (with ReSharper?)

Does anyone know if there's a way in Visual Studio 2010 with ReSharper 6.1 to comment out the selected lines of code with their closing brackets - or simply to comment out both the highlighted opening bracket and it's corresponding closing bracket? Here's an example of what I mean:
if(something) {
do(this);
}
I am looking for a hot-key so that when if(something) { is selected, it will comment out if(something) { and }, preferably fixing the tabs once commented like so:
// if(something) {
do(this);
//}
This isn't entirely what you're after, but it's pretty close:
Highlight the code inside the if statement by placing the cursor at one brace and hitting Ctrl + Shift + ].
Now hit Ctrl + Shift + Alt + Left Arrow. This will move the code 'left', i.e. outside of the if statement.
You don't need to comment the if statement out after this because it's empty.
Note that you can also move code 'right' to put it back in the if statement later.

Windows Constants for Ctrl+X, Ctrl+C, and Ctrl+V

I've got some older MFC code I wrote that I'm "freshening up" a bit. I have the following code in a window class' OnChar() handler.
I really don't like using constants like 0x18. I'd like to make the code more readable. I know I can declare my own, but are there no Windows macros for these values? I couldn't find anything about this on the web.
// Check for clipboard commands
switch (nChar)
{
case 0x18: // Ctrl+X - Cut
OnEditCut();
break;
case 0x03: // Ctrl+C - Copy
OnEditCopy();
break;
case 0x16: // Ctrl+V - Paste
OnEditPaste();
break;
}
Do you have some code above there which is subtracting an offset from nChar?
Those values are the letters' places in the alphabet, but I don't think character codes normally work like that. (It has been a long time since I used any of this so maybe I'm just mis-remembering.)
Anyway, the code fragment you have is effectively this (at least on architectures that use the ASCII character ordering, i.e. alphabetic):
// Check for clipboard commands
switch (nChar)
{
case ('X' - 'A' + 1): // Ctrl+X - Cut
OnEditCut();
break;
case ('C' - 'A' + 1): // Ctrl+C - Copy
OnEditCopy();
break;
case ('V' - 'A' + 1): // Ctrl+V - Paste
OnEditPaste();
break;
}
As mentioned in my other comment, I'd expect there to be some other code checking for Ctrl being held down.

AutoHotKey For Loop

I am trying to get a script like this in AHK but I don't know how to write it in AHK:
string arrow
if (leftArrowKeyPressed) {
arrow = "left"
}
if (rightArrowKeyPressed) {
arrow = "right"
}
if (arrow = "left") {
for (int number = 1000; number < 10000; number++) {
simulateKeyPresses(number)
simulateKeyPresses(mousebutton0)
}
}
I did something similar to this. It uses the While command. Your code might look something like the following:
~left::
While GetKeyState("left", "P") {
Send {NUMBER}
Send {MOUSE_BUTTON}
}
Line 1: '~left::' tells the following lines of code to activate when the left button is pressed. The '~' tells the program to still allow the 'left' arrow key to work. If you wanted this code to run and simultaneously block the 'left' arrow from working, remove the '~'.
Line 2: 'While GetKeyState("left", "P")' is self-explanatory. It's a 'while' loop that runs as long as you are holding the 'left' arrow key.
Line 3 and line 4 are for your code to go. Note that 'NUMBER' can be replace by any number 0-9, and 'MOUSE_BUTTON' can be replaced by either the left mouse button (LButton) or the right mouse button (RButton).
I hoped this helped get you started. Also, as formentioned, the AHK Help manual is very informative. You can search 'AutoHotKey Help' on your computer for your off-line version, or visit this link for the online version. These manuals include documentation of just about anything you could ever hope for, as well as a useful example of code at the bottom of each page.

Resources