【问题标题】:Roslyn Diagnostic + Visual Studio ColorisationRoslyn 诊断 + Visual Studio 着色
【发布时间】:2014-12-01 22:44:49
【问题描述】:

有谁知道我可以在哪里找到基于 Roslyn 诊断的令牌着色的“简单”示例。是的,我可以将它们设为信息、警告、错误或隐藏。因此,假设要使用隐藏,因此它不会出现在错误列表/窗口中,但可以访问,以便我稍后可以使用它。

现在我有了这些隐藏的诊断信息,现在我想影响 IDE 中文本的着色。

这就是我尝试过的。

Private Sub CreateVisuals(ByVal line As ITextViewLine)
  Try
    'grab a reference to the lines in the current TextView 
    Dim textViewLines = _view?.TextViewLines
    If textViewLines Is Nothing Then Exit Sub 
    If line Is Nothing Then Exit Sub 
    Dim lineStart As Integer = line.Start
    Dim lineEnd As Integer = line.End
    Dim q = textViewLines.FirstOrDefault
    If q Is Nothing Then Exit Sub
    Dim qq = q.Snapshot.GetOpenDocumentInCurrentContextWithChanges
    If qq Is Nothing Then Exit Sub
    Dim sm = qq.GetSemanticModelAsync.Result '..GetSemanticModelAsync.Result
    '   Dim di = sm.GetSyntaxDiagnostics.ToArray
    If sm Is Nothing Then Exit Sub
    Dim diags = sm.GetDiagnostics.ToArray

我试过GetSyntaxDiagnostic

    If diags.Any() = False Then Exit Sub
    For Each d In diags
      ' This is the ID if the Diagnostic I want to color.
      'If d.Id<>"SFD000" Then Continue For
      Dim charSpan As New SnapshotSpan(_view.TextSnapshot,
                      Span.FromBounds(d.Location.SourceSpan.Start, d.Location.SourceSpan.End))
      Dim g As Geometry = textViewLines.GetMarkerGeometry(charSpan)
      If g IsNot Nothing Then
        Dim drawing As New GeometryDrawing(_brush, _pen, g) : drawing.Freeze()
        Dim drawingImage As New DrawingImage(drawing) : drawingImage.Freeze()
        Dim image As 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,
                             charSpan, Nothing, image, Nothing)
      End If
    Next
  Catch ex As Exception
    Debug.Print(ex.ToString)
  End Try 
End Sub

我得到了编译器的诊断问题,但不是我的。为什么?

示例可以是 C# 或 VB.net。

【问题讨论】:

    标签: c# vb.net roslyn visual-studio-sdk


    【解决方案1】:

    这只能通过IDiagnosticService 完成(这是 Roslyn 对错误标签和不必要代码进行分类的方式)。

    那个接口是内部的,所以你运气不好(除非你想使用很多反射)。

    您可以在 CodePlex 上提出问题并要求他们将 IDiagnosticService 公开。

    您也可以要求他们公开AbstractDiagnosticsTagProducer&lt;TTag&gt;;它将完全满足您的需求,让您插入过滤器和标签创建器。
    有关详细信息,请在反编译器中查看该类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-29
      • 2017-08-04
      • 2023-01-11
      • 2022-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多