@@ -1,4 +1,5 @@
11use anyhow::Error;
2+use std::future::Future;
23use std::path::PathBuf;
34
45use helix_core::{
@@ -97,6 +98,30 @@ impl Document {
9798 Ok(doc)
9899 }
99100
101+ pub fn save(&self) -> impl Future<Output = Result<(), anyhow::Error>> {
102+ // we clone and move text + path into the future so that we asynchronously save the current
103+ // state without blocking any further edits.
104+
105+ let text = self.text().clone();
106+ let path = self.path.clone().expect("Can't save with no path set!"); // TODO: handle no path
107+
108+ // TODO: mark changes up to now as saved
109+ // TODO: mark dirty false
110+
111+ async move {
112+ use smol::{fs::File, prelude::*};
113+ let mut file = File::create(path).await?;
114+
115+ // write all the rope chunks to file
116+ for chunk in text.chunks() {
117+ file.write_all(chunk.as_bytes()).await?;
118+ }
119+ // TODO: flush?
120+
121+ Ok(())
122+ } // and_then(// lsp.send_text_saved_notification())
123+ }
124+
100125 pub fn set_language(&mut self, scope: &str, scopes: &[String]) {
101126 if let Some(language_config) = LOADER.language_config_for_scope(scope) {
102127 let highlight_config = language_config.highlight_config(scopes).unwrap().unwrap();