2016-06-09 183 views
0

上週,我決定創建一個用於測量C++代碼覆蓋率的Visual Studio擴展。基本上我自己需要我的日常工作。我想到的是可以找到的項目https://github.com/atlaste/CPPCoverage文本前面的Visual Studio裝飾會干擾選擇

大部分工作正常。然而,我有一些裝飾層的問題:

該項目的特點之一是創建(未)覆蓋代碼的突出顯示。突出自身工作正常,但似乎與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

問:有人可以告訴我如何在背景而不是前景渲染塊?

+0

PS:作爲一個變通我注意到你可以設置不透明度圖層。雖然這是一個很好的解決辦法,但我寧願將該圖層置於後臺。 – atlaste

回答

1

7年前問,仍然有關於裝飾和擴展Visual Studio在網絡上的ZERO信息。

這裏是你如何做到這一點:

  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; 
  • 正如你看到的,命令集合是選擇後。我不知道哪一層是最早的一個,但我改變了這樣的代碼,以防止不必要的衝突:
  • [Order(Before = PredefinedAdornmentLayers.BraceCompletion)] 
    
    +0

    很好找。這是7年來的最佳答案......:-S你是對的,遺憾的是文件太少;該框架非常好,但缺乏文檔使其很難使用。 – atlaste

    +0

    @atlaste你可能想把它標記爲答案:) – cubrman

    +1

    好吧,一旦我確認這是正確的答案,我當然會。儘管如此,考慮到花了7年的時間,我不明白爲什麼我現在應該匆忙。 :) – atlaste