ink_lsp_server/dispatch/
actions.rs

1//! Utilities for composing LSP requests and notifications.
2
3use crate::dispatch::Snapshots;
4use crate::translator;
5
6/// Composes `PublishDiagnostics` notification parameters for a set of documents with changes.
7pub fn publish_diagnostics(
8    uri: &lsp_types::Uri,
9    snapshots: &Snapshots,
10) -> anyhow::Result<lsp_types::PublishDiagnosticsParams> {
11    // Computes diagnostics.
12    let (diagnostics, version) = match snapshots.get(uri.as_str()) {
13        // Computes diagnostics for document.
14        Some(snapshot) => {
15            // Computes ink! analyzer diagnostics and translates them into an LSP diagnostics.
16            (
17                snapshot
18                    .analysis
19                    .diagnostics()
20                    .into_iter()
21                    .filter_map(|diagnostic| {
22                        translator::to_lsp::diagnostic(diagnostic, &snapshot.context)
23                    })
24                    .collect(),
25                snapshot.doc_version,
26            )
27        }
28        // Clears diagnostics for missing documents.
29        None => (Vec::new(), None),
30    };
31
32    // Composes `PublishDiagnostics` notification parameters.
33    Ok(lsp_types::PublishDiagnosticsParams {
34        uri: uri.clone(),
35        diagnostics,
36        version,
37    })
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43    use crate::dispatch::Snapshot;
44    use crate::test_utils::document_uri;
45    use crate::utils;
46    use ink_analyzer::{MinorVersion, Version};
47    use std::collections::HashMap;
48    use test_utils::simple_client_config;
49
50    #[test]
51    fn publish_diagnostics_works() {
52        // Creates client capabilities.
53        let client_capabilities = simple_client_config();
54
55        for version in [Version::Legacy, Version::V5(MinorVersion::Base)] {
56            // Creates test document snapshot.
57            let uri = document_uri();
58            let mut snapshots = HashMap::new();
59            snapshots.insert(
60                uri.to_string(),
61                Snapshot::new(
62                    String::from(
63                        r#"
64                        #[ink::contract]
65                        mod my_contract {
66                        }
67                    "#,
68                    ),
69                    utils::position_encoding(&client_capabilities),
70                    Some(0),
71                    version,
72                ),
73            );
74
75            // Composes `PublishDiagnostics` notification parameters for the changes and verifies the expected results.
76            let result = publish_diagnostics(&uri, &snapshots);
77            assert!(result.is_ok());
78            let params = result.as_ref().unwrap();
79            assert_eq!(params.uri, uri);
80            // 3 Expected diagnostics for missing storage, constructor and message.
81            assert_eq!(params.diagnostics.len(), 3);
82        }
83    }
84}