Searching and Navigating Code in Visual Studio 2010

Searching and Navigating Code in Visual Studio 2010

  • Comments 32

Every developer knows that navigating code is vital to happy and productive coding. We’ve added several new features for code search and navigation in Visual Studio 2010, including Navigate To and an improved Call Hierarchy, to complement search staples like the Find and Replace dialog and Incremental Search. With such a wide variety of options, we thought it would help to describe many of VS 2010’s code search and navigation features, from old favorites to new additions, in one place:

Find and Replace

Find and Replace

The Find and Replace dialog provides your basic search functionality. It comes in three flavors:

  1. · Quick Find (Ctrl+F): Optimized for smaller searches, Quick Find is best for searching open documents for a string or expression. It also provides basic options for search scope, partial word matching, case sensitivity, searching up vs. down, and use of regular expressions or wildcards in searches.

· Find in Files (Ctrl+Shift+F): A more robust option, Find in Files is better for searching entire projects or solutions. Unlike Quick Find, Find in Files can list search results in the Find Results window and has additional options to specify which file extensions to search.

· Find Symbol (Alt+F12): Find Symbol allows you to search for symbols, including objects (namespaces, types, interfaces, enumerations, etc.) and members (classes, properties, methods, events, constants, variables, etc.).

Want to use the Find feature without the dialog?  Find Next (F3) will take you to the next occurrence of the text in the toolbar’s Find combo box, which is usually your most recent search term.  To start a new search, just select your desired text in the editor and use Find Next Selected (Ctrl+F3) to jump to the next occurrence of that text.

We’ve also heard feedback that the default Find highlight color, currently a pale blue, is difficult to see and makes it hard to quickly find the results of your search.  Good news:  we’ve updated that color since Beta 2, so it will be a darker blue and hopefully easier to see in the Visual Studio 2010 RC and RTM.  For Beta 2, you can change the highlight color in Tools->Options->Environment->Fonts and Colors by updating the background color for “Inactive Selected Text.”  For RC and RTM, you’ll be able to customize the Find highlight using the background color for “Selected Text.”

Incremental Search

Incremental Search

Incremental Search (Ctrl+I) is fast, lightweight, and interactive, allowing you to search for partial words and refine your search in real time. It uses very little screen real estate, doesn’t cover up your code, and is great for quickly finding a piece of code in your current file.  To use Incremental Search, just press Ctrl+I and start typing.  Your search term will appear in the status bar at the bottom of the screen and your search will update as you type.  Press Ctrl+I again to move to the next result and Enter or Escape to end the search.

Navigate To

Navigate To

Navigate To (Ctrl+comma), new for Visual Studio 2010, is a powerful way to search, especially when you’re not sure exactly what you’re looking for. It helps you locate items in your code by using "fuzzy" search capabilities. For example, if you type Foo Bar instead of FooBar, Navigate To will still return useful results. It’s a smart, incremental search that refines as you type and will find any symbols (e.g. file, type, and member names) matching your search terms.

Call Hierarchy

Call Hierarchy

