Providing a Syntax Coloring Service

The Visual Studio SDK uses language services to provide support for identifying specific syntactical items and then displaying those items with specified colors in an editor, in particular the core editor.

Colorizer Model

A 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

注意

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, a language service can supply custom colorable items that are used by the editor. A language service advertises that it supplies custom colorable items 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 first obtain access to the appropriate language service, which requires a VSPackage to do the following:

    1. Use an object implementing the IVsTextBuffer interface to access the text to be colorized.

      Text is typically displayed using an object that implements the IVsTextView interface.

    2. Obtain an association with a particular language service by querying the service provider of the VSPackage by using the languages service's GUID.

      Which language service to query for is determined by the extension of the file loaded into the editor. The extension is associated with a particular language service's GUID in the registry.

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

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

    注意

    VSPackages that use the core editor do not have to explicitly obtain a language service's colorizer objects. As soon as an instance of the core editor obtains an appropriate language service, it transparently 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.

注意

In addition to using a language service colorizer, a VSPackage can also use the general purpose Visual Studio SDK text coloring mechanism. For more information about this mechanism, see Fonts.

In This Section

See Also

Concepts

Figures Language Service

Fonts

Syntax Coloring in Editors