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
33		Some(mut Document) => {
34			Document.Version += 1;
35
36			Document.Lines = NewContent.lines().map(|L| L.to_owned()).collect();
37
38			Document.IsDirty = true;
39
40			let Version = Document.Version;
41
42			let LangId = Document.LanguageIdentifier.clone();
43
44			RunTime
45				.Environment
46				.ApplicationState
47				.Feature
48				.Documents
49				.AddOrUpdate(Uri.clone(), Document);
50
51			(Version, LangId)
52		},
53	};
54
55	Ok(json!({
56		"uri": Uri,
57		"content": NewContent,
58		"version": NewVersion,
59		"languageId": LanguageId,
60	}))
61}