your JavaScript file.
Javascript
// JavaScript file in current folder
/// < reference path="Toolbox.js" / >
// JavaScript file in parent folder
/// < reference path="../Toolbox.js" / >
// JavaScript file in a path relative to the root folder of the site
/// < reference path="~/Scripts/Toolbox.js" / >
// JavaScript file embedded in Assembly
/// < reference name="Ajax.js" path="System.Web.Extensions, … " / >
// Web Service file
/// < reference path="MyService.asmx" / >
// Standard Page
/// < reference path="Default.aspx" / >
A few restrictions exist on how far references directives will work. First,
references directives that refer to a path outside of the current project are
ignored. Second, references directives are not recursively evaluated so only those
in the file currently being edited are used to help build the context. References
directives inside other files in the context are not used.
intellisense oPtions
Visual Studio 2010 sets up a number of default options for your experience with IntelliSense, but
you can change many of these in the Options dialog if they don’t suit your own way of doing things.
Some of these items are specific to individual languages.
intellisense options .
131
General options
The first options to look at are in the
Environment section under the Keyboard group.
Every command available in Visual Studio has a
very specific entry in the keyboard mapping list
(see the Options dialog shown in Figure 7-17,
accessible via Tools .
Options).
You can override the predefined keyboard
shortcuts or add additional ones. The
commands for the IntelliSense features are
shown in Table 7-1.
table 7-1: IntelliSense commands
fiGure 7-17
coMMand naMe default shortcut coMMand descriPtion
Edit.QuickInfo Ctrl+K, Ctrl+I Displays the Quick Info information
about the selected item
Edit.CompleteWord Ctrl+Space Attempts to complete a word if there
is a single match, or displays a list to
choose from if multiple items match
Edit.ToggleConsumeFirst
CompletionMode
Ctrl+Alt+Space Toggles IntelliSense between
suggestion and completion modes
Edit.ParameterInfo Ctrl+Shift+Space Displays the information about the
parameter list in a function call
Edit.InsertSnippet Ctrl+K, Ctrl+X Invokes the Code Snippet dialog,
from which you can select a code
snippet to insert code automatically
(see the next chapter)
Edit.GenerateMethodStub Ctrl+K,Ctrl+M Generates the full method stub from
a template
Edit.ImplementAbstract
ClassStubs
None Generates the abstract class
definitions from a stub
Edit.ImplementInterfaceStubs
Explicitly
None Generates the explicit implementation
of an interface for a class definition
Edit.ImplementInterfaceStubs
Implicitly
None Generates the implicit implementation
of an interface for a class definition
132 .
chaPter 7 inTelliSenSe And bookmArkS
Use the techniques discussed in Chapter 3 to add additional keyboard shortcuts to any of these
commands.
statement completion
You can control how IntelliSense works on a
global language scale (see Figure 7-18) or per
individual language. In the General tab of the
language group in the Options dialog, you
want to change the Statement Completion
options to control how member lists should
be displayed, if at all.
c#-specific options
Besides the general IDE and language options
for IntelliSense, some languages, such as C#,
provide an additional IntelliSense tab in their
own sets of options. Displayed in Figure 7-19, the IntelliSense for C# can be further customized to
fine-tune how the IntelliSense features should be invoked and used.
First, you can turn off completion lists so they do not appear automatically, as discussed earlier in
this chapter. Some developers prefer this because the member lists don’t get in the way of their code
listings. If the completion list is not to be automatically displayed, but instead only shown when you
manually invoke it, you can choose what is
to be included in the lists in addition to the
normal entries, including keywords and code
snippet shortcuts.
To select an entry in a member list, you
can use any of the characters shown in the
Selection In Completion List section, or
optionally after the spacebar is pressed.
Finally, as mentioned previously, Visual
Studio automatically highlights the member
in a list that was last used. You can turn this
feature off for C# or just clear the history.
extended intellisense
In addition to the basic aspects of IntelliSense, Visual Studio 2010 also implements extended IDE
functionality that falls into the IntelliSense feature set. These features are discussed in detail in
other chapters in this book, as referenced in the following discussion, but this section provides a
quick summary of what’s included in IntelliSense.
fiGure 7-18
fiGure 7-19
Bookmarks and the Bookmark Window .
133
code snippets
Code snippets are sections of code that can be
automatically generated and pasted into your
own code, including associated references
and using statements, with variable phrases
marked for easy replacement. To invoke the
Code Snippets dialog, press Ctrl+K, Ctrl+X.
Navigate the hierarchy of snippet folders
(shown in Figure 7-20) until you find the one
you need. If you know the shortcut for the
snippet, you can simply type it and press Tab,
and Visual Studio invokes the snippet without
displaying the dialog. In Chapter 8, you see
just how powerful code snippets are.
xMl comments
XML comments are described in Chapter 12 as a way of providing automated documentation
for your projects and solutions. However, another advantage of using XML commenting in
your program code is that Visual Studio can use it in its IntelliSense engine to display tooltips
and parameter information beyond the simple variable-type information you see in normal
user-defined classes.
adding your own intellisense
You can also add your own IntelliSense schemas, normally useful for XML and HTML editing, by
creating a correctly formatted XML file and installing it into the Common7\Packages\schemas\
xml subfolder inside your Visual Studio installation directory (the default location is C:\Program
Files\Microsoft Visual Studio 10.0). An example of this would be extending IntelliSense
support for the XML editor to include your own schema definitions. The creation of such a schema
file is beyond the scope of this book, but you can find schema files on the Internet by searching for
“IntelliSense schema in Visual Studio.”
bookMarks and the bookMark window
Bookmarks in Visual Studio 2010 enable you to mark
places in your code modules so you can easily return to
them later. They are represented by indicators in the left
margin of the code, as shown in Figure 7-21.
To toggle between bookmarked and not bookmarked
on a line, use the shortcut Ctrl+K, Ctrl+K.
Alternatively, you can use the Edit . Bookmarks .
fiGure 7-21
Toggle Bookmark menu command to do the
same thing.
fiGure 7-20
134 . chaPter 7 inTelliSenSe And bookmArkS
Figure 7 - 21 shows a section of the code editor window with two bookmarks set. The top bookmark
is in its normal state, represented by a shaded blue rectangle. The lower bookmark has been
disabled and is represented by a solid white rectangle. Disabling a bookmark enables you to keep it
for later use while excluding it from the normal bookmark - navigation functions.
To enable or disable a bookmark use the Edit . Bookmarks . Enable Bookmark toggle menu
command. Use the same command to re - enable the bookmark. This seems counterintuitive because
you actually want to disable an active bookmark, but for some reason the menu item isn ’ t updated
based on the cursor context.
Along with the ability to add and remove bookmarks, Visual Studio provides a Bookmarks tool
window, shown in Figure 7 - 22. You can display this tool window by pressing Ctrl+K, Ctrl+W or via
the View Bookmark Window menu item. By default, this window is docked to the bottom of the
IDE and shares space with other tool windows, such as the Task List and Find Results windows.
Remember that toggle means just that. If you use this command on a line
already bookmarked, it removes the bookmark.
You may want to set up a shortcut for disabling and enabling bookmarks if
you plan on using them a lot in your code management. To do so, access the
Keyboard Options page in the Environment group in Options and look for
Edit.EnableBookmark .
fiGure 7 - 22
Figure 7 - 22 illustrates some useful features of bookmarks in Visual Studio 2010. The fi rst feature is
the ability it gives you to create folders that can logically group the bookmarks. In the example list,
notice that a folder named Old Bookmarks contains a bookmark named Bookmark3.
To create a folder of bookmarks, click the New Folder icon in the toolbar along the top of the
Bookmarks window (it ’ s the second button from the left). This creates an empty folder (using a
summary .
135
default name of Folder1, followed by Folder2, and so on) with the name of the folder in focus so
that you can make it more relevant. You can move bookmarks into the folder by selecting their
entries in the list and dragging them into the desired folder. Note that you cannot create a hierarchy
of folders, but it’s unlikely that you’ll want to. Bookmarks can be renamed in the same way as
folders, and for permanent bookmarks renaming can be more useful than accepting the default
names of Bookmark1, Bookmark2, and so forth. Folders are not only a convenient way of grouping
bookmarks; they also provide an easy way for you to enable or disable a number of bookmarks in
one go, simply by using the checkbox beside the folder name.
To navigate directly to a bookmark, double-click its entry in the Bookmarks tool window.
Alternatively, if you want to cycle through all of the enabled bookmarks defined in the project, use
the Previous Bookmark (Ctrl+K, Ctrl+P) and Next Bookmark (Ctrl+K, Ctrl+N) commands. You can
restrict this navigation to only the bookmarks in a particular folder by first selecting a bookmark in
the folder and then using the Previous Bookmark in Folder (Ctrl+Shift+K, Ctrl+Shift+P) and Next
Bookmark in Folder (Ctrl+Shift+K, Ctrl+Shift+N) commands.
The last two icons in the Bookmarks window are Toggle All Bookmarks, which can be used to
disable (or re-enable) all of the bookmarks defined in a project, and Delete, which can be used
to delete a folder or bookmark from the list.
Deleting a folder also removes all the bookmarks contained in the folder. Visual
Studio provides a confirmation dialog to safeguard against accidental loss of
bookmarks. Deleting a bookmark is the same as toggling it off.
Bookmarks can also be controlled via the Bookmarks submenu, which is found in the Edit main
menu. In Visual Studio 2010, bookmarks are also retained between sessions, making permanent
bookmarks a much more viable option for managing your code organization.
Task lists are customized versions of bookmarks that are displayed in their own tool windows. The
only connection that still exists between the two is that there is an Add Task List Shortcut command
still in the Bookmarks menu. Be aware that this does not add the shortcut to the Bookmarks
window but instead to the Shortcuts list in the Task List window.
suMMary
IntelliSense functionality extends beyond the main code window. Various other windows,
such as the Command and Immediate tool windows, can harness the power of IntelliSense
through statement and parameter completion. Any keywords, or even variables and objects,
known in the current context during a debugging session can be accessed through the IntelliSense
member lists.
IntelliSense in all its forms enhances the Visual Studio experience beyond most other tools available
to you. Constantly monitoring your keystrokes to give you visual feedback or automatic code
136 .
chaPter 7 inTelliSenSe And bookmArkS
completion and generation, IntelliSense enables you to be extremely effective at writing code quickly
and correctly the first time. In the next chapter you dive into the details behind code snippets, a
powerful addition to IntelliSense.
In this chapter you’ve also seen how you can set and navigate between bookmarks in your code.
Becoming familiar with using the associated keystrokes will help you improve your coding
efficiency.
8 8
Code snippets and refactoring
what’s in this chaPter?
.
Using code snippets
.
Creating your own code snippets
.
Refactoring code
One of the advantages of using an Integrated Development Environment (IDE) over a plain
text editor is that it’s designed to help you be more productive and efficient by enabling you to
write code faster. Two of Visual Studio 2010’s most powerful features that help increase your
productivity are its support for code snippets and the refactoring tools that it provides.
Code snippets are small chunks of code that can be inserted into an application’s code base
and then customized to meet the application’s specific requirements. They do not generate
full-blown applications or whole files, unlike project and item templates. Instead, code
snippets are used to insert frequently used code structures or obscure program code blocks
that are not easy to remember. In the first part of this chapter you see how using code snippets
can improve your coding efficiency enormously.
This chapter also focuses on Visual Studio 2010’s refactoring tools — refactoring being the
process of reworking code to improve it without changing its functionality. This might entail
simplifying a method, extracting a commonly used code pattern, or even optimizing a section