开发者

VS 2010: Change color of lines based on a pattern

How can I change color of lines in Visual Studio 2010 based on some custom pattern? For example, I want to change color of a开发者_如何学JAVAll lines that start with logger. . Is it possible at all?

I have ReSharper 5 installed too.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