Searching and Navigating Code in VS 2010 (VS 2010 and .NET 4.0 Series)
 
Published: 21 Oct 2009
Unedited - Community Contributed
Abstract
This article covers some of the new code searching and navigation features that are now built into Visual Studio 2010. Scott provides a detailed overview of the important features such as support for Navigate To, Fuzzy Search Capabilities, View Call Hierarchy and Highlighted References.
by Scott Guthrie
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 19134/ 30

Introduction

Republished with Permission - Original Article

This is the ninth in a series of blog posts I’m doing on the upcoming VS 2010 and .NET 4 release.  In today’s blog post I’m going to cover some of the new code searching and navigation features that are now built-into VS 2010.

Searching and Navigating code

Developers need to be able to easily navigate, search and understand the code-base they are working on.  In usability studies we’ve done, we typically find that developers spend more time reading, reviewing and searching existing code than actually writing new code. 

The VS 2010 code editor adds some nice new features that allow you to more productively search and navigate a code-base, and enable you to more easily understand how code is being used within a solution. 

Searching and Navigating the ASP.NET MVC Source Code

For this blog post I’m going to use the ASP.NET MVC framework code-base (which has many thousand lines of code) to help demonstrate some of the new VS 2010 searching and navigation features.  If you have VS 2010 Beta 2 installed, you can follow along by downloading and opening the ASP.NET MVC framework source code from here.

Figure 1

You should find that the performance of the below features is really fast with this project – despite it being many thousands of lines of code in size.  All of the features I’m demonstrating below are also now built-into VS 2010 (and work for all project types and for both VB and C#).

VS 2010 “Navigate To” Support

Being able to quickly find and navigate code is important with both big and small solutions.

Visual Studio 2010 now supports a new (Ctrl+comma) keyboard shortcut (meaning the control key is held down together with the comma key).  When you press the (Ctrl+comma) combination, a new VS 2010 “Navigate To” dialog will appear that allows you to quickly search for types, files, variables and members within your solution – and then open and navigate to them:

Figure 2

The “Navigate To” dialog provides an fast incremental search UI – with results immediately populating as soon as you start typing search terms.  For example, type “cont” (without pressing enter) and you’ll see that 176 results immediately show up within the results list as you start to type:

Figure 3

Type a few more characters and you’ll see the list automatically filters to just those results that match “controller”:

Figure 4

You can use the scroll bar to scroll through the results – or alternatively press the tab key and then use the cursor arrows if you don’t want to take your hands off the keyboard.  You’ll find that the “Navigate To” window lists all types of results that match your search term – including Type names, Method/Property names, Field declarations, and file names:

Figure 5

Selecting any of the results in the results list will open the relevant source file within VS 2010 (if it isn’t already open) and take you immediately to the relevant source location (and highlight the relevant name within it):

Figure 6

Nice Fuzzy Search Capabilities

The “Navigate To” search box supports some nice “fuzzy search” capabilities that allow you to perform smart filters and searches without having to know exactly the name of the thing you are looking for.  These work well with the incremental/immediate search UI of the dialog – and allow you to refine your searches and get real-time results as you type.

To try this out let’s first search on the word “cache”.  Notice how the search results include not just items that start with the word “cache” – but also display any results that have the word “cache” in it:

Figure 7

We can add multiple words to the search textbox to further filter the results.  For example, below I am filtering the list to only include those that have both “cache” and “action” in the name:

Figure 8

Types and members within the .NET Framework using a naming design-guideline pattern called “Pascal Casing” – which means that the first letter of each word in a Type or Member name is capitalized.  The “Navigate To” dialog allows you to optionally use this “Pascal Casing” convention to quickly filter types. Just type the uppercase first letter of names in a type/member and it will automatically filter for results that match the uppercase pascal naming convention. 

For example, typing “AMS” will filter to the below results (just those types and members that have words in them that start with A then M then S):

Figure 9

The “Navigate To” dialog allows you to quickly filter and locate code with a minimum of keystrokes – and avoid you ever having to use the mouse, open the solution explorer, and click on a file directly.

View Call Hierarchy

Having the ability to quickly search and navigate to code is great.  Being able to also quickly discover how that code is being used is even better.  VS 2010 introduces a new “View Call Hierarchy” feature that allows you to quickly discover where a particular method or property within your code-base is being called from, and allows you to quickly traverse the call tree graph throughout the code-base (without having to run or debug the solution).

To use this feature, simply select a method or property name within your code-base, and then either type the (Ctrl+K,Ctrl+T) keyboard shortcut combination, or right-click and select the “View Call Hierarchy” context menu command:

Figure 10

This will bring up a new “Call Hierarchy” tool window that by default shows up under the code editor.  Below you can see how the “Call Hierarchy” window is displaying the two methods within our solution that invoke the ViewPage.RenderView() method we selected above. 

Figure 11

We can then optionally drill down hierarchically into the first “RenderViewAndRestoreContentType” method to see who in-turn calls it:

Figure 12

For virtual methods/properties you can also use the call hierarchy window to see what types sub-class and override them.

Double clicking any of the members within the “Call Hierarchy” window will open the appropriate source file and take you immediately to that source location:

Figure 13

This allows you to quickly navigate throughout a code-base and better understand the relationships between classes and methods as you code.

Highlighted References

With VS 2010, when you select or highlight a variable / parameter / field declaration within the code-editor, all subsequent usages of it are now automatically highlighted for you within the editor.  This makes it easy to quickly identify where and how a variable or parameter is being used.

For example, when we select the “controllerContext” parameter passed to the ControllerActionInvoker.GetParameterValue() method in the editor below, notice how the 4 usages of it within that method are also now automatically highlighted:

Figure 14

If I select a local variable within the method, all the places it is used are also now automatically highlighted:

Figure 15

Summary

The new VS 2010 text editor makes it easy to quickly search, navigate and explore code within a project or solution.  The performance of these operations is really fast (even with a large code-base) and are kept up to date as you work on the project and make changes to it.  The end result enables you to be much more productive.

Hope this helps,

Scott

P.S. In addition to blogging, I have recently been using Twitter to-do quick posts and share links. You can follow me on Twitter at: www.twitter.com/scottgu (@scottgu is my twitter name)

Resources



User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-23 7:57:11 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search