Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Professional Visual Studio 2005 (2006) [eng]

.pdf
Скачиваний:
117
Добавлен:
16.08.2013
Размер:
21.9 Mб
Скачать

Chapter 20

In addition, you can hide a section of code temporarily if you’re using C# or J#. The keyboard shortcut chord Ctrl+M, Ctrl+H will hide the code currently selected in the editor window. This shortcut is only available when Visual Studio is not outlining the code into automatic regions based on class and procedural definitions.

In previous versions of Visual Studio, you could also perform this function in Visual Basic code, but you can no longer do this with Visual Studio 2005.

Introducing Outlining Commands

The automated regionalizing of class and procedural definitions is available courtesy of Visual Studio 2005’s outlining feature. By default, this functionality constantly analyzes the code and determines when valid regional areas are defined. As soon as a new area has been defined and is capable of being made into a region, the IDE will mark it as such. Conversely, the automated outlining functionality detects immediately when a regionalized area is no longer capable of being a region. For example, Visual Studio will detect the following code as a valid region:

Function MyFunction() As Boolean

Some VB Code

End Function

However, the following code snippet will not be recognized as a region, because the function definition hasn’t been coded correctly (the End Function statement is incomplete):

Function MyFunction2() As Boolean

Some VB Code

End Funct

This automated outlining can be turned off using the keyboard chord Ctrl+M, Ctrl+P. When automated outlining is inactive, only manually defined regions created using the techniques discussed earlier in this chapter can be used to organize the code.

The Collapse to Definitions function is used to collapse the automated regions (and manually defined regions) down to their closed state. You’ll most likely find this more useful than the Collapse All command discussed earlier because you can still see the function definitions within a function, rather than having them hidden inside a collapsed class definition. To invoke Collapse to Definitions, use the Ctrl+M, Ctrl+O keyboard chord.

If you turn outlining off and find that you want it back on, you need to have Visual Studio recreate the automated regions for existing code by first using the Collapse to Definitions command and then expanding the code with the expand all chord, Ctrl+M, Ctrl+L.

The Outlining menu provides an alternative access point to all of these commands. You can access it via the Edit menu (see Figure 20-4) or by right-clicking in the code editor window.

266

Regions and Bookmarks

Figure 20-4

Visual Indicators

The code editor windows in the IDE also provide several visual indicators along with the IntelliSense and automated formatting features of Visual Studio 2005. These include color-coding of the margin and icons to represent breakpoints, bookmarks, and search results. The following sections explain what each of these indicators mean.

Color Coding

When code is first opened or generated in the IDE, the side margin is left as is to indicate that the code is original code. However, as you edit the code, and build and debug the solutions, the margins change color to show that changes have occurred.

When code is added or changed in a module, the margin will turn yellow. Once the changed code has passed through the build process and has been compiled successfully, the margin color changes to green. Figure 20-5 shows the results of a subroutine that was first created and compiled, with an additional line added subsequent to the build.

267

Chapter 20

Figure 20-5

Note that color-coding remains only for as long as the editor window is open. Once you close the editor window and reopen it, the margin will be reset to its default state.

Margin Icons

Figure 20-6 shows the Code view for a form with a number of icons in the left margin. Each of these represents a different indicator so you can see at a glance what’s significant about the particular line.

Tracepoint

Breakpoint

Active Bookmark

Inactive Bookmark

Figure 20-6

From top to bottom, the icons represent a breakpoint, a tracepoint, an inactive bookmark, and an active bookmark.

Because lines of code can have multiple states, these icons can be overlaid on top of each other. Figure 20-7 shows the same code window during a debug session with an additional icon indicating the current line of code being executed. Note how this icon overlays the pre-existing tracepoint icon.

268

Regions and Bookmarks

Figure 20-7

Breakpoints and tracepoints are discussed in Chapter 49.

Bookmarks and the Bookmark Window

Bookmarks in Visual Studio 2005 enable you to mark places in your code modules so you can easily return to them later. As mentioned in the previous discussion, they are represented by a visual indicator in the left margin of the code, but they are also listed together in a Bookmarks tool window (discussed in a moment).

To toggle a bookmark, use the keyboard shortcut chord Ctrl+K, T. Ctrl+K indicates to Visual Studio that the chord is related to bookmarks, while the T invokes the toggle command. Alternatively, you can use the Edit Bookmarks Toggle Bookmark menu command to achieve the same goal.

Remember that toggle means just that. If you use this command on a line already bookmarked, it will remove the bookmark.

Figure 20-8 shows a section of the code editor window with two bookmarks. The top bookmark is in its normal state, represented by a shaded blue rectangle. The lower bookmark has been disabled and is represented by a solid white rectangle. Disabling bookmarks enables you to keep the bookmark for later use while excluding it from the normal bookmark navigation functions.

Figure 20-8

To disable a bookmark, use the Edit Bookmarks Enable Bookmark toggle menu command. Use the same command to re-enable the bookmark.

269

Chapter 20

You may want to set up a shortcut for this if you plan on using bookmarks a lot in your code management. To do so, access the Keyboard options page in the Environment group in Options and look for Edit.EnableBookmark. A good shortcut to use is Ctrl+K, E, as it is consistent with the standard Ctrl+K chords for bookmarks and is easy to remember.

Along with the capability to add and remove bookmarks, Visual Studio provides a Bookmarks tool window, as displayed in Figure 20-9. By default, this window is docked to the bottom of the IDE and shares space with other tool windows such as the Task List and Find Results windows.

