diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 2013a9d81..bc4ac389d 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -321,6 +321,21 @@ fn buffer_previous( Ok(()) } +fn buffer_reopen( + cx: &mut compositor::Context, + _args: Args, + event: PromptEvent, +) -> anyhow::Result<()> { + if event != PromptEvent::Validate { + return Ok(()); + } + + if let Some(last_closed_doc_path) = cx.editor.closed_document_paths.pop() { + cx.editor.open(&last_closed_doc_path, Action::Replace)?; + } + Ok(()) +} + fn write_impl(cx: &mut compositor::Context, path: Option<&str>, force: bool) -> anyhow::Result<()> { let config = cx.editor.config(); let jobs = &mut cx.jobs; @@ -2703,6 +2718,17 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ ..Signature::DEFAULT }, }, + TypableCommand { + name: "buffer-reopen", + aliases: &["br", "breopen"], + doc: "Re-open the most previously closed buffer.", + fun: buffer_reopen, + completer: CommandCompleter::none(), + signature: Signature { + positionals: (0, Some(0)), + ..Signature::DEFAULT + }, + }, TypableCommand { name: "write", aliases: &["w"], diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 89f053741..0cc3bb79a 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -1067,7 +1067,8 @@ pub struct Editor { pub tree: Tree, pub next_document_id: DocumentId, pub documents: BTreeMap, - + 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 pub saves: HashMap>>, @@ -1218,6 +1219,7 @@ impl Editor { tree: Tree::new(area), next_document_id: DocumentId::default(), documents: BTreeMap::new(), + closed_document_paths: Vec::new(), saves: HashMap::new(), save_queue: SelectAll::new(), write_count: 0, @@ -1880,6 +1882,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