Code Navigation Tools

As the lines of code in any given project increase, effectively navigating through the code base (that is, quickly and easily finding lines of interest among the potentially thousands or even millions of lines of code) becomes an issue.

The text editor comes equipped with several tools to help you mark lines of code, search and replace text across source files, and, in general, maintain your situational awareness from within a long code listing.

Line Numbering

As mentioned in the discussion of the text editor’s selection margin, you can enable line numbering for any given document loaded into an editor. This option is controlled in the Options dialog box within the Text Editor, All Languages, General page, or selectively under the individual languages and their General page.

By themselves, line numbers would be fairly useless. The capability to immediately jump to a line of code completes the equation and provides some real benefit from a navigation perspective. While within a text editor, press Ctrl+G to jump to a line of code. This triggers the Go To Line dialog box (see Figure 6.12), which provides a text box for specifying the line number to jump to and even indicates the valid “scope” for the jump by providing a line number range for the current document. Entering a valid line number here moves the cursor position to the start of that line.

Image

FIGURE 6.12 Jumping to a line.

Bookmarks

Bookmarks tackle the problem of navigating through large code files. By placing a bookmark on a line of code, you can instantly navigate back to that line of code at any time. When dealing with a series of bookmarks, you can jump back and forth through the bookmarked lines of code, which turns out to be a surprisingly useful feature. If you are a developer who is dealing with a large base of source code, there are inevitably points of interest within the source code that you want to view in the editor. Recall that the text editor window provides a means of navigating via project, type, and member drop-downs; these are not, however, the best tools for the job when your “line of interest” is an arbitrary statement buried deep within a million lines of code.

Bookmarks are visually rendered in the indicator margin of the text editor. (Refer to Figure 6.8; a bookmark appears on line 7.)

To add a bookmark or navigate through your bookmarks, you use either the text editor toolbar or the Bookmarks window.

You can view the Bookmarks window, shown in Figure 6.13, by choosing View, Other Windows, Bookmark Window. Notice that this window provides a toolbar for bookmark actions and provides a list of all available bookmarks, along with their actual physical location (filename and line number within that file).

Image

FIGURE 6.13 The Bookmarks window.

To toggle a bookmark for a given line of code, you first place your cursor on the desired line within the text editor and then click the Toggle Bookmark button. The same process is used to toggle the bookmark off. Using the Forward and Back buttons within the bookmarks window jumps the text editor’s cursor location back and forth through all available bookmarks.


Tip

Use the Bookmarks window to navigate through code across projects. You are not limited to bookmarks placed within a single code file; bookmarks can, in fact, be in any loaded code file. The list of bookmarks in this window is also a useful mechanism for quickly toggling a bookmark on or off (via the check box next to the bookmark) and for assigning a meaningful name to a bookmark. Right-clicking a bookmark allows you to rename it something more meaningful than Bookmark7.


Bookmark Folders

One interesting feature with the Bookmarks window is the capability to create a bookmark folder. This is an organizational bucket for related bookmarks. For instance, you might want to place bookmarks for a specific math algorithm under a folder called MathFuncs. To do this, you first create a folder by using the New Folder button on the toolbar. You can rename the folder to whatever makes sense for your particular scenario. Then you can create a bookmark and drag and drop it into the folder.

See Figure 6.14 for a look at a populated Bookmarks window. Note that two folders are in use, in addition to bookmarks being shown for various source code files.

Image

FIGURE 6.14 The Bookmarks window with folders.

Call Hierarchy

The Call Hierarchy window is yet another way to navigate through your projects. This window lets you easily follow the calls to and from every method, property, or constructor. With the code editor open, just right-click the member name and select View Call Hierarchy. This launches the Call Hierarchy window. The member name appears in a tree view in the left pane of this window; this tree view itemizes the various calls made to and from the member. If you click one of the calling sites, you can then view the calls to and from that method and so on.

Figure 6.15 shows an example of this iterative information displayed for successive callers. Clicking any of the caller or callee nodes shows you the specific location of that code in the right pane, and double-clicking that information in the right pane immediately jumps the code editor to that line of code.

Image

FIGURE 6.15 Using the Call Hierarchy window to explore code relationships.


Note

Starting with Visual Studio 2012, the Call Hierarchy window works with both Visual Basic and C# code. In earlier versions, only C# code was supported.


The usefulness of this tool doesn’t stop at caller/callee information. If you right-click any node in the tree, you can jump directly to the code that implements the method/property, find all references to the selected method/property within your code, or even directly copy the code content represented by the node. All these commands and more are available right from a node’s right-click pop-up menu.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset