1mod 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 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 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}