Syntax Coloring in a Legacy Language Service
This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here
Visual Studio uses a coloring service to identify elements of the language and display them with the specified colors in an editor.
The language service implements the IVsColorizer interface, which is then used by editors. This implementation is a separate object from the language service, as shown in the following illustration.
Simple colorizer model
The syntax coloring service is separate from the general Visual Studio mechanism for colorizing text. For more information about the general Visual Studio SDK mechanism supporting colorizing, see Using Fonts and Colors.
Besides the colorizer, the language service can supply custom colorable items that are used by the editor by advertising that it supplies custom colorable items. You can do this by implementing the IVsProvideColorableItems interface on the same object that implements the IVsLanguageInfo interface. It returns the number of custom colorable items when the editor calls the GetItemCount method, and it returns an individual custom colorable item when the editor calls the GetColorableItem method.
The GetColorableItem method returns an object that implements the IVsColorableItem interface. If the language service supports 24-bit or high color values, it must implement the IVsHiColorItem interface on the same object as the IVsColorableItem interface.
How a VSPackage Uses a Language Service Colorizer
The VSPackage must get the appropriate language service, which requires the language service VSPackage to do the following:
Use an object implementing the IVsTextBuffer interface to get the text to be colorized.
Text is typically displayed using an object that implements the IVsTextView interface.
Get the language service by querying the service provider of the VSPackage for the language service GUID. Language services are identified in the registry by file extension.
The VSPackage can now obtain and use the colorizer object as follows:
VSPackages that use the core editor do not have to obtain a language service's colorizer objects explicitly. As soon as an instance of the core editor obtains an appropriate language service, it performs all the colorization tasks shown here.
Obtain the language service's colorizer object, which implements the
T:Microsoft.VisualStudio.TextManager.Interop.IVsColorizer, and IVsColorizer2 interfaces, by calling the GetColorizer method on the language service's IVsLanguageInfo object.
Call the ColorizeLine method to obtain the colorizer information for a particular span of text.
ColorizeLine returns an array of values, one for each character in the text span being colorized. The values are indexes into a colorable item list that is either the default colorable item list maintained by the core editor or a custom colorable item list maintained by the language service itself.
Use the colorization information returned by the ColorizeLine method to display the selected text.
In addition to using a language service colorizer, a VSPackage can also use the general-purpose Visual Studio text coloring mechanism. For more information about this mechanism, see Using Fonts and Colors.
In This Section
Implementing Syntax Coloring
Discusses how an editor accesses a language service's syntax coloring and what the language service must implement to support syntax coloring.
How to: Use Built-In Colorable Items
Demonstrates how to use built-in colorable items from the language service.
Custom Colorable Items
Discusses how to implement custom colorable items.