From 587ad62c1d33bc557b810425949de5fce875b7c9 Mon Sep 17 00:00:00 2001 From: Shafkath Shuhan Date: Mon, 30 Jan 2023 17:26:01 -0500 Subject: [PATCH] close buffer after checking if the saved state is valid --- helix-core/src/path.rs | 2 +- helix-term/src/commands/typed.rs | 22 +++++++++++----------- helix-term/tests/test/commands.rs | 5 +---- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/helix-core/src/path.rs b/helix-core/src/path.rs index b3d6519036991..471d0607df103 100644 --- a/helix-core/src/path.rs +++ b/helix-core/src/path.rs @@ -157,7 +157,7 @@ pub fn path_as_bytes>(path: P) -> Vec { pub fn path_from_bytes(slice: &[u8]) -> Result { #[cfg(windows)] - return Ok(PathBuf::from(std::str::from_utf8(slice))); + return Ok(PathBuf::from(std::str::from_utf8(slice)?)); #[cfg(unix)] return Ok(PathBuf::from( diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index f5ae97fbe2507..85375c00dc5e5 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -1915,27 +1915,18 @@ fn open_workspace( log::debug!("Loaded undo index: {:?}", index); // Open the documents in the index and load their histories. - for (id, path) in index { + for (index_id, path) in index { if !path.exists() { continue; } // Close open buffers for the doc. - let doc_id = cx - .editor - .documents() - .find_map(|doc| (doc.path() == Some(&path)).then(|| doc.id())); - if let Some(id) = doc_id { - buffer_close_by_ids_impl(cx, &[id], false)?; - } - let mut file = workspace.get(&id.to_string())?; + let mut file = workspace.get(&index_id.to_string())?; let last_mtime = std::fs::metadata(path.clone())? .modified()? .duration_since(std::time::UNIX_EPOCH)? .as_secs(); - let id = cx.editor.open(path.as_path(), Action::Load)?; - let doc = doc_mut!(cx.editor, &id); let (last_saved_revision, history) = match helix_core::history::History::deserialize( &mut file, &mut std::fs::File::open(&path)?, @@ -1951,6 +1942,15 @@ fn open_workspace( } }; + let doc_id = cx + .editor + .documents() + .find_map(|doc| (doc.path() == Some(&path)).then(|| doc.id())); + if let Some(id) = doc_id { + buffer_close_by_ids_impl(cx, &[id], false)?; + } + let id = cx.editor.open(path.as_path(), Action::Load)?; + let doc = doc_mut!(cx.editor, &id); // Jump to saved revision if the doc wasn't saved. if history.current_revision() != last_saved_revision { let view_id = doc diff --git a/helix-term/tests/test/commands.rs b/helix-term/tests/test/commands.rs index 02b731e88b258..62dfe465e3749 100644 --- a/helix-term/tests/test/commands.rs +++ b/helix-term/tests/test/commands.rs @@ -367,10 +367,7 @@ async fn test_workspace_serde() -> anyhow::Result<()> { Some("ihello:sw:bc!:ow"), Some(&|app| { let mut docs: Vec<_> = app.editor.documents().collect(); - assert_eq!(1, docs.len()); - - let doc = docs.pop().unwrap(); - assert_eq!(Some(file.path()), doc.path().map(PathBuf::as_path)); + assert_eq!(2, docs.len()); }), false, )