Figure 20-9

Figure 20-9 illustrates some useful features of bookmarks in Visual Studio 2005. The first is the capability to create folders that can logically group the bookmarks. In the example list, notice a folder named Variables with a bookmark named PersonName (presumably marking a variable of the same name) inside it.

To create a folder of bookmarks, click the New Folder icon in the toolbar along the top of the Bookmarks window (it’s the second button from the left). This will create an empty folder (using a default name of Folder1, Folder2, and so on). Right-click the entry and select Rename from the context menu to choose a different name.

Move bookmarks into the folder by selecting their entry in the list and dragging them into the desired folder. Note that you cannot create a hierarchy of folders, but it’s unlikely that you’ll want to. Bookmarks can be renamed in the same way as folders, and for permanent bookmarks this can be useful, rather than accepting the default names of Bookmark1, Bookmark2, and so forth.

To navigate to a bookmark, double-click its entry in the list. Alternatively, if you want to cycle through all of the bookmarks defined in the project, use the Previous Bookmark and Next Bookmark commands. You can restrict this navigation to only the bookmarks in a particular folder by first selecting a bookmark in the folder and then using the Previous Bookmark in Folder and Next Bookmark in Folder commands.

The last two buttons in the Bookmarks window are the Toggle All Bookmarks command, which can be used to disable (or re-enable) all of the bookmarks defined in a project, and the Delete command, which can be used to delete a folder or bookmark from the list.

Deleting a folder will also remove all of the bookmarks contained in the folder. Visual Studio will provide a confirmation dialog to safeguard accidental loss of bookmarks. Deleting a bookmark is the same as toggling a bookmark off.

Bookmarks can also be controlled via the Bookmarks submenu, which is found in the Edit main menu.

Unlike previous versions of Visual Studio, bookmarks in Visual Studio 2005 are retained between sessions, making permanent bookmarks a much more viable option for managing your code organization.

270

Regions and Bookmarks

Task Lists are a customized version of Bookmarks that are now displayed in their own tool window. The only connection that still exists is that there is an Add Task List Shortcut command still in the Bookmarks menu. Be aware that this does not add the shortcut to the Bookmarks window, but instead to the Shortcuts list in the Task List window.

Summar y

In this chapter you’ve seen how Visual Studio can provide even more visual cues for you to work with: automated ones such as the color-coded margins, manually instigated indicators such as margin icons for bookmarks and breakpoints, and code regions for easier code management. Using these techniques will help improve your coding efficiency in simple ways.

271

Refactoring

One of the techniques that has received a lot of attention recently is refactoring, the process of reworking code to improve it without changing its functionality. This might entail simplifying a method, extracting a commonly used code pattern, or even optimizing a section of code to make it more efficient. This chapter reviews the refactoring support offered by Visual Studio 2005. Unfortunately, because of the massive list of functionality that the VB.NET team tried to squeeze into the product, support for a wide range of refactoring actions just didn’t make the cut. Luckily for VB.NET developers, Microsoft has come to an arrangement with Developer Express to license the VB version of their Refactor! product for anyone using Visual Studio 2005. Refactor! can be downloaded from the Visual Basic developer center at http://msdn.microsoft.com/vbasic/, as illustrated in Figure 21-1.

Figure 21-1

Refactor! provides a range of refactoring support that complements the integrated support available for C# developers. However, this chapter’s discussion is restricted to the refactoring support provided within Visual Studio 2005 and the Refactor! product.

Chapter 21

Accessing Refactoring Suppor t

Visual Studio 2005 makes use of both the main menu and the right-click context menu to invoke the refactoring actions. Refactor! only uses the context menu to invoke actions, although it does offer hints while you’re working.

C# — Visual Studio 2005

Refactor support for C# developers is available via the Refactor menu or the right-click context menu, as shown in Figure 21-2.

Figure 21-2

The full list of refactoring actions available to C# developers within Visual Studio 2005 includes Rename, Extract Method, Encapsulate Field, Extract Interface, Promote Local Variable to Parameter, Remove Parameters, and Reorder Parameters. You can also use Generate Method Stub, and Surround with Snippet, which can be loosely classified as refactoring.

VB.NET — Refactor!

Refactor support for VB.NET developers is available via the right-click context menu, as shown in Figure 21-3.

As you work with your code, Refactor! is busy in the background. The context menu is dynamically changed so that only valid refactoring actions are displayed. Refactor! also offers useful hints that can be used to invoke refactoring actions, as shown in Figure 21-4.

274

Refactoring

Figure 21-3

Figure 21-4

The limited refactoring support provided by Visual Studio 2005 for VB.NET developers includes Rename and Surround with Snippet. Refactor! adds support for much, much more: Create an overload, Encapsulate a field, Extract a method, Extract a property, Flatten conditional statement, Inline temporary variable, Introduce a constant, Introduce local variable, Move declaration near reference, Move initialization to declaration, Remove assignments to parameters, Rename, Reorder parameters, Replace temporary variable with method, Reverse conditional statement, Safe rename, Simplify conditional statement, Split initialization from declaration, and Split temporary variable.

Refactoring Actions

The following sections describe each of the refactoring options and provide examples of how to use built-in support for both C# and Refactor!

Extract Method

One of the easiest ways to refactor a long method is to break it up into several smaller methods. The Extract Method refactoring action is invoked by selecting the region of code you want moved out of the

275