From 0b967fa5d97ff1e0a1aef97c4211a626d9622ec6 Mon Sep 17 00:00:00 2001 From: Taylor Plewe Date: Sun, 18 May 2025 18:25:35 -0600 Subject: [PATCH] better handling of the closed buffer history --- helix-term/src/commands/typed.rs | 9 +++------ helix-view/src/editor.rs | 8 +++++--- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 954fcd677..d0306265f 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -330,13 +330,10 @@ fn buffer_reopen( return Ok(()); } - if let Some(last_closed_doc_path) = cx.editor.document_history - .iter() - .filter(|path| cx.editor.document_by_path(path).is_none()) - .last() - .cloned() + if let Some(last_closed_doc_path) = cx.editor.closed_document_paths.pop() { - cx.editor.open(&last_closed_doc_path, Action::Load)?; + let id = cx.editor.open(&last_closed_doc_path, Action::Load)?; + cx.editor.switch(id, Action::Load); } Ok(()) } diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 5a06d628f..dedec6a6e 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -1060,7 +1060,7 @@ pub struct Editor { pub tree: Tree, pub next_document_id: DocumentId, pub documents: BTreeMap, - pub document_history: Vec, + pub closed_document_paths: Vec, // We Flatten<> to resolve the inner DocumentSavedEventFuture. For that we need a stream of streams, hence the Once<>. // https://stackoverflow.com/a/66875668 @@ -1212,7 +1212,7 @@ impl Editor { tree: Tree::new(area), next_document_id: DocumentId::default(), documents: BTreeMap::new(), - document_history: Vec::new(), + closed_document_paths: Vec::new(), saves: HashMap::new(), save_queue: SelectAll::new(), write_count: 0, @@ -1799,7 +1799,6 @@ impl Editor { let id = self.new_document(doc); self.launch_language_servers(id); - self.document_history.push(path); helix_event::dispatch(DocumentDidOpen { editor: self, @@ -1872,6 +1871,9 @@ impl Editor { } let doc = self.documents.remove(&doc_id).unwrap(); + if let Some(path) = doc.path() { + self.closed_document_paths.push(path.clone()); + } // If the document we removed was visible in all views, we will have no more views. We don't // want to close the editor just for a simple buffer close, so we need to create a new view