Syntax Coloring

Visual Studio uses a coloring service to identify elements of the language and display them with the specified colors in an editor.

Colorizer Model

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

SVC Colorizer graphic

Note

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

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

  1. The VSPackage must get the appropriate language service, which requires the language service VSPackage to do the following:

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

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

    3. Associate the language service with the IVsTextBuffer by calling its SetLanguageServiceID method.

  2. The VSPackage can now obtain and use the colorizer object as follows:

    Note

    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.

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

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

    3. Use the colorization information returned by the ColorizeLine method to display the selected text.

Note

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

In This Section

See Also

Other Resources

Fonts