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

How to push LSP Diagnostic using OmniSharp LanguageServer?

我正在使用 OmniSharp 的 C# LSP server to implement a simple parsing/language service for a VS Code plugin. I've managed to get the basics up and running, but I've not been able to figure out how to push diagnostic messages to VS Code (like in this 打字稿样本)。

有没有人有任何有用的样本code/hints?

谢谢!

与@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, 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;
   }
}

对于实际代码,您可能需要诊断对象的集中数组,但这显示了如何完成它的基础知识。

谢谢大卫!