What are Virtual Spaces in a VirtualSnapshotSpan in the Visual Studio SDK - c#

In the Visual Studio 2010 SDK there is SnapshotSpans and VirtualSnapshotSpans. According to MSDN, the difference is the Virtual Span has Virtual Spaces, the other does not.
What exactly is a Virtual Space?
For reference:
http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.text.virtualsnapshotspan.aspx

You can find an explanation here:
http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.text.virtualsnapshotpoint%28v=vs.100%29.aspx
Virtual spaces are "extra" spaces at the end of a line that do not add
to the length of the line. You can enable or disable virtual spaces in
Visual Studio. When virtual spaces are disabled, pressing on the right
arrow key at the end of a line causes the caret to move to the
beginning of the next line. When virtual spaces are enabled, pressing
on the right arrow key at the end of a line causes the caret to move
forward on the same line.
Even if the option “Enable virtual Space” under Tools -> Options -> Text Editor -> All Languages -> General is disabled, virtual spaces can be inserted when writing indented text or code.
If you type enter at the end of an indented line, Visual Studio adds virtual spaces at the beginning of the new line. These spaces are "virtual" since they have not been typed by the user and will not be saved in the file unless you enter something after the virtual spaces.
This affects the position of the caret:
After pressing enter at the end of an indented line the "BufferPosition"- and "VirtualBufferPosition"-properties of CaretPosition will be different.
BufferPosition points to column 0 in the new line and VirtualBufferPosition points to the position where the caret is visually rendered.

Related

Stop Visual Studio text editor from auto moving comment lines

Is there a way (or tool) to stop Visual Studio text editor (2015 or 2017) from auto moving the comment lines when pressing Ctrl+K,D or relevant key stroke (Ctrl+E,D - format entire document) ?
I would like my comments to stay at the position placed, regardless of the code position above or below.
For example, when writing a comment line starting say at col 80 as
code line
|-------- white space -------------------| //... comment ..
code line
so that it doesn't interfere with the code below or above, when pressing Ctrl+K,D the comment is shifted left to align with the surrounding code.
On the other hand, when a comment is placed following a code line, as below
|----code line ------|-- white space --| //.... 1st comment line ....
|-------white space ---------------------| //.... 2nd comment line....
an eventual 2nd comment line remains aligned with the above one, which indicates that the editor can adjust the position of comment lines based on certain criteria
Edit: I should clarify that I would like to preserve the rest of the functionality of Ctrl+K,D

How can I enable the border around the selected scope in code in Visual Studio?

How can I Enable in VS8-10 the border/line that surround the codeblock where the cursor is? I have looked hard after this I kinda have to give up now. I have googled everything like: border, line, hightlight schope, and surround.
I know it's possible, I have used it before, but can't remember where to enable it.
e.g.:
if
{
if(foo)
{
________________________________________________________________
I var s; <cursor here> // border around this and the next line I
I var t; // border around this line too I
________________________________________________________________
}
}
Download "Ident Lines" in the Visual Studio Gallery or download Resharper.
For Resharper:
Click "RESHARPER" in the toolbar
Click "Options..."
Select "Environment > Editor > Editor Appearance"
Chech "Highlight matching delimiters when caret is"
Check "at both sides"
Check "outline"
In this case, I selected the first brace after MsgBox_MouseDown.
In ReSharper Options / Environment / Editor / Editor Appearance
Enable Highlight matching delimiters when caret is and set Highlight with option to outline.
You can do a rectangular selection by holding the ALT key down while dragging the mouse over a block of text.
May be it was the DevExpress CodeRush plugin you had seen previously, I have not used it myself but this question may help.

How to comment multiple lines with space or indent

