如何解决如何使用 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 对象数组,但这显示了如何完成它的基础知识。
谢谢大卫!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。