From 8db93013fb33f456b241e8146bc9d85333de6878 Mon Sep 17 00:00:00 2001 From: Kitsu Date: Mon, 29 Apr 2024 10:18:05 -0300 Subject: [PATCH] fix: avoid child area overflow on split (#10620) --- helix-view/src/tree.rs | 44 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/helix-view/src/tree.rs b/helix-view/src/tree.rs index b7ad5aa9d..307dbc71d 100644 --- a/helix-view/src/tree.rs +++ b/helix-view/src/tree.rs @@ -407,11 +407,13 @@ pub fn recalculate(&mut self) { } Layout::Vertical => { let len = container.children.len(); - - let width = area.width / len as u16; + let len_u16 = len as u16; let inner_gap = 1u16; - // let total_gap = inner_gap * (len as u16 - 1); + let total_gap = inner_gap * len_u16.saturating_sub(2); + + let used_area = area.width.saturating_sub(total_gap); + let width = used_area / len_u16; let mut child_x = area.x; @@ -925,13 +927,43 @@ fn all_vertical_views_have_same_width() { assert_eq!(3, tree.views().count()); assert_eq!( vec![ - tree_area_width / 3, - tree_area_width / 3, - tree_area_width / 3 - 2 // Rounding in `recalculate`. + tree_area_width / 3 - 1, // gap here + tree_area_width / 3 - 1, // gap here + tree_area_width / 3 ], tree.views() .map(|(view, _)| view.area.width) .collect::>() ); } + + #[test] + fn vsplit_gap_rounding() { + let (tree_area_width, tree_area_height) = (80, 24); + let mut tree = Tree::new(Rect { + x: 0, + y: 0, + width: tree_area_width, + height: tree_area_height, + }); + let mut view = View::new(DocumentId::default(), GutterConfig::default()); + view.area = Rect::new(0, 0, tree_area_width, tree_area_height); + tree.insert(view); + + for _ in 0..9 { + let view = View::new(DocumentId::default(), GutterConfig::default()); + tree.split(view, Layout::Vertical); + } + + assert_eq!(10, tree.views().count()); + assert_eq!( + std::iter::repeat(7) + .take(9) + .chain(Some(8)) // Rounding in `recalculate`. + .collect::>(), + tree.views() + .map(|(view, _)| view.area.width) + .collect::>() + ); + } }