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

时间:2016-06-09 08:38:03

标签: c# visual-studio-2015 visual-studio-sdk adornment

上周,我决定创建一个用于测量C ++代码覆盖率的Visual Studio扩展。基本上我自己需要它来做我的日常工作。我想出的是可以找到的项目https://github.com/atlaste/CPPCoverage

大部分工作正常。但是,我对装饰层有一些问题:

该项目的一个功能是创建(未)覆盖代码的突出显示。突出显示本身工作正常,但它似乎干扰Visual Studio的选择代码:

Demonstration of issue

负责突出显示的相关代码:

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

问:有人可以告诉我如何在背景上渲染块而不是前景吗?

1 个答案:

答案 0 :(得分:1)

7年前被问到并且仍然有关于装饰的零信息和网上扩展Visual Studio的信息。

以下是您的操作方法:

  1. 使用VS向导创建TextAdornment类实例时(例如:右键单击project =&gt; Add Item),您会收到两个文件:第一个是带有您选择名称的类,另一个是一个是

    <强> [NameYouPicked] TextViewCreationListener.cs

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

  3. 转到您的装饰定义。它应该是这样的:
  4. /// <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. 如您所见,订单设定为选择后。我不知道哪个图层是最早的图层,但我这样更改代码以防止不必要的碰撞:
    2.   [Order(Before = PredefinedAdornmentLayers.BraceCompletion)]