Skip to main content

diagprint_lsp/
lib.rs

1//! Language Server Protocol integration for diagprint.
2//!
3//! This crate translates revision-aware diagprint diagnostics into LSP
4//! diagnostics and converts guarded remediation into versioned workspace-edit
5//! code actions.
6//!
7//! Source revisions and external LSP document versions intentionally remain
8//! separate.
9
10mod action;
11mod batch;
12mod diagnostic;
13mod document;
14mod error;
15mod position;
16
17use diagprint::{CapturedDiagnostic, Diagnostic, DiagnosticReport, SourceSnapshot};
18use lsp_types::{CodeAction, Diagnostic as LspDiagnostic, PublishDiagnosticsParams};
19
20pub use document::{DocumentMap, LspDocument};
21
22pub use error::LspError;
23pub use position::PositionEncoding;
24
25pub use lsp_types;
26
27#[derive(Debug, Clone)]
28pub struct LspAdapter {
29    documents: DocumentMap,
30    encoding: PositionEncoding,
31}
32
33impl LspAdapter {
34    pub fn new(documents: DocumentMap, encoding: PositionEncoding) -> Self {
35        Self {
36            documents,
37            encoding,
38        }
39    }
40
41    pub fn utf16(documents: DocumentMap) -> Self {
42        Self::new(documents, PositionEncoding::Utf16)
43    }
44
45    pub fn documents(&self) -> &DocumentMap {
46        &self.documents
47    }
48
49    pub fn documents_mut(&mut self) -> &mut DocumentMap {
50        &mut self.documents
51    }
52
53    pub const fn encoding(&self) -> PositionEncoding {
54        self.encoding
55    }
56
57    pub fn diagnostic(
58        &self,
59        diagnostic: &Diagnostic,
60        sources: &SourceSnapshot,
61    ) -> Result<LspDiagnostic, LspError> {
62        diagnostic::to_lsp_diagnostic(diagnostic, sources, &self.documents, self.encoding)
63    }
64
65    pub fn captured_diagnostic(
66        &self,
67        captured: &CapturedDiagnostic,
68    ) -> Result<LspDiagnostic, LspError> {
69        self.diagnostic(captured.diagnostic(), captured.sources())
70    }
71
72    pub fn publish_captured(
73        &self,
74        captured: &CapturedDiagnostic,
75    ) -> Result<PublishDiagnosticsParams, LspError> {
76        diagnostic::publish_diagnostic(
77            captured.diagnostic(),
78            captured.sources(),
79            &self.documents,
80            self.encoding,
81        )
82    }
83
84    /// Converts a report into one publishDiagnostics payload per primary
85    /// document.
86    ///
87    /// Diagnostics targeting the same primary source are grouped together.
88    /// Output order is deterministic by diagprint source name.
89    pub fn publish_report(
90        &self,
91        report: &DiagnosticReport,
92        sources: &SourceSnapshot,
93    ) -> Result<Vec<PublishDiagnosticsParams>, LspError> {
94        batch::publish_report(report, sources, &self.documents, self.encoding)
95    }
96
97    /// Builds CodeActions for every diagnostic in a report against one
98    /// immutable source snapshot.
99    pub fn code_actions_report(
100        &self,
101        report: &DiagnosticReport,
102        sources: &SourceSnapshot,
103    ) -> Result<Vec<CodeAction>, LspError> {
104        batch::code_actions_report(report, sources, &self.documents, self.encoding)
105    }
106
107    pub fn code_actions(&self, captured: &CapturedDiagnostic) -> Result<Vec<CodeAction>, LspError> {
108        action::code_actions(
109            captured.diagnostic(),
110            captured.sources(),
111            &self.documents,
112            self.encoding,
113        )
114    }
115}