Skip to main content

Mountain/Environment/DocumentProvider/
ApplyChanges.rs

1//! Document change application logic.
2//!
3//! Implements `ApplyDocumentChanges` for `MountainEnvironment`. Receives an
4//! LSP-style `DidChangeTextDocument` payload (a JSON array of range-based
5//! text edits), applies it to the in-memory `DocumentStateDTO` stored in
6//! `ApplicationState::Feature::Documents::OpenDocuments`, then forwards the
7//! same payload to Cocoon via `notify_model_changed` so the extension host's
8//! model mirrors the workbench state.
9//!
10//! Unknown URIs (document not in `OpenDocuments`) are silently ignored with
11//! a `warn:` log - this can legitimately happen during the brief window
12//! between a document being closed on the Sky side and the last in-flight
13//! change notification arriving from Cocoon.
14
15use CommonLibrary::Error::CommonError::CommonError;
16use serde_json::Value;
17use url::Url;
18
19use crate::{Environment::Utility, dev_log};
20
21/// Applies a collection of content changes to a document.
22pub(super) async fn apply_document_changes(
23	environment:&crate::Environment::MountainEnvironment::MountainEnvironment,
24
25	uri:Url,
26
27	new_version_identifier:i64,
28
29	changes_dto_collection:Value,
30
31	_is_dirty_after_change:bool,
32
33	_is_undoing:bool,
34
35	_is_redoing:bool,
36) -> Result<(), CommonError> {
37	dev_log!("model", "[DocumentProvider] Applying changes to document: {}", uri);
38
39	{
40		let mut open_documents_guard = environment
41			.ApplicationState
42			.Feature
43			.Documents
44			.OpenDocuments
45			.lock()
46			.map_err(Utility::ErrorMapping::MapApplicationStateLockErrorToCommonError)?;
47
48		if let Some(document) = open_documents_guard.get_mut(uri.as_str()) {
49			document.ApplyChanges(new_version_identifier, &changes_dto_collection)?;
50		} else {
51			dev_log!(
52				"model",
53				"warn: [DocumentProvider] Received changes for unknown document: {}",
54				uri
55			);
56
57			return Ok(());
58		}
59	}
60
61	super::Notifications::notify_model_changed(environment, &uri, new_version_identifier, changes_dto_collection).await;
62
63	Ok(())
64}