Current Line Highlighting for VS2010

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:

vs10_clh_1

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:

  1. 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 AdornmentLayerDefinition controls this.
  2. 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 IClassificationFormatMapService.

This service gives you an IClassificationFormatMap that provides two key things:

  • Get the TextFormattingRunProperties that 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.

Comments (10)

Damien GuardJune 17th, 2009 at 4:31 am

But why is Envy Code R appearing in fake-italic (oblique)…

[)amien

Tomas RestrepoJune 17th, 2009 at 12:26 pm

Because until the editor options actually support enabling italics, I’m still using Envy Code R VS :) Too lazy to figure out a way to work around it for now.

ReneJuly 23rd, 2009 at 3:44 pm

Hi Tomas

what’s the Name of this Theme.

very very nice.
Rene

Tomas RestrepoJuly 23rd, 2009 at 7:18 pm

@Rene: It’s not yet a full theme, really. It’s just something I’ve been using lately which is a modified version of my Distant Shores theme, but it’s very rough. Might try to finish it up one of these days and post it :)

ReneJuly 24th, 2009 at 9:00 am

Great News!

Distant Shores is my favorite theme at the moment.

thank you

Patrick SzalapskiJuly 31st, 2009 at 1:22 pm

Could this be adapted for VS2008?

Tomas RestrepoJuly 31st, 2009 at 5:15 pm

@Patrick: Not really… this uses the completely new extensibility model and editor features in VS2010. I know there are other plugins for VS2008 that support this feature, though (like resharper).

ZuhaibAugust 6th, 2009 at 7:16 am

Gr8 Job .. Please finish the theme soon .. would love to have this theme on VS 2008 & VS 2010

Thanks

YangOctober 20th, 2009 at 1:26 am

Cool,i love this theme very much!
BTW:What is this font?

Tomas RestrepoOctober 20th, 2009 at 6:15 pm

The font is Damien Guard’s Envy Code R: http://damieng.com/blog/2008/05/26/envy-code-r-preview-7-coding-font-released

Leave a comment

Your comment