SyntaxEditor has a powerful text selection model that supports both continuous stream (snaking) as well as block selection within editor views.
Accessing the Selection
Each IEditor
The currently active view within a Syntax
Continuous Stream vs. Block Selection
Continuous stream is considered the "normal", snaking type of selection. It has a start and end position and encompasses all text in between the positions.
Block selection, sometimes called column or rectangular selection, allows a singular rectangular portion of text to be selected instead of the whole line. The selection includes all of the characters captured within the rectangle defined by the start (anchor) position and the end position in the selection.
Continuous stream (left) vs. block (right) selection
When a block selection is active, multiple lines can be edited at the same time. Typing will enter the character on each line. Pasting single-line text will paste the text on each line. Likewise, keyboard operations like Del and Bkspace will work on each line.
Getting Information About the Current Selection
There are a number of properties on the IEditor
Member | Description |
---|---|
End |
Gets or sets the end offset of the primary selection range. |
End |
Gets or sets the Text |
First |
Gets the first offset in the primary selection range. |
First |
Gets the first Text |
Get |
Returns an array of Text |
Is |
Gets whether the primary selection range is normalized (Start |
Is |
Gets whether the primary selection range has a zero length. |
Last |
Gets the last offset in the primary selection range. |
Last |
Gets the last Text |
Length Property | Gets the absolute offset length of the primary selection range. |
Mode Property | Gets a Selection |
Position |
Gets or sets the Text |
Ranges Property | Gets the collection of selection ranges in the view. This collection ordinarily contains a single range but can have more than one range when there are multiple selections. Each selection range has its own caret at the selection range's end position. |
Start |
Gets or sets the start offset of the primary selection range. |
Start |
Gets or sets the Text |
Text |
Gets or sets the Text |
Creating Continuous Stream Selections at Run-time
Continuous stream selections can be made via the mouse or the keyboard.
To make a continuous stream selection with the mouse, left click in the text area and drag to create the selection. Hold Ctrl when click+dragging to select by word instead of character, but only if the Syntaxfalse
.
To make a continuous stream selection with the keyboard, hold the Shift key, and press any arrow key. Other keys such as Home, End, PgUp, and PgDn also create selections while holding Shift. Holding Ctrl+Shift when using the arrows keys will select by word instead of character.
Creating Multiple Selections at Run-time
Multiple continuous stream selections can be made when the Syntaxtrue
, the default.
A new selection can be added by holding the Ctrl key when clicking in the text area or clicking on the selection or line number margins.
Press the Esc key when there are multiple selection ranges to collapse back to a single primary selection range.
Most built-in edit actions and editing features are designed to support multiple selections. For instance, pressing Shift+Right Arrow will extend all of the selections by one character. Copying text from multiple selections unions the selected text, joining each range's text with a line terminator. Pasting multi-line text into a view with the same number of selections as text lines being pasted will replace each selection with a related line from the pasted text.
Adding the Next Occurrence
Pressing Ctrl+D adds the next occurrence of the currently-selected text to the selection, or selects the current word if there is no selection.
The IEditor
Creating Block Selections at Run-time
Block selections can be made via the mouse or the keyboard.
To make a block selection with the mouse, hold the Alt key, left click in the text area and drag to create the selection. Hold Ctrl+Alt+Shift when click+dragging to select by word instead of character.
To make a block selection with the keyboard, hold the Shift+Alt keys, and press any arrow key. Holding Ctrl+Alt+Shift when using the arrows keys will select by word instead of character.
Creating a Single Selection Programmatically
The selection can be set programmatically using a number of the properties and methods on IEditor
The Select
When using setter properties or Select
This code selects the text range 0-20
(20 characters) using the current Mode:
This code selects the text range 10-20
(10 characters) using continuous stream mode:
This code selects the text position (line, char) range (1,0)-(2,10)
using block mode:
The IEditor
Creating Multiple Selections Programmatically
The IEditor
Getting or Replacing the Selected Text
The IEditor
This code puts the currently selected text in a string:
For setting text, the Replace
This code deletes currently selected text:
This code replaces currently selected text:
Both these methods have overloads that accept an IEditor
The Is
Determining If Any of the Selected Text is Read-Only
The IEditor
Disabling Certain Selection Modes
The Syntax
By altering the Selection
Selection Change Event
The Syntax
The event passes an Editor
Batch Selection Changes
Sometimes multiple selection changes might be made, such as in the case of performing a text change that alters the selection but wanting to specifically set another selection right afterward. In this scenario, a batch can be created to wrap the text change and any selection changes. The selection changed event will only be raised when the outermost batch is completed.
This example shows how to create a disposable batch object. When the batch is disposed, the batch is completed.
Capturing and Restoring the Selection State
Sometimes it's handy to be able to capture and later restore a selection. This feature is used in actions like indent lines where the text in the selection is modified and when complete, the selection still appears to contain the same relative range of text, even though new characters were inserted within the selected text range.
The IEditor
This functionality makes use of the snapshot translation feature, found in the text framework.
Selection Collapsing
The Esc key can be pressed to collapse the selection.
If there are multiple selection ranges, the Esc key will first remove all selections other than the primary selection.
When there is a single selection range, the selection will collapse to the caret, meaning make the selection go to zero-length. This behavior can be changed so that the selection collapses to the selection anchor instead. The Syntaxtrue
to activate this behavior.
Some end users like the ability to auto-collapse the selection when a copy operation occurs. Set the Syntaxtrue
to enable this behavior.
Selection Grippers for Touch
When the end user touches a view, a small selection gripper will appear below the start and end of the selection. The gripper can be dragged to resize the selection or tapped to show a context menu.
The Syntaxfalse
to disable display of the selection grippers.
Virtual Space
The Syntaxtrue
to allow virtual space at the end of view lines.
This feature permits the caret is able to move beyond a view line's line terminator. Typing a character while the caret is in virtual space past the end of a line will automatically insert enough whitespace characters to fill up to the typed character before it is inserted.
Block selections can be made in virtual space at the end of lines, even without line end virtual space being enabled. This prevents the caret from snapping to an offset when making a block selection, which can sometimes be problematic.