Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/IPC/WindServiceHandlers/Model/
TextfileWrite.rs

1//! Write text to a file on disk. Counterpart to `TextfileRead`;
2//! does not touch the document registry. After a successful disk
3//! write, fires `$acceptModelSaved` to Cocoon so extensions
4//! receive `onDidSaveTextDocument` (T1.4).
5
6use std::sync::Arc;
7
8use serde_json::{Value, json};
9
10use crate::{
11	IPC::WindServiceHandlers::Utilities::JsonValueHelpers::arg_string,
12	RunTime::ApplicationRunTime::ApplicationRunTime,
13	dev_log,
14};
15
16pub async fn Fn(_runtime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
17	let Path = Arguments
18		.first()
19		.and_then(|V| V.as_str())
20		.ok_or_else(|| "textFile:write requires path as first argument".to_string())?
21		.to_string();
22
23	let Content = arg_string(&Arguments, 1);
24
25	tokio::fs::write(&Path, Content.as_bytes())
26		.await
27		.map_err(|Error| format!("textFile:write failed: {}", Error))?;
28
29	dev_log!("vfs", "textFile:write ok path={} bytes={}", Path, Content.len());
30
31	// T1.4 - notify Cocoon that the model on disk now matches the editor
32	// buffer, firing `onDidSaveTextDocument` for all subscribed extensions
33	// (format-on-save, organize-imports, save listeners, etc.).
34	// Fire-and-forget: the write is already complete; a Vine failure here
35	// must not cause the save IPC call to fail from the workbench's
36	// perspective.
37	let FileUri = format!("file://{}", Path);
38
39	tokio::spawn(async move {
40		if let Err(Error) = crate::Vine::Client::SendNotification::Fn(
41			"cocoon-main".to_string(),
42			"$acceptModelSaved".to_string(),
43			json!({ "uri": FileUri }),
44		)
45		.await
46		{
47			dev_log!("vfs", "warn: [TextfileWrite] $acceptModelSaved notify failed: {:?}", Error);
48		}
49	});
50
51	Ok(Value::Null)
52}