上周,我决定创建一个用于测量C ++代码覆盖率的Visual Studio扩展。基本上我自己需要它来做我的日常工作。我想出的是可以找到的项目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。
问:有人可以告诉我如何在背景上渲染块而不是前景吗?
答案 0 :(得分:1)
7年前被问到并且仍然有关于装饰的零信息和网上扩展Visual Studio的信息。
以下是您的操作方法:
使用VS向导创建TextAdornment类实例时(例如:右键单击project =&gt; Add Item),您会收到两个文件:第一个是带有您选择名称的类,另一个是一个是
<强> [NameYouPicked] TextViewCreationListener.cs 强>
在解决方案资源管理器中找到该文件并将其打开。
/// <summary> /// Defines the adornment layer for the adornment. This layer is ordered /// after the selection layer in the Z-order /// </summary> [Export(typeof(AdornmentLayerDefinition))] [Name("TextAdornment")] [Order(After = PredefinedAdornmentLayers.Selection, Before = PredefinedAdornmentLayers.Text)] private AdornmentLayerDefinition editorAdornmentLayer;
[Order(Before = PredefinedAdornmentLayers.BraceCompletion)]