Skip to content

Commit

Permalink
fix: avoid child area overflow on split (helix-editor#10620)
Browse files Browse the repository at this point in the history
  • Loading branch information
l4l authored and Vulpesx committed Jun 7, 2024
1 parent 7e8ce9d commit 959f61a
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions helix-view/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,11 +407,13 @@ impl Tree {
}
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;

Expand Down Expand Up @@ -925,13 +927,43 @@ mod test {
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::<Vec<_>>()
);
}

#[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::<Vec<_>>(),
tree.views()
.map(|(view, _)| view.area.width)
.collect::<Vec<_>>()
);
}
}

0 comments on commit 959f61a

Please sign in to comment.