如何使用 OmniSharp LanguageServer 推送 LSP 诊断?

问题描述

我正在使用 OmniSharp 的 C# LSP server 为 VS Code 插件实现简单的解析/语言服务。我已经掌握了基础知识并开始运行,但我一直无法弄清楚如何将诊断消息推送到 VS Code(例如在 this 打字稿示例中)。

有人有任何有用的示例代码/提示吗?

谢谢!

解决方法

与@david-driscoll 交谈后,我发现我需要在构造函数中存储对 ILanguageServerFacade 的引用,并在 TextDocument 上使用 PublishDiagnostics 扩展方法。即:

public class TextDocumentSyncHandler : ITextDocumentSyncHandler
{
   private readonly ILanguageServerFacade _facade;

   public TextDocumentSyncHandler(ILanguageServerFacade facade)
   {
      _facade = facade;
   }

   public Task<Unit> Handle(DidChangeTextDocumentParams request,CancellationToken cancellationToken) 
   {
      // Parse your stuff here

      // Diagnostics are sent a document at a time,this example is for demonstration purposes only
      var diagnostics = ImmutableArray<Diagnostic>.Empty.ToBuilder();

      diagnostics.Add(new Diagnostic()
      {
         Code = "ErrorCode_001",Severity = DiagnosticSeverity.Error,Message = "Something bad happened",Range = new Range(0,0),Source = "XXX",Tags = new Container<DiagnosticTag>(new DiagnosticTag[] { DiagnosticTag.Unnecessary })
      });

      _facade.TextDocument.PublishDiagnostics(new PublishDiagnosticsParams() 
      {
         Diagnostics = new Container<Diagnostic>(diagnostics.ToArray()),Uri = request.TextDocument.Uri,Version = request.TextDocument.Version
      });

      return Unit.Task;
   }
}

对于真正的代码,您需要一个集中的 Diagnostic 对象数组,但这显示了如何完成它的基础知识。

谢谢大卫!