@@ -58,6 +58,36 @@ pub mod util {
5858 use super::*;
5959 use helix_core::{Range, Rope, Transaction};
6060
61+ /// Converts a diagnostic in the document to [`lsp::Diagnostic`].
62+ ///
63+ /// Panics when [`pos_to_lsp_pos`] would for an invalid range on the diagnostic.
64+ pub fn diagnostic_to_lsp_diagnostic(
65+ doc: &Rope,
66+ diag: &helix_core::diagnostic::Diagnostic,
67+ offset_encoding: OffsetEncoding,
68+ ) -> lsp::Diagnostic {
69+ use helix_core::diagnostic::Severity::*;
70+
71+ let range = Range::new(diag.range.start, diag.range.end);
72+ let severity = diag.severity.map(|s| match s {
73+ Hint => lsp::DiagnosticSeverity::HINT,
74+ Info => lsp::DiagnosticSeverity::INFORMATION,
75+ Warning => lsp::DiagnosticSeverity::WARNING,
76+ Error => lsp::DiagnosticSeverity::ERROR,
77+ });
78+
79+ // TODO: add support for Diagnostic.data
80+ lsp::Diagnostic::new(
81+ range_to_lsp_range(doc, range, offset_encoding),
82+ severity,
83+ None,
84+ None,
85+ diag.message.to_owned(),
86+ None,
87+ None,
88+ )
89+ }
90+
6191 /// Converts [`lsp::Position`] to a position in the document.
6292 ///
6393 /// Returns `None` if position exceeds document length or an operation overflows.