Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/IPC/WindServiceHandlers/Model/
ModelUpdateContent.rs

1//! Replace an open model's content. Increments `Version`,
2//! recomputes `Lines`, marks `IsDirty=true`. Mirrors VS Code's
3//! `TextDocument.update(...)` semantics - the Monaco model
4//! observers see a single coherent edit, not partial state.
5//!
6//! Errors when the URI isn't open; callers must `ModelOpen`
7//! first.
8
9use std::sync::Arc;
10
11use serde_json::{Value, json};
12
13use crate::RunTime::ApplicationRunTime::ApplicationRunTime;
14
15pub async fn Fn(RunTime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
16	let Uri = Arguments
17		.first()
18		.and_then(|V| V.as_str())
19		.ok_or("model:updateContent requires uri".to_string())?
20		.to_owned();
21
22	let NewContent = Arguments
23		.get(1)
24		.and_then(|V| V.as_str())
25		.ok_or("model:updateContent requires content".to_string())?
26		.to_owned();
27
28	let (NewVersion, LanguageId) = match RunTime.Environment.ApplicationState.Feature.Documents.Get(&Uri) {
29		None => return Err(format!("model:updateContent - model not open: {}", Uri)),
30
31		Some(mut Document) => {
32			Document.Version += 1;
33
34			Document.Lines = NewContent.lines().map(|L| L.to_owned()).collect();
35
36			Document.IsDirty = true;
37
38			let Version = Document.Version;
39
40			let LangId = Document.LanguageIdentifier.clone();
41
42			RunTime
43				.Environment
44				.ApplicationState
45				.Feature
46				.Documents
47				.AddOrUpdate(Uri.clone(), Document);
48
49			(Version, LangId)
50		},
51	};
52
53	// Notify Cocoon so `onDidChangeTextDocument` fires with the new content.
54	let UriForCocoon = Uri.clone();
55
56	let ContentForCocoon = NewContent.clone();
57
58	let VersionForCocoon = NewVersion;
59
60	tokio::spawn(async move {
61		let _ = crate::Vine::Client::SendNotification::Fn(
62			"cocoon-main".to_string(),
63			"$acceptModelChanged".to_string(),
64			serde_json::json!([
65				{ "external": UriForCocoon, "$mid": 1 },
66
67				{ "content": ContentForCocoon, "versionId": VersionForCocoon, "isDirty": true, "changes": [] }
68			]),
69		)
70		.await;
71	});
72
73	Ok(json!({
74		"uri": Uri,
75		"content": NewContent,
76		"version": NewVersion,
77		"languageId": LanguageId,
78	}))
79}