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:
- Download the VS2010 SDK.
- Create a new project from the editor extension templates (I usually pick Visual C# -> Extensibility -> Editor Text Adornment).
- Delete all the source files it creates.
- Add HighlightMatchingLines.cs to the project.
- F5 to run/test.
- If you want to change the brush, change the
Fill = Brushes.AliceBlue
line. - If you want to change what text is matched, changed the condition in the
if
expression. - 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.
精彩评论