Skip to content

Commit 4cb5201

Browse files
Fixing chained parents by simplifying the tree on unsplit
1 parent f4d62a4 commit 4cb5201

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

internal/views/splits.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,9 +479,31 @@ 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 chained parents
488+
func (n *Node) Simplify() {
489+
if n.parent == nil {
490+
return
491+
}
492+
493+
ind := 0
494+
if len(n.children) == 1 {
495+
for i, c := range n.parent.children {
496+
if c.id == n.id {
497+
ind = i
498+
}
499+
}
500+
}
501+
502+
parent := n.parent
503+
n.parent.children[ind] = n.children[0]
504+
parent.Simplify()
505+
}
506+
485507
// String returns the string form of the node and all children (used for debugging)
486508
func (n *Node) String() string {
487509
var strf func(n *Node, ident int) string

0 commit comments

Comments
 (0)