Skip to content

Commit 1695f6d

Browse files
Fixing chained parents by simplifying the tree on unsplit
1 parent f4d62a4 commit 1695f6d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

internal/views/splits.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,9 +479,35 @@ func (n *Node) Unsplit() bool {
479479
if n.parent.IsLeaf() {
480480
return n.parent.Unsplit()
481481
}
482+
483+
n.parent.simplify()
482484
return true
483485
}
484486

487+
// simplify removes unnecessary intermediate parents that have only one child
488+
func (n *Node) simplify() {
489+
if n.parent == nil || len(n.children) != 1 {
490+
return
491+
}
492+
493+
ind := 0
494+
for i, c := range n.parent.children {
495+
if c.id == n.id {
496+
ind = i
497+
}
498+
}
499+
500+
n.parent.children[ind] = n.children[0]
501+
n.parent.children[ind].parent = n.parent
502+
if n.children[0].IsLeaf() {
503+
n.parent.children[ind].Kind = n.Kind
504+
}
505+
506+
// Since the node changes its parent, it also changes its siblings,
507+
// so propW and propH need to be updated.
508+
n.parent.markSizes()
509+
}
510+
485511
// String returns the string form of the node and all children (used for debugging)
486512
func (n *Node) String() string {
487513
var strf func(n *Node, ident int) string

0 commit comments

Comments
 (0)