ink_lsp_server/dispatch/
actions.rs1use crate::dispatch::Snapshots;
4use crate::translator;
5
6pub fn publish_diagnostics(
8 uri: &lsp_types::Uri,
9 snapshots: &Snapshots,
10) -> anyhow::Result<lsp_types::PublishDiagnosticsParams> {
11 let (diagnostics, version) = match snapshots.get(uri.as_str()) {
13 Some(snapshot) => {
15 (
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 None => (Vec::new(), None),
30 };
31
32 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 let client_capabilities = simple_client_config();
54
55 for version in [Version::Legacy, Version::V5(MinorVersion::Base)] {
56 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 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 assert_eq!(params.diagnostics.len(), 3);
82 }
83 }
84}