Extending the Code Editor

As explained at the beginning of this chapter, one of the most important new features in the Visual Studio 2010 is the capability of extending the code editor, which is now based on WPF. Code editor extensions get the instance of the WPF objects keeping the editor itself alive. For a better understanding, instead of building a particular extension, we explain required objects taking advantage of one of the sample projects added by the Visual Studio 2010 SDK. Create a new project and select the Visual Basic, Extensibility folder; finally select the Editor Text Adornment project template, as shown in Figure 57.13.

Figure 57.13 Selecting the code editor extension template.

image

The goal of this sample project is simple: adorning each “a” character in the code with a different background color. The most important object in providing editor extensions is the Microsoft.VisualStudio.Text.Editor.IWpfTextView type that represents the instance of the code editor. For the current example, there is the need of placing an adornment on all occurrences of the specified character. To place adornments, you need an instance of the IAdornmentLayer type that represents a space for placing adornments. Listing 57.4 shows the complete code; read comments that can help you understand what is under the hood.

Listing 57.4 Providing a Code Editor Extension with Adornments

image

image

image

image

Notice how the Microsoft.VisualStudio.Text namespace exposes objects and other namespaces for interacting with the code editor. Now run the extension by pressing F5. Try to create a new console project and write some text containing “a” characters and you will see how they are surrounded with a different background, as shown in Figure 57.14.

Figure 57.14 The WPF editor extension adorning some text.

image

There are so many scenarios in which you might need to extend the Visual Studio code editor. You can find lots of interesting extensions by searching the Visual Studio Gallery with the Extension Manager.

Summary

Visual Studio 2010 is an extensible development environment that can be enhanced with custom extensions such as add-ins, packages, and new code editor extensions due to a new architecture based on Windows Presentation Foundation. Instrumentation required for creating extensibility projects are available when installing the Visual Studio 2010 SDK that provides projects templates, tools, and documentation. This chapter explained how to build a custom tool window based on WPF for the Visual Studio development environment. Then you saw how custom extensions can be packaged into VSIX files and deployed to other developers. Next you saw how you can take advantage of the new Extension Manager for getting and easily installing extensions from the Visual Studio Gallery. The last example provided in this chapter was about extending the WPF-based code editor by taking advantage of Visual Studio’s managed assemblies.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.34.198