on master help sign in
fix write scratch buffer to file
commitaf03df3413f04ce7079d14388ce42fe70bd1397e authorSkyler Hawthorne <skyler@dead10ck.com> dateWed 31 Aug 2022, 15:08 (4 years ago) parent18c3211
3 files changed+67−26
helix-term/src/commands/typed.rs−4 helix-term/tests/test/write.rs+54−17 helix-view/src/document.rs+13−5
helix-term/src/commands/typed.rs−4
@@ -273,10 +273,6 @@ fn write_impl(
273273 let doc = doc_mut!(cx.editor);
274274 let path = path.map(AsRef::as_ref);
275275
276− if doc.path().is_none() {
277− bail!("cannot write a buffer without a filename");
278− }
279−
280276 let fmt = if editor_auto_fmt {
281277 doc.auto_format().map(|fmt| {
282278 let callback = make_format_callback(
helix-term/tests/test/write.rs+54−17
@@ -128,6 +128,52 @@ async fn test_write_fail_mod_flag() -> anyhow::Result<()> {
128128 Ok(())
129129}
130130
131+#[tokio::test]
132+async fn test_write_scratch_to_new_path() -> anyhow::Result<()> {
133+ let mut file = tempfile::NamedTempFile::new()?;
134+
135+ test_key_sequence(
136+ &mut Application::new(Args::default(), Config::default())?,
137+ Some(format!("ihello<esc>:w {}<ret>", file.path().to_string_lossy()).as_ref()),
138+ Some(&|app| {
139+ assert!(!app.editor.is_err());
140+
141+ let mut docs: Vec<_> = app.editor.documents().collect();
142+ assert_eq!(1, docs.len());
143+
144+ let doc = docs.pop().unwrap();
145+ assert_eq!(Some(&file.path().to_path_buf()), doc.path());
146+ }),
147+ false,
148+ )
149+ .await?;
150+
151+ helpers::assert_file_has_content(file.as_file_mut(), &helpers::platform_line("hello"))?;
152+
153+ Ok(())
154+}
155+
156+#[tokio::test]
157+async fn test_write_scratch_no_path_fails() -> anyhow::Result<()> {
158+ helpers::test_key_sequence_with_input_text(
159+ None,
160+ ("#[\n|]#", "ihello<esc>:w<ret>", "hello#[\n|]#"),
161+ &|app| {
162+ assert!(app.editor.is_err());
163+
164+ let mut docs: Vec<_> = app.editor.documents().collect();
165+ assert_eq!(1, docs.len());
166+
167+ let doc = docs.pop().unwrap();
168+ assert_eq!(None, doc.path());
169+ },
170+ false,
171+ )
172+ .await?;
173+
174+ Ok(())
175+}
176+
131177#[tokio::test]
132178async fn test_write_new_path() -> anyhow::Result<()> {
133179 let mut file1 = tempfile::NamedTempFile::new().unwrap();
@@ -164,24 +210,15 @@ async fn test_write_new_path() -> anyhow::Result<()> {
164210 )
165211 .await?;
166212
167− file1.as_file_mut().flush()?;
168− file1.as_file_mut().sync_all()?;
169− file2.as_file_mut().flush()?;
170− file2.as_file_mut().sync_all()?;
213+ helpers::assert_file_has_content(
214+ file1.as_file_mut(),
215+ &helpers::platform_line("i can eat glass, it will not hurt me\n"),
216+ )?;
171217
172− let mut file1_content = String::new();
173− file1.as_file_mut().read_to_string(&mut file1_content)?;
174− assert_eq!(
175− helpers::platform_line("i can eat glass, it will not hurt me\n"),
176− file1_content
177− );
178−
179− let mut file2_content = String::new();
180− file2.as_file_mut().read_to_string(&mut file2_content)?;
181− assert_eq!(
182− helpers::platform_line("i can eat glass, it will not hurt me\n"),
183− file2_content
184− );
218+ helpers::assert_file_has_content(
219+ file2.as_file_mut(),
220+ &helpers::platform_line("i can eat glass, it will not hurt me\n"),
221+ )?;
185222
186223 Ok(())
187224}
helix-view/src/document.rs+13−5
@@ -574,7 +574,12 @@ impl Document {
574574 }
575575 };
576576
577− let identifier = self.identifier();
577+ let identifier = if self.path().is_some() {
578+ Some(self.identifier())
579+ } else {
580+ None
581+ };
582+
578583 let language_server = self.language_server.clone();
579584
580585 // mark changes up to now as saved
@@ -628,10 +633,13 @@ impl Document {
628633 if !language_server.is_initialized() {
629634 return Ok(event);
630635 }
631− if let Some(notification) =
632− language_server.text_document_did_save(identifier, &text)
633− {
634− notification.await?;
636+
637+ if let Some(identifier) = identifier {
638+ if let Some(notification) =
639+ language_server.text_document_did_save(identifier, &text)
640+ {
641+ notification.await?;
642+ }
635643 }
636644 }
637645