on master help sign in
Save command
commit3f707c19f46284d745568c632a57103eb1be4dd4 authorBlaž Hrastnik <blaz@mxxn.io> dateFri 30 Oct 2020, 17:00 (6 years ago) parent8f0bcfe
5 files changed+41−11
Cargo.lock+1 helix-term/src/main.rs+3−9 helix-view/Cargo.toml+2 helix-view/src/commands.rs+10−2 helix-view/src/document.rs+25
Cargo.lock+1
@@ -523,6 +523,7 @@ dependencies = [
523523 "crossterm",
524524 "helix-core",
525525 "once_cell",
526+ "smol",
526527 "tui",
527528 "url",
528529]
helix-term/src/main.rs+3−9
@@ -70,16 +70,10 @@ fn main() -> Result<(), Error> {
7070 std::thread::spawn(move || smol::block_on(EX.run(smol::future::pending::<()>())));
7171 }
7272
73− // let mut lsp = helix_lsp::Client::start(&EX, "rust-analyzer", &[]);
73+ let mut app = Application::new(args, &EX).unwrap();
7474
75− smol::block_on(async {
76− // let res = lsp.initialize().await;
77− // let state = helix_core::State::load("test.rs".into(), &[]).unwrap();
78− // let res = lsp.text_document_did_open(&state).await;
79− // loop {}
80−
81− Application::new(args, &EX).unwrap().run().await;
82− });
75+ // we use the thread local executor to spawn the application task separately from the work pool
76+ smol::block_on(app.run());
8377
8478 Ok(())
8579}
helix-view/Cargo.toml+2
@@ -20,3 +20,5 @@ tui = { git = "https://github.com/fdehau/tui-rs", default-features = false, feat
2020crossterm = { version = "0.18", features = ["event-stream"], optional = true}
2121once_cell = "1.4"
2222url = "2"
23+
24+smol = "1"
helix-view/src/commands.rs+10−2
@@ -331,8 +331,7 @@ pub fn append_mode(cx: &mut Context) {
331331}
332332
333333// TODO: I, A, o and O can share a lot of the primitives.
334−
335−pub fn command_mode(_view: &mut View, _count: usize) {
334+pub fn command_mode(_cx: &mut Context) {
336335 unimplemented!()
337336}
338337
@@ -670,3 +669,12 @@ pub fn indent_selection(_cx: &mut Context) {
670669 // loop over each line and recompute proper indentation
671670 unimplemented!()
672671}
672+
673+//
674+
675+pub fn save(cx: &mut Context) {
676+ // Spawns an async task to actually do the saving. This way we prevent blocking.
677+
678+ // TODO: handle save errors somehow?
679+ cx.executor.spawn(cx.view.doc.save()).detach();
680+}
helix-view/src/document.rs+25
@@ -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();