Skip to content

Commit

Permalink
auto remove empty scratch buffer when selected already open buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
Kl4rry committed Jul 15, 2024
1 parent 2ac8e7d commit a02b57d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
4 changes: 4 additions & 0 deletions crates/ferrite-core/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
17 changes: 13 additions & 4 deletions crates/ferrite-core/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 3 additions & 1 deletion crates/ferrite-core/src/panes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a02b57d

Please sign in to comment.