Common/Document/ApplyDocumentChanges.rs
1//! # ApplyDocumentChanges Effect
2//!
3//! Defines the `ActionEffect` for applying a collection of text content
4//! changes to a document.
5
6use std::sync::Arc;
7
8use serde_json::Value;
9use url::Url;
10
11use super::DocumentProvider::DocumentProvider;
12use crate::{Effect::ActionEffect::ActionEffect, Error::CommonError::CommonError};
13
14/// Creates an effect that, when executed, will apply a collection of content
15/// changes to the document at the given URI. This is the primary mechanism
16/// for handling edits originating from the extension host.
17///
18/// It uses the `DocumentProvider` capability from the environment to perform
19/// the operation, which involves updating the in-memory representation of the
20/// document's text.
21///
22/// # Parameters
23/// * `URI`: The `Url` of the document to modify.
24/// * `NewVersionIdentifier`: The new version ID of the document after the
25/// change is applied.
26/// * `ChangesDTOCollection`: A DTO representing the set of text changes to
27/// apply.
28/// * `IsDirtyAfterChange`: A flag indicating the document's dirty state after
29/// the change.
30/// * `IsUndoing`: A flag indicating if this change is part of an "undo"
31/// operation.
32/// * `IsRedoing`: A flag indicating if this change is part of a "redo"
33/// operation.
34///
35/// # Returns
36/// An `ActionEffect` that resolves to `()` on success.
37pub fn ApplyDocumentChanges(
38 URI:Url,
39
40 NewVersionIdentifier:i64,
41
42 ChangesDTOCollection:Value,
43
44 IsDirtyAfterChange:bool,
45
46 IsUndoing:bool,
47
48 IsRedoing:bool,
49) -> ActionEffect<Arc<dyn DocumentProvider>, CommonError, ()> {
50 ActionEffect::New(Arc::new(move |Provider:Arc<dyn DocumentProvider>| {
51 let URIClone = URI.clone();
52
53 let ChangesClone = ChangesDTOCollection.clone();
54
55 Box::pin(async move {
56 Provider
57 .ApplyDocumentChanges(
58 URIClone,
59 NewVersionIdentifier,
60 ChangesClone,
61 IsDirtyAfterChange,
62 IsUndoing,
63 IsRedoing,
64 )
65 .await
66 })
67 }))
68}