I’ve created another simple extension for Visual Studio 2010 beta 1 that will highlight the current line the caret is in with a different background color. Here’s a screenshot of the extension in action:
The extension itself is pretty simple, but it was a good way to understand how adornments for the text editor worked. I think it’s a fairly elegant mechanism that offers quite a bit of potential. A couple of things that I liked quite a bit about it:
- You can easily configure the order in which your adornment layer is rendered in, compared to the rest of the stuff in the editor. For example, you might want to render it last of all, or perhaps after the text, but before the selection mask. Like other editor extensions, the [Order] attribute on your
- You can ask VS to position your adornment relative to the span of text it is associated with, or to the view layout, and VS will do most of the hard work of keeping it in the right place as the buffer view is scrolled and moved. It makes some things a lot easier.
The other thing I found interesting about creating the sample was how you could leverage the Classifications system to integrate with the Fonts and Colors configuration options for an extension that is not a custom classifier extension.
If you look at the extension code, you’ll notice that I have a custom
ClassificationFormatDefinition named “Current Line”, which allows the user to easily change the colors used to render the current line adornment. Then, in the code, I can get the settings the user configured (or the default ones provided by the classification format) by using the
This service gives you an
IClassificationFormatMap that provides two key things:
- Get the
TextFormattingRunPropertiesthat contains the brushes and other settings you need to use for drawing your adornment, and
- An event you can attach to in order to get notified when the user changes the fonts/color settings so that you can refresh your customization.
Pretty cool stuff. You can grab the code for the extension from the GitHub repository.