Syntax Coloring in Custom Editors

Visual Studio Environment SDK editors, including the core editor, use language services to identify specific syntactical items and display them with specified colors for a given document view.

Colorization Requirements

All editors implementing a language service's colorizer must:

  1. Use an object implementing IVsTextBuffer to manage the text to be colorized and an object implementing IVsTextView to provide a document view of the text.

  2. Obtain an interface to a particular language service by querying the VSPackage's service provider using the languages service's identifying GUID.

  3. Call the SetLanguageServiceID method of the object implementing IVsTextBuffer. This method associates the language service with the IVsTextBuffer implementation that the VSPackage uses to manage the text that is to be colorized.

Core Editor Usage of a Language Service's Colorizer

When a language service with a colorizer is obtained by an instance of the core editor, the parsing and rendering of text by a language service's colorizer occurs automatically without requiring any further intervention on your part.

The IDE transparently:

  • Calls the colorizer as needed to parse and analyze text as it is added or modified in the implementation of IVsTextBuffer.

  • Ensures that the display supplied by the document view provided by the IVsTextView implementation is updated and repainted using the information returned by the colorizer.

Non-core Editor Usage of a Language Service's Colorizer

Non-core editor instances can also use a language service's syntax colorization service, but they must explicitly retrieve and apply the service's colorizer, and repaint their document views themselves.

To do this, a non-core editor must:

  1. Obtain a language service's colorizer object (which implements IVsColorizer and IVsColorizer2). The VSPackage does this by calling the GetColorizer method on the language service's interface.

  2. Call the ColorizeLine method to request that a particular span of text be colorized.

    The ColorizeLine method returns an array of values, one for each letter in the text span being colorized. It also identifies the text span as a particular type of colorable item, such as a comment, keyword, or data type.

  3. Use the colorization information returned by ColorizeLine to repaint and display its text.

Note

In addition to using a language service's colorizer, a VSPackage can choose to use the general-purpose Visual Studio Environment SDK text-coloring mechanism. For more information on this mechanism, see Using Fonts and Colors.