1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
use std::collections::HashMap;

use serde::{Deserialize, Serialize};
use url::Url;

use crate::{
    Diagnostic, PartialResultParams, StaticRegistrationOptions, TextDocumentIdentifier,
    TextDocumentRegistrationOptions, WorkDoneProgressOptions, WorkDoneProgressParams,
};

/// Client capabilities specific to diagnostic pull requests.
///
/// @since 3.17.0
#[derive(Debug, Eq, PartialEq, Clone, Default, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DiagnosticClientCapabilities {
    /// Whether implementation supports dynamic registration.
    ///
    /// If this is set to `true` the client supports the new `(TextDocumentRegistrationOptions &
    /// StaticRegistrationOptions)` return value for the corresponding server capability as well.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dynamic_registration: Option<bool>,

    /// Whether the clients supports related documents for document diagnostic pulls.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub related_document_support: Option<bool>,
}

/// Diagnostic options.
///
/// @since 3.17.0
#[derive(Debug, Eq, PartialEq, Clone, Default, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DiagnosticOptions {
    /// An optional identifier under which the diagnostics are
    /// managed by the client.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub identifier: Option<String>,

    /// Whether the language has inter file dependencies, meaning that editing code in one file can
    /// result in a different diagnostic set in another file. Inter file dependencies are common
    /// for most programming languages and typically uncommon for linters.
    pub inter_file_dependencies: bool,

    /// The server provides support for workspace diagnostics as well.
    pub workspace_diagnostics: bool,

    #[serde(flatten)]
    pub work_done_progress_options: WorkDoneProgressOptions,
}

/// Diagnostic registration options.
///
/// @since 3.17.0
#[derive(Debug, Eq, PartialEq, Clone, Default, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DiagnosticRegistrationOptions {
    #[serde(flatten)]
    pub text_document_registration_options: TextDocumentRegistrationOptions,

    #[serde(flatten)]
    pub diagnostic_options: DiagnosticOptions,

    #[serde(flatten)]
    pub static_registration_options: StaticRegistrationOptions,
}

#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
#[serde(untagged)]
pub enum DiagnosticServerCapabilities {
    Options(DiagnosticOptions),
    RegistrationOptions(DiagnosticRegistrationOptions),
}

/// Parameters of the document diagnostic request.
///
/// @since 3.17.0
#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DocumentDiagnosticParams {
    /// The text document.
    pub text_document: TextDocumentIdentifier,

    /// The additional identifier provided during registration.
    pub identifier: Option<String>,

    /// The result ID of a previous response if provided.
    pub previous_result_id: Option<String>,

    #[serde(flatten)]
    pub work_done_progress_params: WorkDoneProgressParams,

    #[serde(flatten)]
    pub partial_result_params: PartialResultParams,
}

/// A diagnostic report with a full set of problems.
///
/// @since 3.17.0
#[derive(Debug, PartialEq, Default, Deserialize, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct FullDocumentDiagnosticReport {
    /// An optional result ID. If provided it will be sent on the next diagnostic request for the
    /// same document.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub result_id: Option<String>,

    /// The actual items.
    pub items: Vec<Diagnostic>,
}

/// A diagnostic report indicating that the last returned report is still accurate.
///
/// A server can only return `unchanged` if result ids are provided.
///
/// @since 3.17.0
#[derive(Debug, PartialEq, Deserialize, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct UnchangedDocumentDiagnosticReport {
    /// A result ID which will be sent on the next diagnostic request for the same document.
    pub result_id: String,
}

/// The document diagnostic report kinds.
///
/// @since 3.17.0
#[derive(Debug, PartialEq, Deserialize, Serialize, Clone)]
#[serde(tag = "kind", rename_all = "lowercase")]
pub enum DocumentDiagnosticReportKind {
    /// A diagnostic report with a full set of problems.
    Full(FullDocumentDiagnosticReport),
    /// A report indicating that the last returned report is still accurate.
    Unchanged(UnchangedDocumentDiagnosticReport),
}

impl From<FullDocumentDiagnosticReport> for DocumentDiagnosticReportKind {
    fn from(from: FullDocumentDiagnosticReport) -> Self {
        DocumentDiagnosticReportKind::Full(from)
    }
}

impl From<UnchangedDocumentDiagnosticReport> for DocumentDiagnosticReportKind {
    fn from(from: UnchangedDocumentDiagnosticReport) -> Self {
        DocumentDiagnosticReportKind::Unchanged(from)
    }
}

