on master help sign in
various fixes in write-all path
commit57de4e62519a59aece104867569c2b9ad044af54 authorSkyler Hawthorne <skyler@dead10ck.com> dateWed 24 Aug 2022, 01:13 (4 years ago) parent7b11e9a
6 files changed+175−25
helix-term/src/commands/typed.rs+19−6 helix-term/src/compositor.rs+6−6 helix-term/tests/integration.rs+1 helix-term/tests/test/commands.rs+2−10 helix-term/tests/test/helpers.rs+25−3 helix-term/tests/test/splits.rs+122
helix-term/src/commands/typed.rs+19−6
@@ -573,13 +573,20 @@ fn write_all_impl(
573573 return Ok(());
574574 }
575575
576− let mut errors = String::new();
576+ let mut errors: Option<String> = None;
577577 let auto_format = cx.editor.config().auto_format;
578578 let jobs = &mut cx.jobs;
579+
579580 // save all documents
580581 for doc in &mut cx.editor.documents.values_mut() {
581582 if doc.path().is_none() {
582− errors.push_str("cannot write a buffer without a filename\n");
583+ errors = errors
584+ .or_else(|| Some(String::new()))
585+ .map(|mut errs: String| {
586+ errs.push_str("cannot write a buffer without a filename\n");
587+ errs
588+ });
589+
583590 continue;
584591 }
585592
@@ -591,7 +598,7 @@ fn write_all_impl(
591598 doc.auto_format().map(|fmt| {
592599 let callback =
593600 make_format_callback(doc.id(), doc.version(), fmt, Some((None, force)));
594− jobs.callback(callback);
601+ jobs.add(Job::with_callback(callback).wait_before_exiting());
595602 })
596603 } else {
597604 None
@@ -603,12 +610,12 @@ fn write_all_impl(
603610 }
604611
605612 if quit {
613+ cx.block_try_flush_writes()?;
614+
606615 if !force {
607616 buffers_remaining_impl(cx.editor)?;
608617 }
609618
610− cx.block_try_flush_writes()?;
611−
612619 // close all views
613620 let views: Vec<_> = cx.editor.tree.views().map(|(view, _)| view.id).collect();
614621 for view_id in views {
@@ -616,7 +623,13 @@ fn write_all_impl(
616623 }
617624 }
618625
619− bail!(errors)
626+ if let Some(errs) = errors {
627+ if !force {
628+ bail!(errs);
629+ }
630+ }
631+
632+ Ok(())
620633}
621634
622635fn write_all(
helix-term/src/compositor.rs+6−6
@@ -29,17 +29,17 @@ pub struct Context<'a> {
2929
3030impl<'a> Context<'a> {
3131 /// Waits on all pending jobs, and then tries to flush all pending write
32− /// operations for the current document.
32+ /// operations for all documents.
3333 pub fn block_try_flush_writes(&mut self) -> anyhow::Result<()> {
3434 tokio::task::block_in_place(|| {
3535 helix_lsp::block_on(self.jobs.finish(Some(self.editor), None))
3636 })?;
3737
38− let doc = doc_mut!(self.editor);
39−
40− tokio::task::block_in_place(|| helix_lsp::block_on(doc.try_flush_saves()))
41− .map(|result| result.map(|_| ()))
42− .unwrap_or(Ok(()))?;
38+ for doc in &mut self.editor.documents.values_mut() {
39+ tokio::task::block_in_place(|| helix_lsp::block_on(doc.try_flush_saves()))
40+ .map(|result| result.map(|_| ()))
41+ .unwrap_or(Ok(()))?;
42+ }
4343
4444 Ok(())
4545 }
helix-term/tests/integration.rs+1
@@ -22,5 +22,6 @@ mod test {
2222 mod commands;
2323 mod movement;
2424 mod prompt;
25+ mod splits;
2526 mod write;
2627}
helix-term/tests/test/commands.rs+2−10
@@ -1,7 +1,4 @@
1−use std::{
2− io::{Read, Write},
3− ops::RangeInclusive,
4−};
1+use std::ops::RangeInclusive;
52
63use helix_core::diagnostic::Severity;
74use helix_term::application::Application;
@@ -86,12 +83,7 @@ async fn test_buffer_close_concurrent() -> anyhow::Result<()> {
8683 )
8784 .await?;
8885
89− file.as_file_mut().flush()?;
90− file.as_file_mut().sync_all()?;
91−
92− let mut file_content = String::new();
93− file.as_file_mut().read_to_string(&mut file_content)?;
94− assert_eq!(RANGE.end().to_string(), file_content);
86+ helpers::assert_file_has_content(file.as_file_mut(), &RANGE.end().to_string())?;
9587
9688 Ok(())
9789}
helix-term/tests/test/helpers.rs+25−3
@@ -1,10 +1,15 @@
1−use std::{io::Write, path::PathBuf, time::Duration};
1+use std::{
2+ fs::File,
3+ io::{Read, Write},
4+ path::PathBuf,
5+ time::Duration,
6+};
27
38use anyhow::bail;
49use crossterm::event::{Event, KeyEvent};
5−use helix_core::{test, Selection, Transaction};
10+use helix_core::{diagnostic::Severity, test, Selection, Transaction};
611use helix_term::{application::Application, args::Args, config::Config};
7−use helix_view::{doc, input::parse_macro};
12+use helix_view::{doc, input::parse_macro, Editor};
813use tempfile::NamedTempFile;
914use tokio_stream::wrappers::UnboundedReceiverStream;
1015
@@ -213,3 +218,20 @@ pub fn app_with_file<P: Into<PathBuf>>(path: P) -> anyhow::Result<Application> {
213218 Config::default(),
214219 )
215220}
221+
222+pub fn assert_file_has_content(file: &mut File, content: &str) -> anyhow::Result<()> {
223+ file.flush()?;
224+ file.sync_all()?;
225+
226+ let mut file_content = String::new();
227+ file.read_to_string(&mut file_content)?;
228+ assert_eq!(content, file_content);
229+
230+ Ok(())
231+}
232+
233+pub fn assert_status_not_error(editor: &Editor) {
234+ if let Some((_, sev)) = editor.get_status() {
235+ assert_ne!(&Severity::Error, sev);
236+ }
237+}
helix-term/tests/test/splits.rsnew file+122
@@ -0,0 +1,122 @@
1+use super::*;
2+
3+#[tokio::test(flavor = "multi_thread")]
4+async fn test_split_write_quit_all() -> anyhow::Result<()> {
5+ let mut file1 = tempfile::NamedTempFile::new()?;
6+ let mut file2 = tempfile::NamedTempFile::new()?;
7+ let mut file3 = tempfile::NamedTempFile::new()?;
8+
9+ test_key_sequences(
10+ &mut helpers::app_with_file(file1.path())?,
11+ vec![
12+ (
13+ Some(&format!(
14+ "ihello1<esc>:sp<ret>:o {}<ret>ihello2<esc>:sp<ret>:o {}<ret>ihello3<esc>",
15+ file2.path().to_string_lossy(),
16+ file3.path().to_string_lossy()
17+ )),
18+ Some(&|app| {
19+ let docs: Vec<_> = app.editor.documents().collect();
20+ assert_eq!(3, docs.len());
21+
22+ let doc1 = docs
23+ .iter()
24+ .find(|doc| doc.path().unwrap() == file1.path())
25+ .unwrap();
26+
27+ assert_eq!("hello1", doc1.text().to_string());
28+
29+ let doc2 = docs
30+ .iter()
31+ .find(|doc| doc.path().unwrap() == file2.path())
32+ .unwrap();
33+
34+ assert_eq!("hello2", doc2.text().to_string());
35+
36+ let doc3 = docs
37+ .iter()
38+ .find(|doc| doc.path().unwrap() == file3.path())
39+ .unwrap();
40+
41+ assert_eq!("hello3", doc3.text().to_string());
42+
43+ helpers::assert_status_not_error(&app.editor);
44+ assert_eq!(3, app.editor.tree.views().count());
45+ }),
46+ ),
47+ (
48+ Some(":wqa<ret>"),
49+ Some(&|app| {
50+ helpers::assert_status_not_error(&app.editor);
51+ assert_eq!(0, app.editor.tree.views().count());
52+ }),
53+ ),
54+ ],
55+ true,
56+ )
57+ .await?;
58+
59+ helpers::assert_file_has_content(file1.as_file_mut(), "hello1")?;
60+ helpers::assert_file_has_content(file2.as_file_mut(), "hello2")?;
61+ helpers::assert_file_has_content(file3.as_file_mut(), "hello3")?;
62+
63+ Ok(())
64+}
65+
66+#[tokio::test(flavor = "multi_thread")]
67+async fn test_split_write_quit_same_file() -> anyhow::Result<()> {
68+ let mut file = tempfile::NamedTempFile::new()?;
69+
70+ test_key_sequences(
71+ &mut helpers::app_with_file(file.path())?,
72+ vec![
73+ (
74+ Some("O<esc>ihello<esc>:sp<ret>ogoodbye<esc>"),
75+ Some(&|app| {
76+ assert_eq!(2, app.editor.tree.views().count());
77+ helpers::assert_status_not_error(&app.editor);
78+
79+ let mut docs: Vec<_> = app.editor.documents().collect();
80+ assert_eq!(1, docs.len());
81+
82+ let doc = docs.pop().unwrap();
83+
84+ assert_eq!(
85+ helpers::platform_line("hello\ngoodbye"),
86+ doc.text().to_string()
87+ );
88+
89+ assert!(doc.is_modified());
90+ }),
91+ ),
92+ (
93+ Some(":wq<ret>"),
94+ Some(&|app| {
95+ helpers::assert_status_not_error(&app.editor);
96+ assert_eq!(1, app.editor.tree.views().count());
97+
98+ let mut docs: Vec<_> = app.editor.documents().collect();
99+ assert_eq!(1, docs.len());
100+
101+ let doc = docs.pop().unwrap();
102+
103+ assert_eq!(
104+ helpers::platform_line("hello\ngoodbye"),
105+ doc.text().to_string()
106+ );
107+
108+ assert!(!doc.is_modified());
109+ }),
110+ ),
111+ ],
112+ false,
113+ )
114+ .await?;
115+
116+ helpers::assert_file_has_content(
117+ file.as_file_mut(),
118+ &helpers::platform_line("hello\ngoodbye"),
119+ )?;
120+
121+ Ok(())
122+}