Skip to main content

Mountain/IPC/WindServiceHandlers/Model/
ModelUpdateContent.rs

1#![allow(non_snake_case)]
2
3//! Replace an open model's content. Increments `Version`,
4//! recomputes `Lines`, marks `IsDirty=true`. Mirrors VS Code's
5//! `TextDocument.update(...)` semantics - the Monaco model
6//! observers see a single coherent edit, not partial state.
7//!
8//! Errors when the URI isn't open; callers must `ModelOpen`
9//! first.
10
11use std::sync::Arc;
12
13use serde_json::{Value, json};
14
15use crate::RunTime::ApplicationRunTime::ApplicationRunTime;
16
17pub async fn ModelUpdateContent(RunTime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
18	let Uri = Arguments
19		.first()
20		.and_then(|V| V.as_str())
21		.ok_or("model:updateContent requires uri".to_string())?
22		.to_owned();
23
24	let NewContent = Arguments
25		.get(1)
26		.and_then(|V| V.as_str())
27		.ok_or("model:updateContent requires content".to_string())?
28		.to_owned();
29
30	let (NewVersion, LanguageId) = match RunTime.Environment.ApplicationState.Feature.Documents.Get(&Uri) {
31		None => return Err(format!("model:updateContent - model not open: {}", Uri)),
32		Some(mut Document) => {
33			Document.Version += 1;
34			Document.Lines = NewContent.lines().map(|L| L.to_owned()).collect();
35			Document.IsDirty = true;
36			let Version = Document.Version;
37			let LangId = Document.LanguageIdentifier.clone();
38			RunTime
39				.Environment
40				.ApplicationState
41				.Feature
42				.Documents
43				.AddOrUpdate(Uri.clone(), Document);
44			(Version, LangId)
45		},
46	};
47
48	Ok(json!({
49		"uri": Uri,
50		"content": NewContent,
51		"version": NewVersion,
52		"languageId": LanguageId,
53	}))
54}