2

上周,我决定创建一个 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 。

问:谁能告诉我如何在背景而不是前景渲染块?

4

1 回答 1

2

7 年前问过,网上仍然有关于装饰和扩展 Visual Studio 的零信息。

这是您的操作方法:

  1. 当您使用 VS 向导创建 TextAdornment 类实例时(例如:右键单击项目 => 添加项目),您会收到两个文件:第一个是您选择的名称的类,另一个是

    [NameYouPicked]TextViewCreationListener.cs

  2. 在解决方案资源管理器中找到该文件并打开它。

  3. 转到您的装饰定义。它应该如下所示:
/// <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;
  1. 如您所见,订单集是 AFTER SELECTION。我不知道哪一层是最早的,但我以这种方式更改代码以防止不必要的碰撞:
  [Order(Before = PredefinedAdornmentLayers.BraceCompletion)]
于 2016-08-29T15:55:37.863 回答