Searching and Navigating Code in VS 2010 (VS 2010 and .NET 4.0 Series)

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.

image 

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:

image

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:

image

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

image

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:

image

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):

image

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:

image

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:

image

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):

image

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:

image

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. 

image

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

image

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:

image

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:

image

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

image

If multiple usages are highlighted, you can cycle through them using the (Ctrl-Shift-up arrow) and (Ctrl-Shift-Down arrow) keystrokes to quickly move the cursor to the previous or next highlighted symbol.

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)

62 Comments

  • mark and will read later

  • Scott, it looks great!!! Should help a lot ...

  • awesome! thank ü ScottGu

  • Nice tutorial. Loving the new features, Call Hierarchy in particular :)

  • Thanks Scott, you ROCK!

  • Easy navigation, finally.
    Now when do you think VS will support CTRL+Click on a type name?

  • That's a good tutorial, when are you coming to Europe..

  • This looks like re-sharper's navigation. I was told to hold off till 2010 release before we looked at getting resharper licences. Are there any other places of overlap between r# 4.5 and 2010? (I'm hoping no, so I can get my boss to spring for r# now!)

  • The call hierarchy feature is great! I've been wanting something like that and wondered why VS could only take you to the methods that a particular block of code calls (like "Go to definition"), but not tell you where *that* code gets called from. Kudos to the VS team.

  • call hierarchy is the best!

  • Imitation is the sincerest form of flattery :-) Eclipse (Java Development Tools) has had all these features for a while now, so I'm thrilled to see them finally make it into VS!

  • I like these new features very much ^_^

  • Those improvements are great. Looking forward to VS2010 even more.

  • Great features! Nice tutorial. Thanks!

    Is VS2010 Pro replacing VS2008 Standard? If so is it decided which version will be available to WebSiteSpark?

  • Absolutely amazing to see how this version is evolving and grows. I loved the highlighted references feature. A lot of tiny features make a big difference (just like Windows 7). Keep doing the wonderful job.

  • Navigate To: Why do you need to press TAB before you can start using cursors to move between results? Why force us to press keys (i.e. TAB) that provide no additional value? Why not just make cursor keys work without tabbing like in DevExpress' CodeRush?

    Please think about our poor wrists and nerves :) - I hope these turn out to be incredibly fast to use.

  • Is the delay in the automatic highlighting intentional?

  • In the Navigate To window, is there any way to specify what type of items you're searching for? For example, can you tell it that, for this search, you only want to look for classes (and not members)? Or that you're specifically looking for an enum? A field? A property? An event? Etc.

    Also: when it's using your filter to find class members, will the filter apply to both the member name and the type name? For example, if I know I'm looking for a field with "cache" in its name, and I know the class where that field is declared has "controller" in its name, can I type "cache controller" and find it? Or would that only find fields that have both "cache" and "controller" in the field name?

  • These features look fantastic but the million dollar question... do they work for unmanaged c++ projects also?

  • Nice article. Not being negative, but it is about time that VS started to catch up with the Eclipse features (all mentioned above are in Eclipse).

    Some questions:
    1) Does "Navigate To" support looking up static files? (e.g. javascript, css, etc)
    2) Is there a way to get "Navigate To" to work on compiled libraries that are referenced in the solution (e.g. if I type "stream" it would bring back all of the classes/functions/etc that have stream in my referenced DLLs)
    3) For the "View Call Hierarchy" can separate keyboard shortcuts be assigned to each feature (e.g. one shortcut to list calls to a function, another for calls from and a third for subclasses)?
    4) For the "View Call Hierarchy" does the feature show both super and sub class overrides of the same function or just subclasses?

    I'd try these out, but I run a Mac...

    Thanks

  • Simply Awesome

  • Great new feature. very helpful for me!

  • This helps a lot and saves developers time and be more productive. I like the select some text and highlights all the places where its been used within that page. Otherwise we would have to do it with Ctrl+F and click F3 to navigate between the selections.

  • VS2010 could have some very basic ReSharper functionality:
    - Show error/warning-bar near the vertical scrollbar. How can you ever refactor an old class without it.
    - Resolve reference and add project reference for missing dll (eg. for System.* and Microsoft.* would be just enough.)
    - Easy way to make one line XML-comments /// ...

  • Oh man, highlighted references are awesome! I use this ALL the time in Notepad++.

    The search stuff seems cool, but it seems like its missing context as input. It appears that searching may in fact return methods, classes, properties - which in some limited scenarios is useful, but really I want global navigation via this feature. Navigating 2 million lines of C# code, I would be overrun with results in our codebase.

    Additionally, the current VS search in files is really useful, now if it only was indexed for instant results, now that would rock! A lot of the time I need to find strings in comments, xml docs, or resx files. I've heard MS internally has such a VS addin...

  • Brilliant Stuff!!

    There's a bug in the Navigate To window (it disappears if there are too many matches, or when scrolling). I've just reported this as a bug.

    But other than that, works great! It's such a small thing but I really like the variable usage highlighting.

  • Hi Scott. I am very glad to see the reference highlighting. (Little things make the biggest difference sometimes.) MyEclipse has had that for a while and I missed it when working with VS2008. MyEclipse also has a nifty feature where if you double click a tab, that tab will maximize to fill the entire IDE window. Double clicking it again resets it back to normal. This would be nice to see in a future release (if it is not in VS2010 already).

  • Can we have an option to enable the lines in the Solution Explorer tree? When you have a big tree, it helps to have those lines around...

  • Great set of features that makes life lot more easier.

  • Isn't it easier that you just buy Jetbrain? (this can also kill Java's best IDE) These feature has been there for years (literally), and in a much user friendly way implemented. It seems microsoft finally find code editor worth some efforts.

  • I miss already a feature "go to file"(file finder) with incremental search. Like delphi

  • That's a good improvement in usability.
    Currently I rely on "Find and Replace" accross Entire Solution dialogue (Ctrl-Shift-F).
    But new search & navigation mechanisms would be a very nice addition to that.

  • Nice to see some plugin features making their way into VS. Not sure if I will be retiring my ReSharper license just yet though.
    It will be interesting to see how the performance goes on a 'real' large project. My Linq2Sql designer file itself is 131000 lines with 300+ classes and ReSharper will bog down with autocomplete when working in the partial DataContext class (VS2008)... Hopefully VS2010 won't suffer the same problem.

  • nice...
    Catching up with ReSharper.

  • the Navigating is quite cool, with it, we can find our methods and properties very quickly. like it.

  • @Bogdan,

    >>>>>>> Now when do you think VS will support CTRL+Click on a type name?

    Can you provide more details on what you are looking for with this? You can click a type name and use F12 today to go to its definition. Was that the scenario you were looking for?

    Thanks,

    Scott

  • @Sumith,

    >>>>>>> That's a good tutorial, when are you coming to Europe..

    I was just in Europe a few weeks ago. :-) I might be coming back in December. I'll blog/tweet if that is indeed the case.

    Thanks,

    Scott

  • @Paul,

    >>>>>>> Are there any other places of overlap between r# 4.5 and 2010? (I'm hoping no, so I can get my boss to spring for r# now!)

    Resharper is great and a lot of devs absolutely adore it. There are other features in VS 2010 that do overlap now more with Resharper functionality (I'll blog about some of them later this week). Having said that, VS 2010 also now has much richer extensibility - so I expect Resharper and other plugin makers to continue to deliver even better value in the future.

    Hope this helps,

    Scott

  • @toth3max,

    >>>>>>> Is VS2010 Pro replacing VS2008 Standard? If so is it decided which version will be available to WebSiteSpark?

    VS Professional has always been there - so I wouldn't think of it as replacing VS Standard.

    WebsiteSpark includes 3 copies of VS Professional (2008 today - 2010 in the future). So you can get 3 copies of VS 2010 Professional with WebsiteSpark (that include all the above features) once it ships.

    Hope this helps,

    Scott

  • @Jussi,

    >>>>>>>>> Navigate To: Why do you need to press TAB before you can start using cursors to move between results? Why force us to press keys (i.e. TAB) that provide no additional value? Why not just make cursor keys work without tabbing like in DevExpress' CodeRush?

    Good news is that I mispoke in my blog post. You actually don't need to click the tab key - just press the arrow keys and it works as well.

    Hope this helps,

    Scott

  • @Jack,

    >>>>>>>> Is the delay in the automatic highlighting intentional?

    There is usually a few second pause. I think it is a combination of both being intentional (to avoid flickering the screen as you move quickly), as well as probably an implementation detail for performance reasons.

    Hope this helps,

    Scott

  • @Mike,

    >>>>>>>>> One question though - I remember a blog post a year or so ago about possible VS features similar to Highlighted References and a comment on that posting lead me to RockScroll (www.hanselman.com/.../IntroducingRockScroll.aspx) Are there any plans to fold any more RockScroll-like features into the final VS2010 code editor? Being able to visualize the entire file in the scroll bar is a very useful tool for me.

    I think we actually have/had something like this built-in (but off by default - although I can't figure out how to turn it on right now ).

    The good news is that the VS 2010 editor is super extensible - so building these types of plugins is now really easy. Expect to see a lot of plugins like this start to appear (written by both Microsoft and other developers/companies).

    Hope this helps,

    Scott

  • Just a couple of comments after playing with Beta 2 for a couple of evenings...

    1. The Start Page automatically closes after a project is opened. Yes! A minor change but much appreciated. It always annoyed me to have to close that page manually.

    2. Selecting a line is subtly different in VS2010 than in previous versions. There's a bit of white space at the beginning of each line between the modified line indicator and the first editable character on the line. In previous versions of VS clicking on that whitespace selected the entire line. In VS2010 I have to click on the modified line indicator area to select the line. Clicking on the whitespace does nothing - in fact the cursor changes back to the normal pointer over that whitespace. Its a subtle change but it means I'll have to break a habit formed from years of use in previous versions - hopefully it's just a beta thing and line selection will revert to the previous behavior in the release version.

    the approx. 1 character's worth of white space between the modified line color bar on the left and the first editable character on the right

  • Scott,

    Great job with the new call hierarchy.

    It would be great to have a list of current partial classes come up in intellisense when defining a new partial class. This would make extending existing partial classes much easier.

    Thanks

  • Hi Scott, thanks for the heads up. These features (especially the call heirarchy) are things I have wanted for a long time. I am looking forward to trying out VS2010.

  • Maybe someone mentioned this already as I haven't read all the comments, but I like the Ctrl + , idea that brings up the search window. However, I was hoping that it would be somewhat like excel where it will show you all instances in your code where that word/call was used. Maybe show a small snippet of the code where it is used and then in a second column, the page and line number where it is found. That would be more handy for me as I could quickly get an idea of the context of the code in the search result and what page it was in so I could doubleclick and quickly get to the right place in my code.

  • Reference Highlighting is not working in my C++ code. Any chance that'll work for production?

    Sadly, I'm getting the "if it aint managed it aint worth our time" vibe on these new features. Call hierarchy also doesn't work with my C++ project either.

  • modal dialog.... please don't....... enough with modal dialogs.
    do something smarter!!

  • @Mike,

    >>>>>>> 2. Selecting a line is subtly different in VS2010 than in previous versions. There's a bit of white space at the beginning of each line between the modified line indicator and the first editable character on the line. In previous versions of VS clicking on that whitespace selected the entire line. In VS2010 I have to click on the modified line indicator area to select the line. Clicking on the whitespace does nothing - in fact the cursor changes back to the normal pointer over that whitespace. Its a subtle change but it means I'll have to break a habit formed from years of use in previous versions - hopefully it's just a beta thing and line selection will revert to the previous behavior in the release version.

    Can you send me email (scottgu@microsoft.com) with more details on this? I have looped in the VS core team and they'd like to work with you to investigate more.

    Thanks,

    Scott

  • Good Demo on the advanced search tool features.

  • What about navigation in JS files, very missing a class viewer for JS objects.

  • Call Hierarchy is cool

  • Scott -

    I dropped you an email (subject line "Line Selection in VS2010") with a screenshot and a bit more explanation.

  • I tried to use this feature in a web site project. It only lists the elements from AppCode fodler. Nothing from the actual website (codebheind file is listed). THe project is in VB and targets framework 3.5 sp1

    Is this a known limitation or a bug?

  • FYI, these features seem to be missing from the Express versions.

  • That is a Great option...

  • I love the new feature

  • Is View Call Hierarchy feature somehow related to Caller Graph?

  • Note: there is also a new visual complement of all this in Visual Studio 2010 Ultimate. See the "Architecture Explorer", and http://social.msdn.microsoft.com/Forums/en-US/vsarch/threads for some pointers to docs and videos.

  • The highlight references feature is great. Now if you only get Refactoring to work just like that (ie. edit and change all references) THAT would be even more awesome (ala Refactor! Pro). The native refactoring in VS could use a little love with its modal dialogs.

  • The View Call Hierarchy is just great. I know the renaming functionality of VS 2008 can help you, if you want to rename a variable or method, but I quite often do want to check where a specific method or property is used, whether I need to rename or not.

  • I like the "Call hierarchy"

Comments have been disabled for this content.