Microsoft Visual Studio 2010 RTM is now here and developers are excited about its many new features. Even the venerable code editor has got its share of new features. These additions can make a big difference in how developers write and navigate code. Read on to learn how they work!
Many of the new features in Microsoft Visual Studio 2010 are related to developer productivity. And when it comes to the code editor, almost no other window in Visual Studio puts equal hours into the clock. Thus, Microsoft needs to make sure developers are productive, and stay so from version to version.
In Microsoft Visual Studio 2010, the code editor has gotten a facelift with the new, WPF based implementation. Alone, this would have been a nice technical advancement, especially when considering that all the previous features are still there. The new editor technologies also help provide cleaner visualizations and zooming (as you will see later). On top of that, Microsoft wanted to listen to developers and add additional features to the editor.
These new features both help developers write code and navigate in applications that are getting larger and larger. The features also help you to be more productive on modern hardware. Most of the new features are available to all supported languages: C#, Visual Basic .NET, C++ and F#.
A WPF Based Code Editor
Technically speaking, the biggest change in Visual Studio 2010 code editor compared to the previous Visual Studio versions is the all-WPF editing window (Figure 1). Migrating from a traditional Windows GDI interface to modern WPF technologies allows many advanced improvements in the future. Programming custom extensions is also easier, thanks to the new Managed Extensibility Framework, MEF.
Figure 1. Visual Studio 2010 has a new WPF based code editor
However, in the 2010 version the improvements in visualization features are still quite few. Nonetheless, cleaner views and the ability to zoom freely are features that help developers code more efficiently and pleasantly. For instance, a small but useful gray highlight (with the default color scheme) allows you to more easily see the block of code that is to be collapsed using the code outlining feature. The shading appears when you move the mouse over the minus sign on the left code editor margin (Figure 2).
Figure 2. A (slight) gray highlight helps in identifying blocks of code
The zooming feature is now available in Microsoft Visual Studio 2010 code editor, which is without doubt the most important text window in the product. However, the Visual Studio documentation also mentions zooming should be available on other text based windows such as Output and Immediate windows. However, this appears not to be the case in practice, as zooming won't work the same in these windows as in the code editor.
Nevertheless, zooming in the code editor works just fine. If you are a mouse person, then hold down the Ctrl key while scrolling the wheel of your mouse to zoom in the code editor, just like for instance in Internet Explorer. If you instead prefer to use the keyboard, then the shortcut keys Ctrl+Shift+comma and Ctrl+Shift+period are your friends (Ctrl+Shift+, and Ctrl+Shift+.). The maximum zooming level is 400% (this ought to be enough); at the other end, 20% is the minimum zoom.
Notice also how there is a small zoom box at the bottom left of the code editor window (Figure 3). From there, you can select the zoom level you prefer. Of course, you can still change the font and font size used by the code editor; this affects how large the font will appear at 100% zoom. By the way, the new default font is now named Consolas, with a default font size of 10 points.
[Figure 3 - Zoom Box.pngFigure 4 - Vertical Edits.png]
Figure 3. The zoom box allows you to both see and change the zoom level in the editor
Selecting Boxes and Floating the Windows
Many of the new features in Visual Studio 2010 code editor directly mean improved productivity. Let's for example take a basic function such as selecting text. Every developer is aware of the ability to select code with a mouse gesture or the Shift+arrow keys, but sometimes, it would be appropriate to select an area that is rectangular. Rectangular selections have been possible for years, and can be made by holding down the Alt key while using the mouse or the normal keyboard shortcuts of Shift+arrows.
In Visual Studio 2008 for instance, the rectangular selection feature allowed you to quickly copy and paste rectangular areas of code, or delete for example a column of characters from your code. But in Visual Studio 2010, this neat feature has been extended by supporting insertion of text typed into each line simultaneously. This feature is useful for example changing variable names, private methods to public, and so on.
Another related new feature is the support for zero-length columns; now you can easily insert a new word for example to multiple lines at once. When the zero-length column is selected, a tiny narrow blue highlight is shown, indicating where typed characters will be inserted (Figure 4).
[Figure 4 - Vertical Edits.png]
Figure 4. Vertical editing is a three-step process
Often when editing code in previous versions of Visual Studio, you found yourself switching between different code editor windows, or the visual design time view and the code-behind file. Thus, showing multiple documents simultaneously was somewhat difficult: you either had to create multiple tab groups, or use the Multiple documents window layout option in Tools/Options, Environment/General group. But even so, you could not move editor windows outside the main Visual Studio window.
Now in Visual Studio 2010, you can freely float almost any window wherever you prefer. For instance, you can simply start dragging a file tab at the top of the code editor area and drag it to a convenient location, such as another screen if you have multiple monitors. Or, you could dock the window so that you can see at the same time both the designer (such as the WPF designer) and your C# code (Figure 5). Note that you could do the same in Visual Studio 2008, but it was more difficult than it is now in Visual Studio 2010.
[Figure 5 - Simultaneous Code and Design.png]
Figure 5. Showing the code at the same time as the design-time view is now easier than ever
Finding and Navigating Code
The larger applications you are creating, the more time will be spent in navigating inside the source code of your application. Visual Studio 2008 and the preceding versions were not particularly bad in this, but in Visual Studio 2010, Microsoft still found room for improvement. One of these new features is the ability to quickly go from one place to another with the new Navigate To feature (Figure 6).
[Figure 6 - Navigate To.png]
Figure 6. The Navigate To is a new feature in Visual Studio 2010
You can activate this feature by pressing Ctrl+comma in the editor. With the mouse, use the Edit/Navigate To menu command instead. The command allows you to quickly search code elements, such as properties, methods, fields and even filenames inside your whole solution and all files you have open. Initially choosing the "Navigate To" command will bring an empty dialog box to the screen. After you start typing a few letters into the search field, Visual Studio will start to search elements that match what you have typed. For instance, if you type "Hom" in an MVC application, classes such as HomeController will be shown, but also the code files containing the word typed, such as
The search is also intelligent in that it supports the conventional camel (or Pascal) casing of identifiers. For instance, if you have a method called
ChangePassword, you can find it by simply typing "CP", as the letters C and P are in capital letters in the name. Note that typing "cp" with small letters will not find the method; you must use capital letters to ignite the camel casing search. You can also type in multiple letters or words by separating them with a space for a Logical And operation. Thus, the search "C L" would find the file
LogOnUserControl.ascx in an MVC application, because both the letters C and L in capital can be found in the filename.
If matches are found in multiple projects, the project name will be displayed after the name of the matching element. To open the desired element, simply select it with arrow keys, and press Enter, or click the OK button with your mouse. Note that at the bottom of the Navigate To dialog box, you can find an option called "Hide external items". When this option is selected (the default is off), the search will not include files that are not part of your solution or project(s).
The Navigate To feature is a great new addition to the product, but so is the smaller new feature called Reference Highlighting. This feature activates itself whenever you move the blinking caret in the code editor over a symbol: almost immediately, all references to the selected symbol are shown with a distinct background color (Figure 7). This allows you to easily see where the selected symbol is used, and you can even use the keyboard shortcuts Ctrl+Shift+Down and Ctrl+Shift+Up arrow to move the caret between the references.
[Figure 7 - Highlight References.png]
Figure 7. By highlighting references you can both see and move between the references easily
Note that the highlight feature works inside the whole document, not just a given method, for instance. Unfortunately, you cannot yet simply start typing and changing the name of all the selected references like you can do for instance in Embarcadero Delphi, but that is what the Visual Studio Rename refactoring command is for.
Another useful code navigation feature is called Call Hierarchy. This feature is available for C# and C++ developers, and can be activated by first selecting a member in the code editor, and then pressing the Ctrl+K,T keyboard shortcut. You can also right-click a member, and choose the similarly named command from the popup menu. Either way, you will end up in a new window shown in Figure 8.
This window will quickly show you which methods call your member, and which other methods the selected member uses. By opening the
Calls To and
Calls From sub-nodes, you can easily see how your calls are constructed, and you can even quickly go from code location to another by double-clicking a node on the list. The Details panel on the right shows you the code file and line number of the selected member. Note that you can show multiple members at the same time in the window; to remove unneeded references, simply select the topmost level node in the tree, and click the red X button, or hit Del.
[Figure 8 - Call Hierarchy.png]
Figure 8. Call Hierarchy quickly shows you how a method is being used
Remember also to check out the popup menu while right-clicking the tree nodes to reveal additional options. The Call Hierarchy window itself can be shown through the Edit/Call Hierarchy menu command or the Ctrl+W,K keyboard shortcut.
Benefitting From the Improved IntelliSense
In Visual Studio, the IntelliSense feature is one of the biggest developer productivity aids. Every developer is familiar with at least the List Members feature in the code editor. It suggests symbol and code snippet names while you start typing. In Visual Studio 2010, there are several new features, each of which is useful for almost any developer.
First, you should become aware of the slight change in the List Members command (Figure 9). In previous Visual Studio versions, whenever you typed the period "." after a symbol name, the List Members feature would show you a list of suggested properties, methods, and so on. Typing a few letters after the period would filter the list with words starting with the letters typed.
[Figure 9 - Completion Mode.png]
Figure 9. IntelliSense has got new tweaks in how members are filtered
Now in Visual Studio 2010, the list will filter with both names starting and containing the characters typed. Thus, if you type in "Cont" after the period, you can get a list suggesting ContextMenu and ContextMenuClosing, but also DataContext. This helps you select a correct member even if you are not sure with which characters the name starts with. Note that the filtering will work the same as in previous Visual Studio versions if you invoke the List Members feature manually with Ctrl+Space (or Ctrl+J).
IntelliSense also has a new feature called Suggestion Mode in Visual Studio 2010. This feature can be toggled on and off by pressing Ctrl+Alt+Space while the Members List window is visible. By default, a mode called Completion Mode is in use, and in this case, Visual Studio will try to help you select a correct member name. But by activating the Suggestion Mode, Visual Studio respects what you are typing, and merely suggest making a good choice (Figure 10).
[Figure 10 - Suggestion Mode.png]
Figure 10. The Suggestion Mode is a new non-intrusive way to benefit from the member list
In Suggestion Mode, the text you are typing is shown at the top of the list, and usual suggestions are shown below. However, in the Suggestion Mode, no option is actively selected, but instead indicated with a hollow selection rectangle. If you wish to select an option from the list, simply use the arrow keys or your mouse, and then press Enter, Tab or continue typing with, for example, a period or semicolon. If you simply press for instance Enter in the Suggestion Mode, then only what you typed is inserted.
Contrast this to what often happened in Visual Studio 2008 (or in Visual Studio 2010 when you are using the default Completion Mode) if you typed a new member name that matched the beginning of an existing longer member name. When you continued writing and typed the period key, Visual Studio would often irritatingly insert the longer member name, even if you wanted to only type in the shorter name.
Because writing code that makes a call to a method before the method itself is written is so common, Microsoft implemented the Suggestion Mode feature. In this mode, no false additions are made unless you request them to be added. Note that there's no indicator itself in the Members List window whichever mode is active. However, you can easily spot the Suggestion Mode from the single dividing line at the top of the list; this line does not exist in the default Completion Mode.
The previously mentioned support for camel casing is also available in IntelliSense with Visual Studio 2010. For instance, if you have a property named MaxWidth, you can see that property listed whenever you type "MW" after the period, for example.
Another nice feature for developers that like to write code that uses members and classes before they are defined, is called Generate From Usage (Figure 11). This new feature is also very useful whenever you prefer to write your unit tests before writing the actual implementation, as in Test-First Development.
[Figure 11 - Generate From Usage.png]
Figure 11. New properties and fields can be created with just a few clicks
To use the "Generate From Usage" feature, type for instance a new assignment statement, and then press the Ctrl+period shortcut key. Or, move the mouse cursor over the small blue rectangle below the first letter to show a smart tag, and then click the tag. A popup menu will now open with appropriate options, such as "Generate property stub" or "Generate field stub". The feature also works with methods (this was introduced in Visual Studio 2008), and also with classes whenever you create a new object initialization with a code line such as:
MyNewClass mnc = new MyNewClass();
In this case, pressing Ctrl+period at the end of the statement will show the commands "Generate class for '
MyNewClass'", and also "Generate new type". By selecting the latter command, Visual Studio will show a new dialog box on the screen, shown in Figure 12. As the name of the dialog box indicates, you can use this feature to add new types to your project.
[Figure 12 - New Type.png]
Figure 12. Creating basic code for classes and structs can be done through the new Generate New Type feature
For example, you can specify the wanted access specifier, kind (as in class, struct, enumeration or interface) and location. By default, a new file will be created for new types, but you can also store the new type in an existing code file. The latter option is useful if you are creating just a small new class, or perhaps an enumeration.
Visual Studio 2010 is the new version of the venerable Windows, Web, Silverlight, Office, Windows Phone 7 and Microsoft Azure development tool that many developers will greet warmly. Many new features are packed into this feature, and there has also been room for improvement in the most commonly used features of the product, namely the code editor.
In this article, you learned the most important new features in Visual Studio 2010's code editor, including the improved IntelliSense features, better code navigation, and highlighting references to name a few. Of course, there's much more to the 2010 than the new additions to the code editor. But since most developers spend their time in the code editor, even a little additional help can save hours in the long run.