Now that you are aware of the SyntaxHilightTextBox's public properties it is easier to discuss control operations. I should point out that the very name of the control is not altogether apt; syntax highlighting is mainly a background operation. The remaining operations are more like editing aids, and therefore more in the foreground. But the syntax-highlighting aspect is the more unique and more powerful feature, which is why I chose to name it as I did.
Shifting Blocks of Text
Properly formatted codebe it SQL, Perl, XML, or any other languagerelies on visual cues from indentation to facilitate understanding by human readers. The SyntaxHilightTextBox provides two keystrokes to assist with indentation.
- Alt-> shifts code to the right, increasing the indentation.
- Alt-< left shifts code left, decreasing the indentation.
|Figure 14. Quick Indentation: The first row of images shows the effect of selecting a block of text and right-shifting it. The second row selects successively larger, encompassing rows to show nested right-shifting.|
illustrates the utility of this feature. To test it, enter the seven lines of text as shown at the upper left of the figure; the control colors the keywords and the string constant. Advance to the second frame in the top row and mirror the selection you see by selecting the three rows containing the inner select statement. Press Alt->
and the text shifts to the right, as shown in the third frame. The text remains selected so you can continue to operate on it if you wish. Try another Alt->
to shift it even further right, and then bring it back again with Alt-<
. Select all but the first row, as shown at the bottom left. Apply another right-shift and you end up with the final frame shown in Figure 14
Recall the ExpandTab
property discussed earlier. In this demo program it is false, so genuine tabs are used. To confirm this, click at the front of the second "select" keyword, then use the left arrow key to move to the left. A single press of the key should move your cursor below the "where"
keyword. If the program was using spaces, you would have moved only a single character width.
The underlying RichTextBox control does not expose a mechanism for changing the width of a tab. But if you switch to tab-equivalent spaces, then you do. Terminate the program, go to the visual designer, select the SyntaxHilightTextBox and find the ExpandTab property. Change it to true, and change the TabSize property to 2. Re-execute the program and repeat the test process described earlier and you get much less dramatic movements when you right shift.
Left-shifting has a subtle difference from right-shifting. Right-shifting will always add either a tab or tab-equivalent spaces, dependent solely on the ExpandTab property. Left-shifting, however, ignores whether spaces or tabs were inserted; the object of the operation is to remove them, whatever they are! Left-shifting removes white space from the beginning of the line. So, if the first character of a given line is a tab, the operation removes that tab; otherwise, it removes the number of spaces specified in the TabSize property, if present. If neither condition is met, then no action is performed.
Both right- and left-shifting operate on either the current line (when there's no selection) or the lines comprising the current selection. Neither operation requires users to select full lines.
Commenting Blocks of Text
Commenting and uncommenting are conceptually similar to shifting, although the operation inserts or removes comment characters rather than spaces or tabs. Users are free to manually insert either closed comments or end-of-line comments, but the commenting invoked by keystroke commands acts exclusively on end-of-line comments. Closed comments introduce dependencies between lines; a middle line of a three-line comment, for example, is only a comment because line one contains an opening mark and line three contains a closing mark. End-of-line comments allow every line to remain independent so users may then freely comment or uncomment any arbitrary selection of lines without changing semantics of its neighbors.
|Figure 15. Keystroke Commenting: The sequence illustrates successive applications of block commenting, with the end-of-line comment character defined as a double hyphen (--).|
shows a sequence of commenting operations. ExpandTabs
is set to true
for variety (note the smaller indentations in this figure as compared to Figure 14
). In the first frame the cursor, though not visible, is at the beginning of the line containing "two". Pressing the comment keystroke (Control-Shift-C
adds a comment mark to the beginning of that line, as shown in the second frame. In the third frame I changed the selection to encompass two lines, and the fourth frame shows the result of commenting them. The fifth frame is the final result after changing the selection again and performing the commenting operation. As an added exercise, leave the selection where it is and uncomment the selection repeatedly (Control-Shift-U
). The control does what you expectit removes comment markers if present while ignoring lines without comment markers.
Sizing Blocks of Text
A common feature in word processors—but not often in code editors—is the ability to change the size of a block of text. The SyntaxHilightTextBox provides this ability, using the standard Ctrl->
keystrokes to increase and decrease font size, respectively. Rather than make these keys change the font size by a constant amount, I have programmed them to adjust the increment relative to the current font size. So, for example if you start at 8-point type, the keys will adjust font size by a single point. But if you are at 36-point type, the font size will change by 4 points. Imagine if you were at 64-point type and you wanted to go to 24-point, but had to do it in 1-point increments!