Visual Studio 文本前面的修饰会干扰选择

Visual Studio adornment in front of text interferes with selection

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

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

7 年前问过,网上关于装饰和延伸的信息几乎为零Visual Studio。

这是您的操作方法:

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

    [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)]