views:

25

answers:

1

How can I change color of lines in Visual Studio 2010 based on some custom pattern? For example, I want to change color of all lines that start with logger. . Is it possible at all?

I have ReSharper 5 installed too.

+1  A: 

I wrote up a quick little extension to do this; since you'll very likely want to modify it, you should grab the source. The important part is the code in LayoutChanged:

    void ViewLayoutChanged(object sender, TextViewLayoutChangedEventArgs e)
    {
        IWpfTextView view = sender as IWpfTextView;
        var adornmentLayer = view.GetAdornmentLayer("HighlightLines");

        foreach (var line in e.NewOrReformattedLines)
        {
            if (line.Extent.GetText().StartsWith("logger.", StringComparison.OrdinalIgnoreCase))
            {
                Rectangle rect = new Rectangle()
                    {
                        Width = view.ViewportWidth + view.MaxTextRightCoordinate,
                        Height = line.Height,
                        Fill = Brushes.AliceBlue
                    };

                Canvas.SetTop(rect, line.Top);
                Canvas.SetLeft(rect, 0);
                adornmentLayer.AddAdornment(line.Extent, null, rect);
            }
        }
    }

To build/run this, you'll need to:

  1. Download the VS2010 SDK.
  2. Create a new project from the editor extension templates (I usually pick Visual C# -> Extensibility -> Editor Text Adornment).
  3. Delete all the source files it creates.
  4. Add HighlightMatchingLines.cs to the project.
  5. F5 to run/test.
  6. If you want to change the brush, change the Fill = Brushes.AliceBlue line.
  7. If you want to change what text is matched, changed the condition in the if expression.
  8. If you want to change what file type the extension is loaded for, change the [ContentType] attribute. The "Content Types" section of this msdn page lists out some of the common ones.
Noah Richards
Thought about writing an extension, but didn't want to get into it. Nevertheless you are awesome!
HeavyWave