Implementing Syntax Coloring
Implementing a colorizer requires a parser for your language service. The parser converts a line of text into an array of colorable items and returns token types corresponding to these colorable items. The parser should return token types that belong to a list of default colorable items and any custom colorable items that you create. Visual Studio displays each colorable item in the code window according to the color attributes assigned by the colorizer object to the appropriate token type.
Visual Studio does not specify a parser interface, and parser implementation is completely up to you. However, an example parser implementation is provided in the Figures Language Service sample. For managed code, the managed package framework (MPF) provides complete support for colorizing text. You must supply only a parser or scanner.
Steps Followed by an Editor to Colorize Text
The editor obtains the colorizer by calling the GetColorizer method on the IVsLanguageInfo object.
The editor calls the GetStateMaintenanceFlag method to determine whether the colorizer needs the state of each line maintained outside the colorizer.
If the colorizer requires the state to be maintained outside the colorizer, the editor calls the GetStartState method to get the state of the first line.
For each line in the buffer, the editor calls the ColorizeLine method.
ColorizeLine performs the following steps:
The line of text is passed to a parser or scanner to convert the text into tokens. Each token specifies the extent or span of the token's text, and the token's type.
The token's type is converted into an index into a colorable items list, either the default list supplied by Visual Studio or the custom colorable items list supplied by the language service itself.
ColorizeLine uses the token's information to fill in an array supplied in the call. Each element of the array corresponds to a character in the line. The values stored in the array are the indexes into the colorable items list.
ColorizeLine returns the state at the end of the line for each line.
If the colorizer requires the state to be maintained, the editor caches the state for that line.
The editor renders the line of text using the information returned from the ColorizeLine method. This requires the following steps:
For each character in the line, get the colorable item index.
If using the default colorable items, access the editor's colorable items list.
Otherwise, call the language service's GetColorableItem method to obtain a colorable item.
Use the information in the colorable item to render the text into the display.
Figures Language Service Sample
The Figures language parser (implemented on the CFigParse object, which is part of the Figures Language Service sample) returns type information for each item, one line of text at a time. The IFigColorizer::ColorizeLine method calls the parser to get information for the modified line of text. If the parser does not report any errors, the colorizer cycles through the items in the line of text, modifying each character's attributes by putting appropriate values into an array of color attributes passed to the ColorizeLine method. The integrated development environment (IDE) then uses this information to display the line with appropriate color information.
Managed Package Framework Colorizer
The managed package framework (MPF) provides all the classes that are required to implement a colorizer. All that you need to do is derive a class from the LanguageService class and implement the required methods. Then you must supply a parser or scanner by implementing the IScanner interface, and return an instance of that interface from the GetScanner method (one of the methods that must be implemented in the LanguageService class). For more information, see Syntax Highlighting (Managed Package Framework).