c# - 如何使用 OmniSharp LanguageServer 推送 LSP 诊断?
问题描述
我正在使用 OmniSharp 的C# LSP 服务器为 VS Code 插件实现一个简单的解析/语言服务。我已经设法让基础知识启动并运行,但我无法弄清楚如何将诊断消息推送到 VS Code(就像在这个打字稿示例中一样)。
有没有人有任何有用的示例代码/提示?
谢谢!
解决方案
与@david-driscoll 交谈后,我发现我需要在构造函数中存储对ILanguageServerFacade 的引用,并在TextDocument 上使用PublishDiagnostics 扩展方法。IE:
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;
}
}
对于实际代码,您可能需要一个集中的诊断对象数组,但这显示了如何完成它的基础知识。
谢谢大卫!
推荐阅读
- c++ - 更改类型后失去对 std::variant 对象的引用
- python - 如何在 codeskulptor 中每 x 秒生成一个形状?
- java - 具有构建器模式的通用对象
- python - Scrapy:提取字典存储为脚本标签中的文本
- r - R:在 2 个不同的行和列上查找重复和减法
- flutter - Flutter 文本输入块表情符号
- kotlin - Mongo 和 Kotlin 的委托问题
- android - 恢复后的闪屏问题(Flutter - Android)
- google-cloud-storage - 有没有办法将谷歌存储签名的 url 更改为不包含文件名?
- arrays - How to access Array Item With Variable perl?