Common/Document/DocumentProvider.rs
1//! # DocumentProvider Trait
2//!
3//! Defines the abstract service trait for document lifecycle and modification
4//! capabilities.
5
6use async_trait::async_trait;
7use serde_json::Value;
8use url::Url;
9
10use crate::{Environment::Environment::Environment, Error::CommonError::CommonError};
11
12/// An abstract service contract for an environment component that can manage
13/// the lifecycle and content of text documents.
14///
15/// This trait is implemented by `MountainEnvironment` and is responsible for
16/// maintaining the "single source of truth" for all open documents, handling
17/// file I/O, and synchronizing state with the `Cocoon` sidecar.
18#[async_trait]
19pub trait DocumentProvider: Environment + Send + Sync {
20 /// Opens an existing document from a URI or creates a new untitled document
21 /// with initial content.
22 ///
23 /// # Parameters
24 /// * `URIComponentsDTO`: A DTO representing the URI of the document to
25 /// open.
26 /// * `LanguageIdentifier`: An optional language ID, typically for new
27 /// documents.
28 /// * `Content`: Optional initial content for a new, untitled document.
29 ///
30 /// # Returns
31 /// A `Result` containing the canonical `Url` of the opened document.
32 async fn OpenDocument(
33 &self,
34
35 URIComponentsDTO:Value,
36
37 LanguageIdentifier:Option<String>,
38
39 Content:Option<String>,
40 ) -> Result<Url, CommonError>;
41
42 /// Saves the document at the given URI to disk.
43 async fn SaveDocument(&self, URI:Url) -> Result<bool, CommonError>;
44
45 /// Saves the document currently identified by `OriginalURI` to a new
46 /// location. If `NewTargetURI` is `None`, the user should be prompted to
47 /// select a location.
48 async fn SaveDocumentAs(&self, OriginalURI:Url, NewTargetURI:Option<Url>) -> Result<Option<Url>, CommonError>;
49
50 /// Saves all currently "dirty" (modified) documents.
51 ///
52 /// # Parameters
53 /// * `IncludeUntitled`: If `true`, also attempts to save untitled
54 /// documents, which will typically trigger a "Save As" dialog for each.
55 async fn SaveAllDocuments(&self, IncludeUntitled:bool) -> Result<Vec<bool>, CommonError>;
56
57 /// Applies a collection of content changes to the document at the given
58 /// URI. This is the primary method for handling edits from the extension
59 /// host.
60 ///
61 /// # Parameters
62 /// * `URI`: The URI of the document to modify.
63 /// * `NewVersionIdentifier`: The new version ID of the document after the
64 /// change.
65 /// * `ChangesDTOCollection`: A DTO representing the set of text changes to
66 /// apply.
67 /// * `IsDirtyAfterChange`: A flag indicating the document's dirty state
68 /// after the change.
69 /// * `IsUndoing`: A flag indicating if this change is part of an "undo"
70 /// operation.
71 /// * `IsRedoing`: A flag indicating if this change is part of a "redo"
72 /// operation.
73 async fn ApplyDocumentChanges(
74 &self,
75
76 URI:Url,
77
78 NewVersionIdentifier:i64,
79
80 ChangesDTOCollection:Value,
81
82 IsDirtyAfterChange:bool,
83
84 IsUndoing:bool,
85
86 IsRedoing:bool,
87 ) -> Result<(), CommonError>;
88}