Skip to main content

Mountain/Track/Effect/CreateEffectForRequest/
Diagnostics.rs

1use std::sync::Arc;
2
3use CommonLibrary::{Diagnostic::DiagnosticManager::DiagnosticManager, Environment::Requires::Requires};
4use serde_json::{Value, json};
5use tauri::Runtime;
6
7use crate::Track::Effect::{
8	CreateEffectForRequest::Utilities::Params::{string_at, val_at},
9	MappedEffectType::MappedEffect,
10};
11
12pub fn CreateEffect<R:Runtime>(MethodName:&str, Parameters:Value) -> Option<Result<MappedEffect, String>> {
13	match MethodName {
14		"Diagnostic.Set" => {
15			crate::effect!(run_time, {
16				let provider:Arc<dyn DiagnosticManager> = run_time.Environment.Require();
17
18				let owner = string_at(&Parameters, 0);
19
20				let entries = val_at(&Parameters, 1);
21
22				let Result = provider
23					.SetDiagnostics(owner.clone(), entries.clone())
24					.await
25					.map(|_| json!(null))
26					.map_err(|e| e.to_string());
27
28				// Fan back to Cocoon so peer extensions hooking
29				// `vscode.languages.onDidChangeDiagnostics` observe the
30				// change. The matching subscriber lives at
31				// `Languages/Namespace.ts:1140` on the
32				// `diagnostics.didChange` Emitter channel. Extract the
33				// list of changed URIs from the entries payload
34				// (`entries` is `[[uriString, diagnostics[]], ...]`).
35				let Uris:Vec<Value> = entries
36					.as_array()
37					.map(|Arr| {
38						Arr.iter()
39							.filter_map(|Pair| Pair.as_array().and_then(|P| P.first().cloned()))
40							.collect()
41					})
42					.unwrap_or_default();
43
44				let _ = ::Vine::Client::SendNotification::Fn(
45					"cocoon-main".to_string(),
46					"$acceptDiagnosticsChanged".to_string(),
47					json!({ "owner": owner, "uris": Uris }),
48				)
49				.await;
50
51				Result
52			})
53		},
54
55		"Diagnostic.Clear" => {
56			crate::effect!(run_time, {
57				let provider:Arc<dyn DiagnosticManager> = run_time.Environment.Require();
58
59				let owner = string_at(&Parameters, 0);
60
61				let Result = provider
62					.ClearDiagnostics(owner.clone())
63					.await
64					.map(|_| json!(null))
65					.map_err(|e| e.to_string());
66
67				// Clear translates to "every URI previously held by this
68				// owner now has zero diagnostics". Without knowing the
69				// prior URI set we send an empty `uris` list - the
70				// `onDidChangeDiagnostics` subscriber should re-query
71				// `getDiagnostics(uri)` if it needs the new state.
72				let _ = ::Vine::Client::SendNotification::Fn(
73					"cocoon-main".to_string(),
74					"$acceptDiagnosticsChanged".to_string(),
75					json!({ "owner": owner, "uris": [] }),
76				)
77				.await;
78
79				Result
80			})
81		},
82
83		_ => None,
84	}
85}