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

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.

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

Is there a shortcut in VS to reformat a block of code to one line?

I'd like to be able to take this code block
var uVacationLandLubbers = new UserAttribute {
Name = "Land Lubbers",
Project = pVacation,
SystemUserAttribute = context.SystemUserAttributes.Single(x => x.Name == "Yes/No")
};
and reformat it to
var uVacationLandLubbers = new UserAttribute { Name = "Land Lubbers", Project = pVacation, SystemUserAttribute = context.SystemUserAttributes.Single(x => x.Name == "Yes/No") };
Is there a VS/Resharper/Other way of doing this?
This solution would not fully satisy your requirements, but help you considerably.
In Visual Studio, navigate to Tools -> Options.
In Options window, on the left select the language of your choice, say C#. Under C#, select Formatting -> New Lines. Once you select New Lines, a set of rules would be displayed on the right side, uncheck the following rules:
Place open brace on new line for types
Place open brace on new line for anonymous methods
Place open brace on new line for anonymous types
Place open brace on new line for object initializers
Place memebers in object initializers in new line
Place memebers in anonymous types in new line
Note: While unchecking a preview of the rule would be displayed which would help you to view the changes.
Once you do these settings, use this shortcut Ctrl + K + D to format the document.
Use shortcut Ctrl + K + D to format the document code

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.

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

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 /* */

VS 2010: Change color of lines based on a pattern

How can I change color of lines in Visual Studio 2010 based on some custom pattern? For example, I want to change color of all lines that start with logger. . Is it possible at all?
I have ReSharper 5 installed too.
I wrote up a quick little extension to do this; since you'll very likely want to modify it, you should grab the source. The important part is the code in LayoutChanged:
void ViewLayoutChanged(object sender, TextViewLayoutChangedEventArgs e)
{
IWpfTextView view = sender as IWpfTextView;
var adornmentLayer = view.GetAdornmentLayer("HighlightLines");
foreach (var line in e.NewOrReformattedLines)
{
if (line.Extent.GetText().StartsWith("logger.", StringComparison.OrdinalIgnoreCase))
{
Rectangle rect = new Rectangle()
{
Width = view.ViewportWidth + view.MaxTextRightCoordinate,
Height = line.Height,
Fill = Brushes.AliceBlue
};
Canvas.SetTop(rect, line.Top);
Canvas.SetLeft(rect, 0);
adornmentLayer.AddAdornment(line.Extent, null, rect);
}
}
}
To build/run this, you'll need to:
Download the VS2010 SDK.
Create a new project from the editor extension templates (I usually pick Visual C# -> Extensibility -> Editor Text Adornment).
Delete all the source files it creates.
Add HighlightMatchingLines.cs to the project.
F5 to run/test.
If you want to change the brush, change the Fill = Brushes.AliceBlue line.
If you want to change what text is matched, changed the condition in the if expression.
If you want to change what file type the extension is loaded for, change the [ContentType] attribute. The "Content Types" section of this msdn page lists out some of the common ones.

Resources