Custom colorable items

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

You can override the list of types for colorizing, such as keywords and comments, by implementing custom colorable items as part of your language service.

User settings of colorable items

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

Caution

You should 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 experimental cache with the CreateExpInstance tool, which is installed with the Visual Studio SDK, typically in the following directory under the Visual Studio installation folder:

VSSDK\VisualStudioIntegration\Tools\Bin

To reset the cache, enter CreateExpInstance /Reset. For more information about CreateExpInstance, see CreateExpInstance utility.

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.

Implement custom colorable items

  1. Define what must be colorized in your language, for example Keyword, Operator, and Identifier.

  2. Create an enumeration of these colorable items.

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

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

  5. In the same class that implements the IVsLanguageInfo interface, implement the IVsProvideColorableItems interface and its two methods, GetItemCount and GetColorableItem.

  6. Implement the IVsColorableItem interface.

  7. If you want to support 24-bit or high color values, also implement the IVsHiColorItem interface.

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

  9. In your implementation of the GetItemCount method, return the number of items in your custom colorable items list.

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

See also