您的位置:首页 > 编程语言

在VS 2010中搜索定位代码新招盘点 (VS 2010 和.NET 4.0 系列博文)

2009-10-22 11:06 751 查看
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.

这篇文章是为了迎接
VS 2010

.NET
4
正式版前的第
9
篇博文,今天要介绍的是已经内嵌到
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.

新版
VS 2010
的代码编辑器新增了许多新特性,你会逐渐发现搜索定位代码越发地高效,同时更加方便你理解现有的代码结构。

Searching and Navigating the ASP.NET MVC Source Code



搜索定位


ASP.NET MVC


源代码



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

.

这里以
ASP.NET
MVC
架构源文件作为示例,来为大家做下演示,如果你已经安装了
VS 2010
Beta2
,你只需要再下载一下
ASP.NET
MVC
架构的源文件即可,地址如下:
http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24471






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
中啦。

VS 2010 “Navigate To” Support



VS 2010


“定位到”功能



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:

VS
2010
新增了一个“
Ctrl+comma
”快捷键,同时按下,会弹出一个新的“定位到”对话框,你可以用它来快速搜索你项目中涉及的类型、文件、变量及成员等。





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:

这个“定位到”对话框提供了一个简洁快速的查找界面,下面的结果栏会快速显示查询结果。例如,你键入“
cont
”(无需点击回车),你会立即看到所有结果立即显示在你的面前:



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

稍微键入多一些字符后,你会发现列表页也在不断地进行过滤,直到满足你键入的“
controller
”。



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:

你可以拉动结果栏中的滚动条,或者选择按下
tab
键切换到结果项中,并使用方向键来进行再定位,你会发现这个“定位到”窗口列出了所有匹配你键入内容的类型。



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

选中任意结果,并点击该条记录,都会自动通过
VS 2010
来打开相应的文件,并将焦点定位到对应的代码段上。



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:

我们尝试键入“
cache
”这个单词,注意在结果框中不仅出现了所有以
cache
开头的项,同时也列出了一些包含“
cache
”的结果项。



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:

我们可以通过键入多个关键字来对结果进行过滤。例如,下面我要搜索同时包含“
cache
”和“
action
”这两个关键字的结果项:



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.

对于
.NET
Framework
中的类型和成员,我们统一使用一种称为
Pascal
Casing
的匹配方式,也就是说每个类型和成员中独立单位的首字母作为关键字。你可以根据需要使用“定位到”搜索框的“
Pascal
Casing
”特性来过滤结果。只需键入类型
/
成员的名称首字母,系统会自动进行过滤。

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

例如,当你键入“
AMS
”后,你会得到如下的一个结果:



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

快速搜索定位代码实在是太方便了。但能快速查看这些代码的调用关系那就更帅了。
VS 2010
新增了一项“查看调用关系”功能,通过它你可以快速查找当前方法或属性调用来源,并能快速定位到相应位置,不必每次都重新调试。

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:

用法很简单,在工程中任意选择一个方法或者属性名,使用快捷键“
Ctrl+K

Ctrl+T
”,或者右键属性,选择“
View Call Hierarchy
”命令:



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.

指令会在代码编辑器的下方,自动打开一个新的“
Call
Hierarchy
”工具窗口。我们会看到窗口中列出了调用我们所选
ViewPage.renderView()
的两个方法。



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

我们可以打开相应项,查看一下方法的详细调用情况。



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:

双击调用关系窗口中的任何项,便会立即打开相应的文件资源:



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.


VS 2010
中,当你在代码编辑器中选择一个变量、参数或其他代码块时,所有对应的部分都会高亮显示,以后再也不用为寻找变量而发愁了!

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:

例如,当我们选择
ControllerActionInvoker.GetParameterValue()
方法中的“
controllerContext
”这个参数时,注意方法中用到的该变量都会高亮显示!



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

如果选择一个局部变量,所有使用到该变量的地方都会高亮显示:



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.

你可通过快捷键“
Ctrl-Shift-up arrow

”和“
Ctrl-Shift-Down
arrow

”来快速移动你的焦点,很方便哈!

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



原文地址:

http://weblogs.asp.net/scottgu/archive/2009/10/21/searching-and-navigating-code-in-vs-2010-vs-2010-and-net-4-0-series.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