-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fixing missing case for handling root node for splitting #3983
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Fixing missing case for handling root node for splitting #3983
Conversation
|
When this is the initial state... ...and this the state after the first unsplit (back to just one pane)... ...shouldn't we flat this state into the initial state already to return to the initial state with this one and last split? So that any further unsplit doesn't do anything, even not in the background. Besides of that the rearrangement of |
I did think about this while creating the PR fix. The thing is I think this behavior has always been a valid state for the root node, whether it is before or after my One of the trick point is that the I probably could change the logic of I vaguely remember the first iteration of So in short, in theory yes I agree with you. In practice I will probably need to spend more time to recover my memory on how it works and see if I can modify it without making it recursive. And also need to test it as well. I can take a look at it at some point but can't promise anything for now. If you want to have a try, go for it :) |
c8a121f to
a87f3dd
Compare
func (n *Node) flatten() {
if len(n.children) != 1 {
return
}
if n.parent == nil {
n.Kind = STUndef
n.children = n.children[:0]
return
}
[...]Not 100% sure, if this covers all and everything, but looks good from behavior and node printing. |
| return 0 | ||
| } | ||
| if n.Kind == STUndef { | ||
| if n.parent == nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The next commit seems to make this change unneeded? I mean, if we guarantee that the root is always STUndef, why not use it? Otherwise, if we don't use STUndef, why do we even need it?
And IMHO using STUndef makes the code a bit more readable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor thing but I would think n.parent == nil is more straightforward no? Since that is the definition of root node.
A child node can be of type STUndef (Although it is a bug), but a child node definite cannot have a node parent of nil right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then, again, why need this STUndef constant at all?
Although, after more thought, - ok, let's keep it (even though it is causing a bit of redundancy), just to let it reflect that it is indeed undefined whether it is horizontal or vertical.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then, again, why need this
STUndefconstant at all?
Nah, we don't need it. Didn't do it cuz I only did minimal change just to fix the issue.
It doesn't matter if it is horizontal or not if it gets changed anyway. If we agree to remove it, I can do it :)
|
Just tried testing this PR... Apparently it is fixing one crash but introducing another one, which is even easier to reproduce: for example:
Result: |
0a07c9e to
c554fa7
Compare
| *n = *n.children[0] | ||
| n.parent = nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yes...right. We overwrite the root node with the last remaining children, by which it receives the id of the children, with which it is identified in the tab resize.
But still we should keep the removed n.Kind = STUndef, otherwise func (n *Node) String() wouldn't print what we expect from the new root. Otherwise in case we agree to remove STUndef we've to adjust this function to print "/" on n.parent == nil.
Am I right that we should keep the fault in tab.go#L382 to identify that something went wrong, because GetNode() returns nil by intention in case the pane ID wasn't found?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise in case we agree to remove
STUndefwe've to adjust this function to print "/" onn.parent == nil.
True. Forgot about that change when I made this change. Will add it back if we want to leave STUndef as it is.
Am I right that we should keep the fault in tab.go#L382 to identify that something went wrong, because
GetNode()returnsnilby intention in case the pane ID wasn't found?
Not sure what fault you are referring to. t.GetNode(p.ID()) should never fail no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what fault you are referring to.
t.GetNode(p.ID())should never fail no?
But it can splits.go#L140 and this is intended, when the given ID isn't present at the related nodes.
Will add it back if we want to leave STUndef as it is.
I vote for keeping it too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if I am missing something but the given id should always be present for the node unless something is wrong no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I vote for keeping it too.
I vote for removing it :) since it's sole purpose is to identify root node and that can be done by just checking if parent exists or not.
Just need @dmaluka 's vote or anyone else interested.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I vote for keeping it, so that we don't need to meaninglessly pass STVert or STHoriz to NewNode() when creating the root node.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay got it. Added back the n.Kind = STUndef
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't like this n.Kind = STUndef statement, right? 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JoeKar
Forgot to explain it why I removed n.Kind = STUndef after adding it 😅
I initlially kept it when adding
for _, c := range n.children {
c.parent = n
}So like
if n.parent == nil {
*n = *n.children[0]
n.parent = nil
n.Kind = STUndef
for _, c := range n.children {
c.parent = n
}
return
}for fixing the crash @dmaluka observed.
Then when I was testing it, it failed to resize the last split correctly when unsplitting the splits one by one, where the last split only took half of the screen instead of taking the whole screen.
After some debugging, I realized the kind for the root node cannot be undef when we remove the intermediate one since it can have more than one grandchildren.
Like so:
2026/02/03 18:46:40 Node Tree:
-{0 0 211 46} 1 0
|{0 0 105 46} 1 1🍁
|{105 0 106 46} 2 1
-{105 0 106 15} 2 2🍁
-{105 15 106 15} 3 2🍁
-{105 30 106 16} 4 2🍁
Becomes this if I removed leaf node ID 1:
(forget about the coords and parent id. This is just for demo purposes, I cba to rebuild micro to get the logs)
2026/02/03 18:46:40 Node Tree:
-{0 0 211 46} 1 0
-{105 0 106 15} 2 2🍁
-{105 15 106 15} 3 2🍁
-{105 30 106 16} 4 2🍁
Looking at the tree before being flattened.
If I unsplit leaf node ID 1, the unsplit will flatten the intermediate non leaf node ID 2 with grandparent leaf nodes 2, 3 and 4. If the kind for the root node is Undef, the resize won't work correctly.
c554fa7 to
3789835
Compare
|
Another crash, observed with the newest version of this PR:
Result: |
3789835 to
49fd03c
Compare
Nice one. Thanks for finding out all the crashes, appreciated. Hopefully, it is good this time. |
| marker := "|" | ||
| marker := "/" | ||
| if n.Kind == STHoriz { | ||
| marker = "-" | ||
| } else if n.Kind == STVert { | ||
| marker = "|" | ||
| } | ||
| var parentId uint64 = 0 | ||
| if n.parent != nil { | ||
| parentId = n.parent.id | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should slightly rework this:
var marker string
var parentId uint64 = 0
if n.parent == nil {
marker = "/"
} else {
if n.Kind == STHoriz {
marker = "-"
} else if n.Kind == STVert {
marker = "|"
}
parentId = n.parent.id
}What do you think?
At the beginning, the node tree looks like this, where x =
STUndefWhen performing VSplit or HSplit at the beginning, we check if we are root node using
STUndef, in which we will always put the new split as its children, then we set the node kind for the root node. Which then looks like this:Since we have set root node kind after the initial split, the special handling for the root node using
STUndefwill no longer be valid.When using quit instead of unsplit, you can only get to the following node tree, and doing quit on the child will quit micro entirely.
However, if you are using unsplit, you can actually go back to the original state, like this:
One pane left:
After unsplit:
The fix is simply check if the parent is
nilto determine if we are the root node or not.I have also rearrange
VSplitlast if condition to matchHSplitwhere we are treating current node as non-leaf node and add the new split to it's children.Also updated the
String()to show the parent node id for easier debugging.Fixes #3980