In Visual Studio 2010, I have multiple lines of text to be commented:
A
B
C
Using CTRL+E+C to comment out multiple lines, I get
//A
//B
//C
I would like to have a space (or indent) between // and A, hence // A instead of //A.
However, after I group tab a block of text and indent it, CTRL+E+C no longer comments out the selected text.
How to group comment and get the following:
// A
// B
// C
Pressing Ctrl+K+C or Ctrl+E+C After selecting the lines you want to comment will not give space after slashes. you can use multiline select to provide space as suggested by Habib
Perhaps, you can use /* before the lines you want to comment and after */ in that case you might not need to provide spaces.
/*
First Line to Comment
Second Line to Comment
Third Line to Comment
*/
One way to do it would be:
Select the text, Press CTRL + K, C to comment (CTRL+E+C )
Move the cursor to the first line after the delimiter // and before the Code text.
Press Alt + Shift and use arrow keys to make selection. (Remember to make line selection(using down, up arrow keys), not the text selection - See Box Selection and Multi line editing)
Once the selection is done, press space bar to enter a single space.
Notice the vertical blue line in the below image( that will appear once the selection is made, then you can insert any number of characters in between them)
I couldn't find a direct way to do that. The interesting thing is that it is mentioned in the C# Coding Conventions (C# Programming Guide) under Commenting Conventions.
Insert one space between the comment delimiter (//) and the comment
text
But the default implementation of commenting in visual studio doesn't insert any space
I was able to achieve the desired result by using Alt + Shift + up/down and then typing the desired comment characters and additional character.
You can customize every short cut operation according to your habbit.
Just go to Tools > Options > Environment > Keyboard > Find the action you want to set key board short-cut and change according to keyboard habbit.
Might just be for Visual Studio '15, if you right-click on source code, there's an option for insert comment
This puts summary tags around your comment section, but it does give the indentation that you want.

Visual Studio Shortcuts to commented a part of the line, and select a word

Is there any keyboard shortcuts for commented a part of the line? when we want all the line will be comment use ctrl+C but when we need just some selection word between a line be commented what can do?
And is there any keyboard shortcut for select a word, assume your cursor is in the middle of the word and want select all the word (attention because the cursor is in the middle you can not use shift+left(or right)) is there any shortcut for this?
or Is there a keyboard combination to select an entire line?
Ctrl+W selects the current word in Visual Studio. If you are in the middle of a word and want to get to either side of it, Ctrl+Left/Right works great. So, for commenting a sequence of words to the right and if your cursor is in the middle of the first word, you can use Ctrl+Left, Ctrl+Shift+Right, Right, Right... to select all the words you want.
Select a word: Ctrl-W
(Un)comment one line out: Ctrl-K/C & Ctrl-K/U
Selecting a word is even better with Resharper: it allows you to extend your selection.
As for commenting out one word, no shortcuts that I know of.You could however create a macro to do that and hotkey it.
Not exactly an answer, but with CodeRush Express from DevExpress you can use the +/- from the numpad to increase and reduce selection. It offers very useful tools for refactoring.
To answer your question, without an extension this is not possible.

How to enable Virtual Space in AvalonEdit?

I want to achieve "Virtual Space" functionality, similar to one in Visual Studio, in AvalonEdit.
I.e. the caret could be positioned beyond the end of the text line, and if you press any key, there would be spaces automatically added to match.
I am very used to this feature, but neither Googling nor studying AvalonEdit's code gave me any clues on how to enable it, if it is supported at all.
If it is not, suggestions how to extend caret handling mechanisms would be nice.
Thanks!
Edit:
Virtual space support has been added to AvalonEdit in version 4.2.0.8283.
Set textEditor.Options.EnableVirtualSpace = true;.
Below is my original answer.
It's not currently supported.
If you want to try adding it, make sure you read the "coordinate systems" documentation (in the help file on CodeProject). You'll want to extend the "visual column" so that positions after the line end are valid. And you'll have to adjust the position<->column calculations (VisualLine.GetVisualColumn and friends). Use TextView.WideSpaceWidth to figure out the columns past the end of the line.
The above should allow you to use the mouse to place the caret in virtual space. After that, you'll need to change the caret movement (arrow keys, CaretNavigationCommandHandler) and text input logic (TextArea.PerformTextInput) to also support the virtual space.
And if you're successful with the above steps, don't forget to contribute your changes back to AvalonEdit. :-)
While I'm not a fan of virtual space myself, we need something like it to make the RectangleSelection work properly.

Categories