aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSkyler Hawthorne2022-08-31 19:08:00 +0000
committerSkyler Hawthorne2022-10-19 02:31:39 +0000
commitaf03df3413f04ce7079d14388ce42fe70bd1397e (patch)
treed23e4e7746c4f5c31ec9709e95798b81beb67252
parent18c32118b1df63895b662c1b37ada28ad0d5c9b5 (diff)
fix write scratch buffer to file
-rw-r--r--helix-term/src/commands/typed.rs4
-rw-r--r--helix-term/tests/test/write.rs71
-rw-r--r--helix-view/src/document.rs18
3 files changed, 67 insertions, 26 deletions
diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs
index 7fd619d9..d312c45f 100644
--- a/helix-term/src/commands/typed.rs
+++ b/helix-term/src/commands/typed.rs
@@ -273,10 +273,6 @@ fn write_impl(
let doc = doc_mut!(cx.editor);
let path = path.map(AsRef::as_ref);
- if doc.path().is_none() {
- bail!("cannot write a buffer without a filename");
- }
-
let fmt = if editor_auto_fmt {
doc.auto_format().map(|fmt| {
let callback = make_format_callback(
diff --git a/helix-term/tests/test/write.rs b/helix-term/tests/test/write.rs
index 544f1ba1..7d105431 100644
--- a/helix-term/tests/test/write.rs
+++ b/helix-term/tests/test/write.rs
@@ -129,6 +129,52 @@ async fn test_write_fail_mod_flag() -> anyhow::Result<()> {
}
#[tokio::test]
+async fn test_write_scratch_to_new_path() -> anyhow::Result<()> {
+ let mut file = tempfile::NamedTempFile::new()?;
+
+ test_key_sequence(
+ &mut Application::new(Args::default(), Config::default())?,
+ Some(format!("ihello<esc>:w {}<ret>", file.path().to_string_lossy()).as_ref()),
+ Some(&|app| {
+ assert!(!app.editor.is_err());
+
+ let mut docs: Vec<_> = app.editor.documents().collect();
+ assert_eq!(1, docs.len());
+
+ let doc = docs.pop().unwrap();
+ assert_eq!(Some(&file.path().to_path_buf()), doc.path());
+ }),
+ false,
+ )
+ .await?;
+
+ helpers::assert_file_has_content(file.as_file_mut(), &helpers::platform_line("hello"))?;
+
+ Ok(())
+}
+
+#[tokio::test]
+async fn test_write_scratch_no_path_fails() -> anyhow::Result<()> {
+ helpers::test_key_sequence_with_input_text(
+ None,
+ ("#[\n|]#", "ihello<esc>:w<ret>", "hello#[\n|]#"),
+ &|app| {
+ assert!(app.editor.is_err());
+
+ let mut docs: Vec<_> = app.editor.documents().collect();
+ assert_eq!(1, docs.len());
+
+ let doc = docs.pop().unwrap();
+ assert_eq!(None, doc.path());
+ },
+ false,
+ )
+ .await?;
+
+ Ok(())
+}
+
+#[tokio::test]
async fn test_write_new_path() -> anyhow::Result<()> {
let mut file1 = tempfile::NamedTempFile::new().unwrap();
let mut file2 = tempfile::NamedTempFile::new().unwrap();
@@ -164,24 +210,15 @@ async fn test_write_new_path() -> anyhow::Result<()> {
)
.await?;
- file1.as_file_mut().flush()?;
- file1.as_file_mut().sync_all()?;
- file2.as_file_mut().flush()?;
- file2.as_file_mut().sync_all()?;
+ helpers::assert_file_has_content(
+ file1.as_file_mut(),
+ &helpers::platform_line("i can eat glass, it will not hurt me\n"),
+ )?;
- let mut file1_content = String::new();
- file1.as_file_mut().read_to_string(&mut file1_content)?;
- assert_eq!(
- helpers::platform_line("i can eat glass, it will not hurt me\n"),
- file1_content
- );
-
- let mut file2_content = String::new();
- file2.as_file_mut().read_to_string(&mut file2_content)?;
- assert_eq!(
- helpers::platform_line("i can eat glass, it will not hurt me\n"),
- file2_content
- );
+ helpers::assert_file_has_content(
+ file2.as_file_mut(),
+ &helpers::platform_line("i can eat glass, it will not hurt me\n"),
+ )?;
Ok(())
}
diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs
index 61bea527..ff64689e 100644
--- a/helix-view/src/document.rs
+++ b/helix-view/src/document.rs
@@ -574,7 +574,12 @@ impl Document {
}
};
- let identifier = self.identifier();
+ let identifier = if self.path().is_some() {
+ Some(self.identifier())
+ } else {
+ None
+ };
+
let language_server = self.language_server.clone();
// mark changes up to now as saved
@@ -628,10 +633,13 @@ impl Document {
if !language_server.is_initialized() {
return Ok(event);
}
- if let Some(notification) =
- language_server.text_document_did_save(identifier, &text)
- {
- notification.await?;
+
+ if let Some(identifier) = identifier {
+ if let Some(notification) =
+ language_server.text_document_did_save(identifier, &text)
+ {
+ notification.await?;
+ }
}
}