/// A full diagnostic report with a set of related documents.
///
/// @since 3.17.0
#[derive(Debug, PartialEq, Default, Deserialize, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct RelatedFullDocumentDiagnosticReport {
    /// Diagnostics of related documents.
    ///
    /// This information is useful in programming languages where code in a file A can generate
    /// diagnostics in a file B which A depends on. An example of such a language is C/C++ where
    /// macro definitions in a file `a.cpp` result in errors in a header file `b.hpp`.
    ///
    /// @since 3.17.0
    #[serde(with = "crate::url_map")]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default)]
    pub related_documents: Option<HashMap<Url, DocumentDiagnosticReportKind>>,
    // relatedDocuments?: { [uri: string]: FullDocumentDiagnosticReport | UnchangedDocumentDiagnosticReport; };
    #[serde(flatten)]
    pub full_document_diagnostic_report: FullDocumentDiagnosticReport,
}

/// An unchanged diagnostic report with a set of related documents.
///
/// @since 3.17.0
#[derive(Debug, PartialEq, Deserialize, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct RelatedUnchangedDocumentDiagnosticReport {
    /// Diagnostics of related documents.
    ///
    /// This information is useful in programming languages where code in a file A can generate
    /// diagnostics in a file B which A depends on. An example of such a language is C/C++ where
    /// macro definitions in a file `a.cpp` result in errors in a header file `b.hpp`.
    ///
    /// @since 3.17.0
    #[serde(with = "crate::url_map")]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default)]
    pub related_documents: Option<HashMap<Url, DocumentDiagnosticReportKind>>,
    // relatedDocuments?: { [uri: string]: FullDocumentDiagnosticReport | UnchangedDocumentDiagnosticReport; };
    #[serde(flatten)]
    pub unchanged_document_diagnostic_report: UnchangedDocumentDiagnosticReport,
}

/// The result of a document diagnostic pull request.
///
/// A report can either be a full report containing all diagnostics for the requested document or
/// an unchanged report indicating that nothing has changed in terms of diagnostics in comparison
/// to the last pull request.
///
/// @since 3.17.0
#[derive(Debug, PartialEq, Deserialize, Serialize, Clone)]
#[serde(tag = "kind", rename_all = "lowercase")]
pub enum DocumentDiagnosticReport {
    /// A diagnostic report with a full set of problems.
    Full(RelatedFullDocumentDiagnosticReport),
    /// A report indicating that the last returned report is still accurate.
    Unchanged(RelatedUnchangedDocumentDiagnosticReport),
}

impl From<RelatedFullDocumentDiagnosticReport> for DocumentDiagnosticReport {
    fn from(from: RelatedFullDocumentDiagnosticReport) -> Self {
        DocumentDiagnosticReport::Full(from)
    }
}

impl From<RelatedUnchangedDocumentDiagnosticReport> for DocumentDiagnosticReport {
    fn from(from: RelatedUnchangedDocumentDiagnosticReport) -> Self {
        DocumentDiagnosticReport::Unchanged(from)
    }
}

/// A partial result for a document diagnostic report.
///
/// @since 3.17.0
#[derive(Debug, PartialEq, Default, Deserialize, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct DocumentDiagnosticReportPartialResult {
    #[serde(with = "crate::url_map")]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default)]
    pub related_documents: Option<HashMap<Url, DocumentDiagnosticReportKind>>,
    // relatedDocuments?: { [uri: string]: FullDocumentDiagnosticReport | UnchangedDocumentDiagnosticReport; };
}

#[derive(Debug, PartialEq, Deserialize, Serialize, Clone)]
#[serde(untagged)]
pub enum DocumentDiagnosticReportResult {
    Report(DocumentDiagnosticReport),
    Partial(DocumentDiagnosticReportPartialResult),
}

impl From<DocumentDiagnosticReport> for DocumentDiagnosticReportResult {
    fn from(from: DocumentDiagnosticReport) -> Self {
        DocumentDiagnosticReportResult::Report(from)
    }
}

impl From<DocumentDiagnosticReportPartialResult> for DocumentDiagnosticReportResult {
    fn from(from: DocumentDiagnosticReportPartialResult) -> Self {
        DocumentDiagnosticReportResult::Partial(from)
    }
}

/// Cancellation data returned from a diagnostic request.
///
/// If no data is provided, it defaults to `{ retrigger_request: true }`.
///
/// @since 3.17.0
#[derive(Debug, PartialEq, Deserialize, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct DiagnosticServerCancellationData {
    pub retrigger_request: bool,
}

impl Default for DiagnosticServerCancellationData {
    fn default() -> Self {
        DiagnosticServerCancellationData {
            retrigger_request: true,
        }
    }
}