Custom Colorable Items
With a VSPackage, you can override the list of items available for colorizing, such as keywords and comments, by implementing custom colorable items as part of a language service.
User Settings of Colorable Items
Your user has access to all the colorable items in the integrated development environment (IDE). You can display the Fonts and Colors dialog box by selecting Options on the Tools menu, and then selecting Fonts and Colors under Environment. When you select a display, such as Text Editor or Command Window, the Display items list box shows all the colorable items for that display. You can view and change the font, size, foreground color, and background color for each colorable item. Your choices are stored in a cache in the registry and accessed by the colorable item name.
Presentation of Colorable Items
Because the IDE handles user overrides of colorable items in the Fonts and Colors dialog box, you need only to supply each custom colorable item with a name. This name is what appears in the Display items list. The colorable items appear in alphabetical order. This organization gives you a convenient way to group your language service's custom colorable items. You can begin each name with your language name, for example NewLanguage - Comment and NewLanguage - Keyword.
警告
We strongly recommend that you include the language name in the colorable item name to avoid collisions with existing colorable item names.If you change the name of one of your colorable items during development, you must reset the cache that was created the first time your colorable items were accessed. You can reset the cache with the vsregex.exe tool. Use the command line vsregex.exe GetOrig 8.0 Exp, substituting the appropriate version and registry name extension for your version of Visual Studio. The vsregex.exe tool is installed with the Visual Studio SDK.
The first item in your list of colorable items is never referenced. The first item corresponds to a colorable item index of 0, and Visual Studio always supplies the default text colors and attributes for that item. The easiest way of dealing with this unreferenced item is to supply a placeholder colorable item in your list as the first item.
Implementing Custom Colorable Items
Define what items in your language must be colorized, for example Keyword, Operator, and Identifier.
Create an enumeration of these colorable items.
Associate the token types returned from a parser or scanner with the enumerated values.
For example, the values representing the token types could be the same values in the custom colorable items enumeration.
In your implementation of the ColorizeLine method in your IVsColorizer object, fill the attributes list with the values from your custom colorable items enumeration corresponding to the token types returned from the parser or scanner.
In the same class that implements the IVsLanguageInfo interface, implement the IVsProvideColorableItems interface and its two methods, GetItemCount and GetColorableItem.
Implement the IVsColorableItem interface.
If you want to support 24-bit or high color values, also implement the IVsHiColorItem interface.
In your language service object, create a list that contains your IVsColorableItem objects, one for each colorable item your parser or scanner can identify.
You can access each item in the list by using the corresponding value from the custom colorable items enumeration. Use the enumeration values as an index into the list. Realize that the first item in the list is never accessed, because it corresponds to the default text style that Visual Studio always handles itself. You can compensate for this by inserting a placeholder colorable item at the beginning of your list.
In your implementation of the GetItemCount method, return the number of items in your custom colorable items list.
In your implementation of the GetColorableItem method, return the requested colorable item from your list.
For an example of how to implement the IVsColorableItem and IVsHiColorItem interfaces, see IVsHiColorItem.