Skip to main content

aft/lsp/
pull_params.rs

1use serde::{Deserialize, Serialize};
2
3/// Parameters for `textDocument/diagnostic` pull requests.
4///
5/// This mirrors `lsp_types::DocumentDiagnosticParams`, but omits absent
6/// optional fields from the JSON payload. The LSP 3.17 schema allows
7/// `identifier` and `previousResultId` to be strings or absent, not `null`.
8#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
9#[serde(rename_all = "camelCase")]
10pub struct AftDocumentDiagnosticParams {
11    /// The text document.
12    pub text_document: lsp_types::TextDocumentIdentifier,
13
14    /// The additional identifier provided during registration.
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub identifier: Option<String>,
17
18    /// The result ID of a previous response if provided.
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub previous_result_id: Option<String>,
21
22    #[serde(flatten)]
23    pub work_done_progress_params: lsp_types::WorkDoneProgressParams,
24
25    #[serde(flatten)]
26    pub partial_result_params: lsp_types::PartialResultParams,
27}
28
29#[derive(Debug)]
30pub enum AftDocumentDiagnosticRequest {}
31
32impl lsp_types::request::Request for AftDocumentDiagnosticRequest {
33    type Params = AftDocumentDiagnosticParams;
34    type Result = lsp_types::DocumentDiagnosticReportResult;
35    const METHOD: &'static str = "textDocument/diagnostic";
36}
37
38/// Parameters for `workspace/diagnostic` pull requests.
39///
40/// This mirrors `lsp_types::WorkspaceDiagnosticParams`, but omits absent
41/// optional fields from the JSON payload. The LSP 3.17 schema allows
42/// `identifier` to be a string or absent, not `null`.
43#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
44#[serde(rename_all = "camelCase")]
45pub struct AftWorkspaceDiagnosticParams {
46    /// The additional identifier provided during registration.
47    #[serde(skip_serializing_if = "Option::is_none")]
48    pub identifier: Option<String>,
49
50    /// The currently known diagnostic reports with their previous result ids.
51    pub previous_result_ids: Vec<lsp_types::PreviousResultId>,
52
53    #[serde(flatten)]
54    pub work_done_progress_params: lsp_types::WorkDoneProgressParams,
55
56    #[serde(flatten)]
57    pub partial_result_params: lsp_types::PartialResultParams,
58}
59
60#[derive(Debug)]
61pub enum AftWorkspaceDiagnosticRequest {}
62
63impl lsp_types::request::Request for AftWorkspaceDiagnosticRequest {
64    type Params = AftWorkspaceDiagnosticParams;
65    type Result = lsp_types::WorkspaceDiagnosticReportResult;
66    const METHOD: &'static str = "workspace/diagnostic";
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72    use std::str::FromStr;
73
74    fn text_document_identifier() -> lsp_types::TextDocumentIdentifier {
75        lsp_types::TextDocumentIdentifier {
76            uri: lsp_types::Uri::from_str("file:///tmp/example.ts").expect("valid test uri"),
77        }
78    }
79
80    #[test]
81    fn document_diagnostic_params_omits_none_fields() {
82        let params = AftDocumentDiagnosticParams {
83            text_document: text_document_identifier(),
84            identifier: None,
85            previous_result_id: None,
86            work_done_progress_params: Default::default(),
87            partial_result_params: Default::default(),
88        };
89
90        let json = serde_json::to_value(&params).expect("serialize params");
91        let object = json.as_object().expect("params object");
92        assert!(!object.contains_key("identifier"));
93        assert!(!object.contains_key("previousResultId"));
94    }
95
96    #[test]
97    fn document_diagnostic_params_serializes_some_fields() {
98        let params = AftDocumentDiagnosticParams {
99            text_document: text_document_identifier(),
100            identifier: Some("tsgo".to_string()),
101            previous_result_id: Some("result-1".to_string()),
102            work_done_progress_params: Default::default(),
103            partial_result_params: Default::default(),
104        };
105
106        let json = serde_json::to_value(&params).expect("serialize params");
107        assert_eq!(json["identifier"], "tsgo");
108        assert_eq!(json["previousResultId"], "result-1");
109    }
110
111    #[test]
112    fn workspace_diagnostic_params_omits_none_identifier() {
113        let params = AftWorkspaceDiagnosticParams {
114            identifier: None,
115            previous_result_ids: Vec::new(),
116            work_done_progress_params: Default::default(),
117            partial_result_params: Default::default(),
118        };
119
120        let json = serde_json::to_value(&params).expect("serialize params");
121        let object = json.as_object().expect("params object");
122        assert!(!object.contains_key("identifier"));
123        assert_eq!(json["previousResultIds"], serde_json::json!([]));
124    }
125
126    #[test]
127    fn workspace_diagnostic_params_serializes_some_identifier() {
128        let params = AftWorkspaceDiagnosticParams {
129            identifier: Some("tsgo".to_string()),
130            previous_result_ids: Vec::new(),
131            work_done_progress_params: Default::default(),
132            partial_result_params: Default::default(),
133        };
134
135        let json = serde_json::to_value(&params).expect("serialize params");
136        assert_eq!(json["identifier"], "tsgo");
137        assert_eq!(json["previousResultIds"], serde_json::json!([]));
138    }
139}