summaryrefslogtreecommitdiff
path: root/support/texlab/src/workspace/api.rs
diff options
context:
space:
mode:
Diffstat (limited to 'support/texlab/src/workspace/api.rs')
-rw-r--r--support/texlab/src/workspace/api.rs86
1 files changed, 86 insertions, 0 deletions
diff --git a/support/texlab/src/workspace/api.rs b/support/texlab/src/workspace/api.rs
new file mode 100644
index 0000000000..38fa0ce530
--- /dev/null
+++ b/support/texlab/src/workspace/api.rs
@@ -0,0 +1,86 @@
+use std::{fs, path::PathBuf, sync::Arc};
+
+use anyhow::Result;
+
+use crate::{DocumentLanguage, Uri};
+
+use super::Document;
+
+#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, PartialOrd, Ord)]
+pub enum WorkspaceSource {
+ Client,
+ Server,
+}
+
+#[derive(Debug, Clone)]
+pub struct WorkspaceSubset {
+ pub documents: Vec<Arc<Document>>,
+}
+
+pub type OpenHandler = Arc<dyn Fn(Arc<dyn Workspace>, Arc<Document>) + Send + Sync + 'static>;
+
+pub trait Workspace: Send + Sync {
+ fn open(
+ &self,
+ uri: Arc<Uri>,
+ text: String,
+ language: DocumentLanguage,
+ source: WorkspaceSource,
+ ) -> Arc<Document>;
+
+ fn register_open_handler(&self, handler: OpenHandler);
+
+ fn reload(&self, path: PathBuf) -> Result<Option<Arc<Document>>> {
+ let uri = Arc::new(Uri::from_file_path(path.clone()).unwrap());
+
+ if self.is_open(&uri) {
+ return Ok(self.get(&uri));
+ }
+
+ let data = fs::read(&path)?;
+ let text = String::from_utf8_lossy(&data).into_owned();
+ if let Some(language) = DocumentLanguage::by_path(&path) {
+ Ok(Some(self.open(
+ uri,
+ text,
+ language,
+ WorkspaceSource::Server,
+ )))
+ } else {
+ Ok(None)
+ }
+ }
+
+ fn load(&self, path: PathBuf) -> Result<Option<Arc<Document>>> {
+ let uri = Arc::new(Uri::from_file_path(path.clone()).unwrap());
+
+ if let Some(document) = self.get(&uri) {
+ return Ok(Some(document));
+ }
+
+ let data = fs::read(&path)?;
+ let text = String::from_utf8_lossy(&data).into_owned();
+ if let Some(language) = DocumentLanguage::by_path(&path) {
+ Ok(Some(self.open(
+ uri,
+ text,
+ language,
+ WorkspaceSource::Server,
+ )))
+ } else {
+ Ok(None)
+ }
+ }
+
+ fn documents(&self) -> Vec<Arc<Document>>;
+
+ fn has(&self, uri: &Uri) -> bool;
+
+ fn get(&self, uri: &Uri) -> Option<Arc<Document>>;
+
+ fn close(&self, uri: &Uri);
+
+ fn is_open(&self, uri: &Uri) -> bool;
+
+ fn subset(&self, uri: Arc<Uri>) -> Option<WorkspaceSubset>;
+}