You give a correct implementation of vnode2allPositions().
Based on Brian's code, here is the code I suggest for replacing leoCommands.vnode2allPositions():
The implementation of vnode2allPositions() in leoCommand.py is seriously flawed. Correcting it would
probably change it extensively.
You can see the problem by considering the case of one parent node A with one position X, one clone node B with with positions Y and Z that are immediate children of X. Node B.parents has two members both of which are A. Hence leoCommands. vnode2allPositions() returns 2 positions because the outermost loop is on the members of B.parents. But, both positions returned position Y because A.children.index(B) is used to determine the index for each position and the index(self, z) is always the first occurrence of z in the list self.
My best
guess is that the implementation in leoCommands.py is complicated by
trying to be efficient. But vnode2allPositions() is not used by
Leo-Editor core, so there is probably no need for it to be particularly
efficient.
Brian,
So far all Leo-Editor UNL's have been presented in left-to-right (root to target position) order. Like you I have written code that presents the UNL is right-to-left (target position to root) order because usually the target position is of much more interest than the root and the right-to-left order always places the the target position at the same column (the left side of the screen).
Here is a function that guarantees positions Y and Z display as different UNL's by appending/prepending the child index for Y or Z (child index in brackets) to the headline of X.
```
def unlpc(posX, r2l=False):
""" Append/Prepend the child index to the parent of the last node
in the UNL.
Arguments:
posX: Target position
r2l: Right to Left
False --> UNL in root to posX direction.
True --> UNL in posX to root direction.
Returns:
unlpc: A UNL with the child index of the last node
in the UNL appended in brackets to the parent's headline.
If the parent is the hidden root node, then
the bracketed child index is appended to the
empty string.
Suppose a cloned-node has several positions that are
immediate children of one node. This function produces
unique UNL's for each of these positions.
This function can produce equal UNL's for several cloned-node
positions if several nodes have the same headline.
"""
unl = posX.get_UNL(with_file=False, with_index=False)
unlList = unl.split("-->")
uLen = len(unlList)
if uLen == 1:
parent = ''
uBefore = list()
else:
parent = unlList[-2]
uBefore = unlList[:-2]
if r2l:
unlList = uBefore + [f'[{posX.childIndex()}]' + parent, unlList[-1]]
unlList.reverse()
arrow = '<--'
else:
unlList = uBefore + [parent + f'[{posX.childIndex()}]', unlList[-1]]
arrow = '-->'
return arrow.join(unlList)
```
I hope this helps,
SegundoBob