Line Numbering and Outlining


None of what we're talking about in this chapter has anything to do with what happens when you build your applications. Depending on the language you use, compilers remove formatting and white space when a file is processed. At the editor level, however, even the small features provided in the IDE can have a profound effect on your productivity and comfort when you're working with code.

Line Numbering

You can set line numbering on the General page for any of the languages available in the Text Editor folder in the Options dialog box. You can set this option for any specific language, or you can set it for all languages. You can toggle this setting in the Options dialog box, but there is no named command associated with this setting.

To toggle this setting without opening the Options dialog box, you have to run a macro or an add-in to automate that functionality. Two of the macros that are part of the Samples macros set included with Visual Studio were designed to turn line numbering on and off. You can customize these macros yourself, or you can use them from the Command Window, from shortcuts, or by creating new menu commands or toolbar buttons.

The line-numbering macros are Macros.Samples.Utilities.TurnOnLineNumbers and Macros. Samples.Utilities.TurnOffLineNumbers. Either of these can be a finger buster to type into the Command Window, even with the aid of IntelliSense®, so we'll create an alias for each of these commands. An alias is a short command name that's used to represent a longer command in the Command Window. To create an alias for the TurnOnLineNumbers macro, open the Command Window by pressing Ctrl+Alt+A and type the following command:

 >alias lnon Macros.Samples.Utilities.TurnOnLineNumbers 

Now when you type lnon, line numbering will be turned on (if it's currently off). To turn line numbering off, we'll create an alias for the TurnOffLineNumbers macro by typing the following into the Command Window:

 >alias lnoff Macros.Samples.Utilities.TurnOffLineNumbers 

The TurnOffLineNumbers macro is now mapped to lnoff.

Suppose you now want to map these macros to keyboard shortcuts. That's not a problem— you can just search for the word Numbers on the Keyboard page in the Environment folder of the Options dialog box, as we did earlier. Select the macro that you want to map to a keystroke, type your keystroke, and then click the Assign button. We use the following mapping for the line-numbering macros. For TurnOnLineNumbers, we map the keys Ctrl+0, Ctrl+N. (The N stands for numbering.) We've mapped the TurnOffLineNumbers macro to Ctrl+0, Ctrl+Shift+N. Figure 3-8 shows how that shortcut looks after we've assigned it to the macro.

image from book
Figure 3-8: The TurnoffLineNumbers macro with a shortcut key assignment

Setting up line numbering is fairly straightforward, but will we want to toggle line numbering often enough to justify the brain cells it'll take to remember the aliases and the shortcuts we just created? Maybe. But if not, we can store these commands on a menu and then find them there when we need them. To add these macros to a menu, right-click on a toolbar in Visual Studio and choose Customize. On the Commands tab of the Customize dialog box, you'll find a Macros category. With Macros selected, scroll through the Commands list until you find Samples.Utilities.TurnOnLineNumbers. Choose that command and drag it to a menu. The menu you drag it to will expand, and you can place your selected command precisely where you want it.

To customize the new menu command, right-click on it to bring up a shortcut menu. (The Customize dialog box must remain open.) You can rename the new command Turn On Line Numbers to make it a little more readable. Do the same with the TurnOffLineNumbers macro, as shown in Figure 3-9.

image from book
Figure 3-9: Adding the TurnOffLineNumbers macro to a menu

You can add a macro such as this one to a toolbar in just the same way. In that case, you'll probably want to specify a button image to use with the macro that you're adding.

Outlining

The Visual Studio outlining feature is probably familiar to programmers who've used other advanced editors. The idea is to group code by functionality to make it easier to navigate the code in the Code Editor. Figure 3-10 shows a code file in which the outline has been collapsed to the methods in the file. At the end of each collapsed line, you'll see a box with an ellipsis in it. Hover your mouse pointer over that box to display a ToolTip that shows some of what's in the collapsed node.

image from book
Figure 3-10: A ToolTip indicates the contents of a collapsed node when outlining is enabled.

You can turn off outlining by pressing Ctrl+M, Ctrl+P (Edit.StopOutlining). Turning off outlining makes the outlining indicators along the side of the Code Editor disappear completely. You can restart outlining by pressing Ctrl+M, Ctrl+O (Edit.CollapsetoDefinitions). The CollapsetoDefinitions command will restart outlining in the Code Editor and will collapse each code block in the file. Pressing Ctrl+M, Ctrl+L (Edit.ToggleAllOutlining) will open all the collapsed blocks in the Code Editor. You can also type Edit.StartAutomaticOutlining from the Command Window to restart outlining.

In addition to hiding logical code blocks in the Text Editor, you can collapse an arbitrary selection in Visual C++ by selecting some text and pressing Ctrl+M, Ctrl+H (Edit.HideSelection). This function can be very handy for collapsing some code between two distant points in a code file, and it even works to collapse lines in a plain-text file. If you want to expand the collapsed selection, press Ctrl+M, Ctrl+U (Edit.StopHidingCurrent).

Table 3-5 lists the shortcuts associated with outlining in the Text Editor.

Table 3-5: Outlining Shortcuts

Command

Keystroke

Named Command

Stop outlining

Ctrl+M, Ctrl+P

Edit.StopOutlining

Toggle outlining

Ctrl+M, Ctrl+L

Edit.ToggleAllOutlining

Toggle expansion

Ctrl+M, Ctrl+M

Edit.ToggleOutliningExpansion

Hide selection

Ctrl+M, Ctrl+H

Edit.HideSelection

Stop hiding selection

Ctrl+M, Ctrl+U

Edit.StopHidingCurrent




Working with Microsoft Visual Studio 2005
Working with Microsoft Visual Studio 2005
ISBN: 0735623155
EAN: 2147483647
Year: 2006
Pages: 100

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net