@@ -574,6 +574,7 @@ def remove(self, children=None):
574574 def _shift_before_ord (self , reference_ord , without_children = False ):
575575 """Internal method for changing word order."""
576576 all_nodes = self ._root ._descendants
577+ empty_nodes = self ._root .empty_nodes
577578
578579 # Moving a single node can be faster than nodes_to_move = [self]
579580 if without_children or not self ._children :
@@ -584,15 +585,25 @@ def _shift_before_ord(self, reference_ord, without_children=False):
584585 all_nodes [i_ord - 1 ]._ord = i_ord
585586 all_nodes [reference_ord - 2 ] = self
586587 self ._ord = reference_ord - 1
588+ for en in empty_nodes :
589+ if en ._ord > my_ord and en ._ord < reference_ord :
590+ en ._ord -= 1
587591 elif reference_ord < my_ord :
588592 for i_ord in range (my_ord , reference_ord , - 1 ):
589593 all_nodes [i_ord - 1 ] = all_nodes [i_ord - 2 ]
590594 all_nodes [i_ord - 1 ]._ord = i_ord
591595 all_nodes [reference_ord - 1 ] = self
592596 self ._ord = reference_ord
597+ for en in empty_nodes :
598+ # Empty nodes before the first overt token (ID=0.X) will be never moved this way.
599+ # We cannot know whether the caller wanted to place the shifted node before or after them.
600+ if en ._ord < my_ord and en ._ord > reference_ord :
601+ en ._ord += 1
593602 self ._parent ._children .sort ()
594603 return
595604
605+ #TODO: Updating ords of empty nodes is implemented only for the simple case above,
606+ # but it has to be implemented also for the complex case below!
596607 nodes_to_move = self .descendants (add_self = True )
597608 first_ord , last_ord = nodes_to_move [0 ]._ord , nodes_to_move [- 1 ]._ord
598609
0 commit comments