Visual Studio Hacks [Electronic resources]

Andrew Lockhart

نسخه متنی -صفحه : 172/ 142
نمايش فراداده

Hack 97. Collapse and Expand Your Code

Collapsing and expanding regions can be fun, but there's a free add-in that lets you do the same with If, Try, and With statements.

Classify is a Visual Studio .NET add-in that exposes a new toolbar and new context sensitive menu items when editing source code. Classify is largely compatible with C#, though some features are currently specific to Visual Basic .NET. Classify can be downloaded from http://www.visualstudiohacks.com/classify.

The goal of the add-in is to simplify basic coding tasks and to assist in the common aim of any good programmer, which is to make code readable and maintainable.

To assist with readability, Visual Studio .NET has a feature that enables you to collapse, or temporarily hide, sections of code. However, by default, this behavior applies only to basic code blocks like regions, subroutines, and enumeration blocks. What Classify does, for Visual Basic .NET, is take this to the next level.

Auto-Collapse and Scan-Collapse are very similar features, in that they collapse code. Auto-Collapse is invoked at will by the user in each location where code is to be hidden. Scan-Collapse automatically scans the current source file and collapses all sections of code that the programmer is likely to want to collapse.

For example, imagine that you have a subroutine containing a large try . . . Catch block. Within that you have a With...End With and inside that maybe several If . . . End If and While . . . End While blocks. Having nested constructs like this often leads to having a lot of code to read, and once you've read it, you could hide it from view. When hidden, it no longer distracts the programmer or reader. So now, where you can collapse subroutines, you can additionally collapse any logical code block that has a known start and end statement, like If . . . End If.

With Auto-Collapse, you simply right-click the mouse over the starting statement, for example If, and Classify will collapse the code up to and including the End If.

It is important to note that collapsed regions are not persisted: each time you reload your project, your entire source code will be revealed once again. This point makes Auto-Collapse and Scan-Collapse a nondestructive activity. It also means you can freely collapse regions of code, and it will not interfere with other developers who might want the code collapsed in a different way.

In order to install Classify, simply use the Add-in Manager from the Visual Studio .NET Tools menu. Place tick boxes next to the Classify add-in in order to have it activate when Visual Studio .NET is loaded.

Once Classify is active, whenever you right-click in a source code window, you will now see additional items in the pop-up context menu as shown in Figure 13-16.

Figure 13-16. Classify right-click menu

To use Auto-Collapse, simply right-click on a line containing a start keyword, such as If, try, and so on. The following code and Figure 13-17 show a before and after scenario for Auto-Collapse:

If EndLine <> 0 Then
If EndLine > StartLine Then
If FirstWord = "Test" Then
DoSomething( )
Else
DoSomethingElse( )
End If
End If
End If

Figure 13-17. After Auto-Collapse is applied to the first nested If

Note the trailing . . . , which indicates the code can be expanded. One of the strongest ways in which you can use Auto-Collapse is to have Classify scan the entire source file and automatically choose all the code that can be collapsed. To do this, click the Scan-Collapse icon on the Classify toolbar or the Scan-Collapse item on the right-click menu.

After Scan-Collapse has been applied, as in Figure 13-18, you will see all If, Select, and try blocks will be collapsed. Note in this example that even the Case statements within the Select Case block have been collapsed.

Figure 13-18. If, Select, and Try blocks collapsed

Even on a large source file, Auto-Collapse takes only a few seconds to traverse from start to end. This is a great feature to use on source code that you are not familiar with; it allows you to remove the maximum amount of code from view while still being able to peek inside blocks, all helping you to grasp the purpose of the code you are examining.

Chris Nurse