From d8fbff38562c770b435e6229c15e78dd635fd4c9 Mon Sep 17 00:00:00 2001 From: Termina94 Date: Tue, 12 Jul 2022 21:56:52 +0100 Subject: [PATCH] refactor buffer resize to single function --- helix-view/src/editor.rs | 10 +++--- helix-view/src/tree.rs | 75 +++++++++++++++++++++++----------------- 2 files changed, 49 insertions(+), 36 deletions(-) diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index e50069fdd..900b330b5 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -6,7 +6,7 @@ use crate::{ input::KeyEvent, register::Registers, theme::{self, Theme}, - tree::{self, Tree}, + tree::{self, Dimension, Resize, Tree}, view::ViewPosition, Align, Document, DocumentId, View, ViewId, }; @@ -1646,19 +1646,19 @@ impl Editor { } pub fn grow_buffer_width(&mut self) { - self.tree.grow_buffer_width(); + self.tree.resize_buffer(Resize::Grow, Dimension::Width); } pub fn shrink_buffer_width(&mut self) { - self.tree.shrink_buffer_width(); + self.tree.resize_buffer(Resize::Shrink, Dimension::Width); } pub fn grow_buffer_height(&mut self) { - self.tree.grow_buffer_height(); + self.tree.resize_buffer(Resize::Grow, Dimension::Height); } pub fn shrink_buffer_height(&mut self) { - self.tree.shrink_buffer_height(); + self.tree.resize_buffer(Resize::Shrink, Dimension::Height); } pub fn buffer_focus_mode(&mut self) { diff --git a/helix-view/src/tree.rs b/helix-view/src/tree.rs index 7c687dafe..aa7de6a5d 100644 --- a/helix-view/src/tree.rs +++ b/helix-view/src/tree.rs @@ -29,6 +29,16 @@ pub enum Content { Container(Box), } +pub enum Resize { + Shrink, + Grow, +} + +pub enum Dimension { + Width, + Height, +} + impl Node { pub fn container(layout: Layout) -> Self { Self { @@ -687,38 +697,41 @@ impl Tree { None } - pub fn grow_buffer_width(&mut self) { - if let Some(bounds) = self.get_active_node_bounds_mut(Layout::Vertical) { - if bounds.width < 20 { - bounds.width += 1; - self.recalculate(); - } - } - } - - pub fn shrink_buffer_width(&mut self) { - if let Some(bounds) = self.get_active_node_bounds_mut(Layout::Vertical) { - if bounds.width > 1 { - bounds.width -= 1; - self.recalculate(); - } - } - } - - pub fn grow_buffer_height(&mut self) { - if let Some(bounds) = self.get_active_node_bounds_mut(Layout::Horizontal) { - if bounds.height < 20 { - bounds.height += 1; - self.recalculate(); + pub fn resize_buffer(&mut self, resize_type: Resize, dimension: Dimension) { + match dimension { + Dimension::Width => { + if let Some(bounds) = self.get_active_node_bounds_mut(Layout::Vertical) { + match resize_type { + Resize::Shrink => { + if bounds.width > 1 { + bounds.width -= 1; + } + } + Resize::Grow => { + if bounds.width < 20 { + bounds.width += 1; + } + } + }; + self.recalculate(); + } } - } - } - - pub fn shrink_buffer_height(&mut self) { - if let Some(bounds) = self.get_active_node_bounds_mut(Layout::Horizontal) { - if bounds.height > 1 { - bounds.height -= 1; - self.recalculate(); + Dimension::Height => { + if let Some(bounds) = self.get_active_node_bounds_mut(Layout::Horizontal) { + match resize_type { + Resize::Shrink => { + if bounds.height > 1 { + bounds.height -= 1; + } + } + Resize::Grow => { + if bounds.height < 20 { + bounds.height += 1; + } + } + }; + self.recalculate(); + } } } }