Inside Microsoft® Visual Studio® .NET 2003 [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Inside Microsoft® Visual Studio® .NET 2003 [Electronic resources] - نسخه متنی

Brian Johnson

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید










Syntax Coloring, Line Numbering, and Outlining (or, What the Compiler Saw)


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.


Syntax Coloring


To change the colors used in the Text Editor, go to the Fonts And Colors page located in the Environment folder in the Options dialog box. This page, shown in Figure 3-8, lets you change settings for most of the windows used in the IDE. The settings include printer settings, tool window settings, and the query and view designer settings.


Figure 3-8. The Fonts And Colors page



To customize an item in your Text Editor windows, select the item in the Display Items list and select the Item Foreground color and Item Background color you want the item to use in the editor. Select the Bold check box if you want an item to appear in bold. The Text item governs the overall look of the Text Editor, so if you want to make a drastic change, such as displaying white text on a black background, set those colors first and then customize the rest of your items. This will let you see in the Sample window how other items with the Item Background or Item Foreground settings set to Automatic will look with the custom color you've selected for Text. For example, in Figure 3-9, the Text item is set to display white text on a black background. Clicking the Comment item will show an Automatic background and a Dark Green foreground. You can change the foreground to Green and get much better visibility for the item.


Figure 3-9. Customizing the Text Editor



You can also use the Fonts And Colors page to change the default font used in the dialog boxes and on the Start Page of the IDE. Figure 3-10 shows a tool window after Dialogs And Tool Windows has been customized to use a custom font. You can easily get back to the Visual Studio .NET defaults by clicking the Use Defaults button on the Fonts And Colors page.


Figure 3-10. The Index tool window after the Dialogs And Tool Windows option has been customized



There's one customization on the Fonts And Colors page that can be very useful if you use a standard black-and-white printer to print your code listings or if you do a lot of customization of the Text Editor in the IDE. You can select Printer in the Show Setting For list box to set the fonts and colors used when you print a document. The Use button replaces the Use Defaults button in this circumstance to allow you to use a different set of font and color options when you print. If you're displaying white text on a black background, for example, you probably don't want to print your pages in that format. The Printer settings are kept separate from those for the Text Editor by default. If you want, you can use your custom Text Editor settings for printing by selecting Text Editor Settings from the list shown in Figure 3-11.


Figure 3-11. Customizing printer settings



Here we'll use settings that are separate from those in the Text Editor. By customizing Printer output, you can improve the readability of the code that you're printing. Figure 3-12 shows some printer output after the comments have been set to Bold on the Fonts And Colors page. Keep in mind that this setting doesn't affect the appearance of the code in the Text Editor.


Figure 3-12. Printed source code with comments in bold




Customizing at Microsoft


If you've been to any conferences where Microsoft employees have demonstrated products, you might have noticed that they don't do much customization of the user interface or the Windows environment in which they work. There's a reason for this. There's an informal understanding at Microsoft that if you need to customize your environment (and you want to show that to customers), you should file a bug and argue that the feature you're customizing should be set to your preference by default. (Of course, a customization such as temporarily making the font in the Text Editor larger so audience members can read more easily is exempt from this guideline.) It's not that it's wrong to customize thingsit's just that we strive to always provide the customer with the best experience out of the box. If something needs to be changed to make that experience better, then that setting should be made the default if possible.

If you spend a lot of time customizing the colors and fonts in your IDE, you might want to back up those settings so you can apply them on a different machine or have them in case you need to rebuild. You can back up your custom settings by using Regedit to export the following registry key to a REG file on your machine.


Visual Studio .NET 2002
HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\7.0\FontAndColors


Visual Studio .NET 2003
HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\7.1\FontAndColors

Once you've saved the registry key, you can import the REG file using Regedit if you need to. Restarting the IDE after importing your custom settings will apply the imported settings.


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 .NET 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 enter the following command:


>alias lnon Macros.Samples.Utilities.TurnOnLineNumbers

Now when you enter 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 problemyou 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 click the Assign button. We use the following mapping for the line-numbering macros. For TurnOnLineNumbers we map the keys Ctrl+,, Ctrl+N. (The N stands for numbering.) We've mapped the TurnOffLineNumbers macro to Ctrl+,, Ctrl+Shift+N. Figure 3-13 shows how that shortcut looks after we've assigned it to the macro.


Figure 3-13. 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 .NET 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-14.


Figure 3-14. Adding the TurnOffLineNumbers macro to a menu



You can add a macro like 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 .NET 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-15 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 ellipses in it. Hover your mouse pointer over that box to display a ToolTip that shows some of what's in the collapsed node.


Figure 3-15. 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.

In addition to hiding logical code blocks in the Text Editor, you can collapse an arbitrary selection 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


/ 118