Call Hierarchy (Ctrl+ K, Ctrl+T), or right-click and choose “View Call Hierarchy”, helps you understand the flow of code execution in more complex solutions and projects (C# only). Invoking Call Hierarchy on a method, property, indexer, or constructor in the source code shows you a tree of all the calls to and from that method, property, etc. Each method, property, and constructor in the Calls To and Calls From nodes can also be expanded into its own Calls To and Calls From sub-nodes. Call Hierarchy is great for navigating by code path or understanding relationships between functions.

Go To Definition

Go To Definition

Go To Definition (F12) does what its name suggests: it takes you to the definition of a given symbol. It works for a variety of symbols, including methods, types, classes, members, and more. It’s great for diving into implementation details, especially when reading code.

Navigate Backward/Forward

Navigate Forward and Backward

Navigate Backward (Ctrl+minus) and Navigate Forward (Ctrl+Shift+minus) allow you to quickly move between places you’ve already been in your code. For example, say you just used Go To Definition and now you want to go back to where you came from. It’s easy – just use Navigate Backward. If you’re interested in exactly how this works or how we determine exactly where to navigate you back to, check out Sara Ford’s blog post for details.

Highlight References

Highlight References

Highlight References is another new feature in Visual Studio 2010 (C# and VB only). Any time you place the blinking caret on a symbol, Visual Studio will automatically highlight all instances of that symbol for you. So what does this have to do with navigation? You can actually cycle through these highlighted references – just use Ctrl+Shift+up arrow and Ctrl+Shift+down arrow to move to the previous or next highlighted symbol.

All of these are available in VS 2010 Beta 2, so I’d encourage you to try them out, especially the new-for-VS 2010 Navigate To and Call Hierarchy features. Like what you see? Have suggestions for improvements? Want to share your favorite navigation tips & tricks? Please leave a comment and let us know!

Brittany Behrens
Program Manager, Visual Studio Editor Team

clip_image002I’m Brittany, a Program Manager on the Visual Studio Editor team.  Some of you may recognize me from Connect bugs or as the main voice of @VSEditor on Twitter, and I’m responsible for a variety of Editor features.  I love hearing from customers, so please let me know what you think!

Leave a Comment
  • Please add 5 and 2 and type the answer here:
  • Post
  • Thank you!

  • Thanks, those are all great features. But as native C++ developer, I was always missing the build in function for switch/toggle between header (*.h, *.hpp, *.hxx) and source (*.c, *.cc, *.cpp, *.cxx, *.inl) files. Yes I know, it is pretty simple to implement own macro for this - but it could be very nice feature for default visual studio setup.

  • I've posted a number of times about how you guys are hitting the nail with these features.

    The problem (albeit a significantly positive one) is that you guys have so much more work to do now that these things are coming to fruition.

    Now that we *know* what you're building your features on (WPF) we also *know* that we can expect more.

    Looking forward to ongoing contributions in the code-browsing and code-navigation space, Britany.  Again, great stuff folks!

    My comments on these bits: http://theycallmemrjames.blogspot.com/2010/01/visual-studio-2010-code-browsing-tip.html

    Cheers,

    -jc

  • @samsa

    >I was always missing the build in function for switch/toggle between header (*.h, *.hpp, *.hxx) and source (*.c, *.cc, *.cpp, *.cxx, *.inl) files

    Can you explain a bit more what you mean by this?  There is a Go To Definition command (which generally would take you to the header) and a Go To Declaration command (which would take you to the cpp file).  

    Ryan

  • @CanadianJames

    >Now that we *know* what you're building your features on (WPF) we also *know* that we can expect more.

     Agreed that WPF does make some things easier, but some of these navigation/search issues are much deeper than what kind of UI layer we are using and require a lot of cooperation between the editor, the language services, the shell, etc.  Not to say we aren't thinking about/investigating improvements, just that WPF alone doesn't make it as easy as one may think since VS is a platform and highly extensible and contains many components (both native and managed) and UI that ranges from Win32 WM_PAINT style elements through WinForms and WPF.

    Ryan

  • @Ryan Molden

    Thanks for your response!

    Here are two links for mentioned macros:

    http://www.codeproject.com/KB/macros/h_cpp_switcher.aspx and

    http://www.codeproject.com/KB/macros/hcppswitchermacro.aspx

    The commands "Go To Definition" and "Go To Declaration" are great features. They are very useful for navigation throw already existing c++ source base. But if you developing a new stuff, it is more efficient to have the possibility for quick switch between source and header.

  • @Ryan Molden

    Here is my simplified macro that i use for this purpose:

    Sub ToggleSourceHeader()

    Dim activeDocName As String = DTE.ActiveDocument.Name

    Dim projItem As ProjectItem

    If activeDocName.EndsWith(".cpp") Then

    Dim sourceFileName As String = activeDocName.Remove(activeDocName.LastIndexOf("."), 4)

    projItem = DTE.Solution.FindProjectItem(sourceFileName + ".h")

    ElseIf activeDocName.EndsWith(".cxx") Then

    Dim sourceFileName As String = activeDocName.Remove(activeDocName.LastIndexOf("."), 4)

    projItem = DTE.Solution.FindProjectItem(sourceFileName + ".h")

    ElseIf activeDocName.EndsWith(".h") Then

    Dim headerFileName As String = activeDocName.Remove(activeDocName.LastIndexOf("."), 2)

    projItem = DTE.Solution.FindProjectItem(headerFileName + ".cpp")

    ElseIf activeDocName.EndsWith(".hpp") Then

    Dim headerFileName As String = activeDocName.Remove(activeDocName.LastIndexOf("."), 4)

    projItem = DTE.Solution.FindProjectItem(headerFileName + ".cpp")

    Else

    Exit Sub

    End If

    'open file and catch exception if file doesn't found

    Try

    'projItem.Open(Constants.vsViewKindTextView).Activate()

    DTE.ItemOperations.OpenFile(projItem.FileNames(0))

    Catch e As System.Exception

    'do nothing

    End Try

    End Sub

  • @Samsa

    It does appear that C++ supports 'Go To Header File' (on the editor context menu), but I don't see any 'Go to code file'.  So half of your problem is solved :)  I forwarded your request on to some people on the C++ IDE team and pointed them here to your comment.

    Ryan

  • I do program a lot and this is very useful when searching for codes in a complex projects.

    Thank you.

  • Highlight references is huge! I just wish it was also in the C++ editor. I use the MSVC IDE for embedded work (i.e. I don't use the VC compiler, I just use the makefile project capabilities), and the editor is fantastic. But the competition is the various unix derived editors, and the thing they all have that I wish I had access to is the ability to simultaneously highlight all references to a particular symbol or the result of a particular search.

  • Nice, however there's one feature I've found to be missing in Visual Studio 2010 that was present in all previous versions, and that was the ability to leave the "Replace with" text box blank on the Find and Replace dialog.  This let's you effectively delete any instance of the search term.  Is there any other way of doing this?  Otherwise I think this is a bug.  Maybe not a huge bug, but important to me as I've already run into it several times using the RC build.

  • @Daniel:

    We unfortunately regressed the behavior of Replace All when replacing with the empty string between VS 2010 Beta2 and RC.  This is a known bug for the RC that we've already fixed for RTM, so you should see Replace working correctly with the empty string and any other replace term in the final release of VS 2010.

    Thanks for posting!

    - Brittany

  • VS2010 search & replace - which requires you to tab between the search screen and the target file - is a productivity killer. Is there a setting that makes it work/look like VS2008, i.e. where you can "next occurence" through the file while you're looking it at? I tried the "disappear" settting; wish I had been that good at hide and seek when I was a kid ... they never would have found me ;)

  • Found it: Right click on Find and Replace tab and select Float

  • When I do a find (the find is at the bottom of my screen with the output, error list and tfs pending changes window), and I click on one of the results (or context menu "Go To Location"), the C# file opens in the that lower part, NOT in the main window part at the top with the other C# files.

    How can I change this behaviour? How can I change the Dev Studio 2010 so it opens all C# files in the C# files part?

    Never had this problem with previous versions of the Dev Studio.

Page 1 of 3 (32 items) 123