上周,我决定创建一个 Visual Studio 扩展来测量 C++ 代码覆盖率。基本上我自己的日常工作都需要它。我想出的是可以在https://github.com/atlaste/CPPCoverage找到的项目。
大多数它工作正常。但是,我对装饰层有一些问题:
该项目的功能之一是创建(未)覆盖的代码的突出显示。突出显示本身工作正常,但它似乎干扰了 Visual Studio 的选择代码:
负责突出显示的相关代码:
private void HighlightCoverage(CoverageState[] coverdata, ITextViewLine line)
{
IWpfTextViewLineCollection textViewLines = view.TextViewLines;
int lineno = 1 + view.TextSnapshot.GetLineNumberFromPosition(line.Extent.Start);
CoverageState covered = lineno < coverdata.Length ?
coverdata[lineno] : CoverageState.Irrelevant;
if (covered != CoverageState.Irrelevant)
{
SnapshotSpan span = new SnapshotSpan(view.TextSnapshot,
Span.FromBounds(line.Start, line.End));
Geometry g = textViewLines.GetMarkerGeometry(span);
if (g != null)
{
GeometryDrawing drawing = (covered == CoverageState.Covered) ?
new GeometryDrawing(coveredBrush, coveredPen, g) :
new GeometryDrawing(uncoveredBrush, uncoveredPen, g);
drawing.Freeze();
DrawingImage drawingImage = new DrawingImage(drawing);
drawingImage.Freeze();
Image image = new Image();
image.Source = drawingImage;
//Align the image with the top of the bounds of the text geometry
Canvas.SetLeft(image, g.Bounds.Left);
Canvas.SetTop(image, g.Bounds.Top);
layer.AddAdornment(AdornmentPositioningBehavior.TextRelative,
span, null, image, null);
}
}
}
可以在此处找到具有正确上下文的完整代码:https ://github.com/atlaste/CPPCoverage/blob/master/CoverageExt/CodeRendering/CodeCoverage.cs 。
问:谁能告诉我如何在背景而不是前景渲染块?