简体   繁体   English

Roslyn诊断+ Visual Studio着色

[英]Roslyn Diagnostic + Visual Studio Colorisation

Does any know where I can location an "simple" example of colourisation of a token based on a Roslyn diagnostic. 有谁知道在哪里可以找到基于Roslyn诊断程序的标记着色的“简单”示例。 Yep I can make them Info, Warning, Error or Hidden. 是的,我可以将其设置为“信息”,“警告”,“错误”或“隐藏”。 So let say want to use Hidden so it doesn't appear in the Errors list/window, but is accessible so I could do something with it later. 因此,可以说要使用“隐藏”,这样它就不会出现在“错误”列表/窗口中,但是可以访问,因此以后可以对其进行处理。

Now I've got these hidden diagnostic, now I would like to affect the colourisation of the text in the IDE. 现在,我有了这些隐藏的诊断程序,现在,我想影响IDE中文本的颜色。

This is what I've tried so for. 这就是我尝试过的。

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

I have tried GetSyntaxDiagnostic 我已经尝试过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

I get the diagnostic issue by the compiler, but not mine. 我得到了编译器的诊断问题,但不是我的。 Why? 为什么?

The Example can be either C# or VB.net. 该示例可以是C#或VB.net。

This can only be done with IDiagnosticService (which is how Roslyn classifies error tags & unnecessary code). 这只能通过IDiagnosticService来完成(这是Roslyn对错误标记和不必要的代码进行分类的方式)。

That interface is internal, so you're out of luck (unless you want to use a lot of Reflection). 该接口是内部接口,因此您很不走运(除非您想使用大量的Reflection)。

You can file an issue on CodePlex & ask them to make IDiagnosticService public. 您可以在CodePlex上提出问题,并要求他们将IDiagnosticService公开。

You can also ask them to make AbstractDiagnosticsTagProducer<TTag> public; 您还可以要求他们公开AbstractDiagnosticsTagProducer<TTag> it will do exactly what you're looking for, letting you plug in a filter and a tag creator. 它会完全满足您的需求,让您插入过滤器和标签创建者。
For more information, look at that class in a decompiler. 有关更多信息,请在反编译器中查看该类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM