From a02b57d899b5b29dbec1203c256ef8e1f8f459b7 Mon Sep 17 00:00:00 2001 From: Axel Kappel <69117984+Kl4rry@users.noreply.github.com> Date: Mon, 15 Jul 2024 22:49:37 +0200 Subject: [PATCH] auto remove empty scratch buffer when selected already open buffer --- crates/ferrite-core/src/buffer.rs | 4 ++++ crates/ferrite-core/src/engine.rs | 17 +++++++++++++---- crates/ferrite-core/src/panes.rs | 4 +++- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/crates/ferrite-core/src/buffer.rs b/crates/ferrite-core/src/buffer.rs index 832a253..e1500db 100644 --- a/crates/ferrite-core/src/buffer.rs +++ b/crates/ferrite-core/src/buffer.rs @@ -1843,6 +1843,10 @@ impl Buffer { self.history.finish(); } } + + pub fn is_disposable(&self) -> bool { + !self.is_dirty() && self.rope().len_bytes() == 0 && self.file.is_none() + } } pub struct ViewLine<'a> { diff --git a/crates/ferrite-core/src/engine.rs b/crates/ferrite-core/src/engine.rs index 48a1e90..8393b09 100644 --- a/crates/ferrite-core/src/engine.rs +++ b/crates/ferrite-core/src/engine.rs @@ -377,9 +377,16 @@ impl Engine { let _ = finder.handle_input(input); if let Some(choice) = finder.get_choice() { self.buffer_finder = None; - self.workspace + let old = self + .workspace .panes .replace_current(PaneKind::Buffer(choice.id)); + if let PaneKind::Buffer(id) = old { + let buffer = &self.workspace.buffers[id]; + if buffer.is_disposable() { + self.workspace.buffers.remove(id); + } + } } } else { match self.workspace.panes.get_current_pane() { @@ -646,7 +653,7 @@ impl Engine { Command::ForceQuit => *control_flow = EventLoopControlFlow::Exit, Command::Logger => { self.logger_state.lines_scrolled_up = 0; - self.workspace.panes.replace_current(PaneKind::Logger) + self.workspace.panes.replace_current(PaneKind::Logger); } Command::Theme(name) => match name { Some(name) => { @@ -827,12 +834,14 @@ impl Engine { .as_deref() == Some(&real_path) }) { - Some((id, _)) => self.workspace.panes.replace_current(PaneKind::Buffer(id)), + Some((id, _)) => { + self.workspace.panes.replace_current(PaneKind::Buffer(id)); + } None => match Buffer::from_file(path) { Ok(buffer) => { if let PaneKind::Buffer(buffer_id) = self.workspace.panes.get_current_pane() { let current_buf = self.workspace.buffers.get_mut(buffer_id).unwrap(); - if !current_buf.is_dirty() && current_buf.rope().len_bytes() == 0 { + if current_buf.is_disposable() { *current_buf = buffer; return; } diff --git a/crates/ferrite-core/src/panes.rs b/crates/ferrite-core/src/panes.rs index fc8c277..47d0003 100644 --- a/crates/ferrite-core/src/panes.rs +++ b/crates/ferrite-core/src/panes.rs @@ -332,13 +332,15 @@ impl Panes { self.current_pane } - pub fn replace_current(&mut self, pane: PaneKind) { + pub fn replace_current(&mut self, pane: PaneKind) -> PaneKind { if self.node.contains(pane) { self.node.remove(pane); } self.node.replace(self.current_pane, pane); + let old = self.current_pane; self.current_pane = pane; + old } pub fn remove_pane(&mut self, pane: PaneKind) -> bool {