Search and code navigation in VS 2010

Original author: Scott Gu
  • Transfer
image
Developers should be able to easily search, navigate and understand the code in which they work. We studied usability and came to the conclusion that the developer spends a lot of time reading, revising and researching existing code, instead of writing new code.

VS 2010 editor adds new features that allow you to more efficiently search and navigate in the code, as well as easier to understand how the code is used in the project.

Search and navigation in the source code of ASP.NET MVC

For this article I will use the source code of the ASP.NET MVC framework, which has many thousands of lines of code, to demonstrate some new features in search and navigation in VS 2010. If you have installed VS 2010 Beta 2, you can download the ASP.NET MVC framework .

image

You will find that the performance below the possibilities is really high, despite looking at a large piece of code of several thousand lines. All the features that I will describe are already integrated in VS 2010 and work for all projects in C # or VB.

Support for “Navigate To”

It is very important for small or large projects to be able to quickly search and navigate through the code.

Visual Studio 2010 now supports a new hot key combination (Ctrl + comma), when clicked, a new “Navigate To” window opens, which allows you to quickly find types, files, variables and members in your project and move to their declaration.

image

Thanks to the output of the query input, “Navigate To” gives an increase in search through the UI:

image

Type a few more letters and you will see an automatically filtered list that matches the “controller” query:

image

you can use the scroll bar to move through the search result or use the alternative - press Tab and then use the arrows if you don't want to take your hands off the keyboard. The “Navigate To” list includes all types of results that match the search query, including the names of types, methods, properties, files and declaration of fields.

image

Selecting any result from the list will open the desired source file in VS 2010 (if it is not already open) and will throw you to the right place in the code, highlighting the relevant name.

image

Fuzzy search options

The search field in “Navigate To” can do tricky things, it allows you to filter and search smartly, without really knowing the name of the thing you are looking for. This allows you to filter your searches and receive information in real time, while typing.

To test this feature, let's first look for the word “cache”. Please note that the result includes not only elements that begin with the word “cache”, but also any elements that contain the word “cache”:

image

We can add several words to the search field to further filter the results. For example, below, I filter the list by elements that contain both the words “cache” and “action” in the name:

image

Types and members in the .NET Framework use a naming rule called “Pascal Casing,” which means that the first letters of each word in a type or member name must be capitalized. The “Navigate To” window allows you to optionally use the “Pascal Casing” convention to quickly filter types. It is enough to capitalize the first letters of the names of the types / members, and it will automatically filter out the results that comply with this agreement.

For example, writing “AMS” you will see the result below (only those types and members that contain words that start with A, then with M, then with S):

image

The “Navigate To” window allows you to quickly filter and navigate through the code using minimum clicks and saves you from using the mouse, opening Solution Explorer and clicking directly on the file.

View Call Hierarchy

Of course, having the ability to quickly navigate and search by code is great, but being able to see how the code is used is even better! VS 2010 introduces a new feature - “View Call Hierarchy”, which allows you to quickly find the places in the code from which your methods or properties are called and move around the call tree without launching or debugging the application.

To use this feature, simply select the name of the method or property in the code, press Ctrl + K, Ctrl + T or right-click and select “View Call Hierarchy” from the context menu:

image

This will open a new “Call Hierarchy” tool window, which by default appears under the code editor. Below, you can see how the “Call Hierarchy” displays two methods in our project that call the ViewPage.RenderView () method highlighted above.

image

Further, we can deepen the hierarchy of the first method “RenderViewAndRestoreContentType” to see the call queue:

image

For virtual methods / properties, you can also call the hierarchy window to see what the subclass shows and overload them.

Double-clicking on any member in the “Call Hierarchy” window will open the corresponding source file and move it to the right place in the code.

image

This will allow you to quickly navigate through the code and better understand the relationship between classes and methods while you work.

Highlighted references

D VS 2010, when you highlight or highlight field variables / parameters / declaration in the code editor, all places with them are automatically highlighted for you in the editor. This makes it easy to determine where and how a variable or parameter is used.

For example, when we select the “controllerContext” parameter in the editor that is passed to the ControllerActionInvoker.GetParameterValue () method below, its four places of use in this method are also highlighted:

image

If I select a local variable in the method, all places of its use will also be automatically highlighted:

image

If highlighted several places of use, you can navigate through them using Ctrl-Shift- ↑ and Ctrl-Shift- ↓

Also popular now: