@@ -65,12 +65,30 @@ create_tokens <- function(tokens,
65
65
# ' @family token creators
66
66
create_pos_ids <- function (pd , pos , by = 0.1 , after = FALSE , n = 1 ) {
67
67
direction <- ifelse(after , 1L , - 1L )
68
- first <- pd $ pos_id [ pos ] + by * direction
68
+ first <- find_start_pos_id( pd , pos , by , direction , after )
69
69
new_ids <- seq(first , to = first + direction * (n - 1 ) * by , by = by * direction )
70
70
validate_new_pos_ids(new_ids , after )
71
71
new_ids
72
72
}
73
73
74
+ # ' Find legit starting value for a new positional id
75
+ # '
76
+ # ' Looks at the current nest as well as into its children (if necessary) to make
77
+ # ' sure the right id is returned. Otherise, ordering of tokens might not be
78
+ # ' preserved.
79
+ # ' @inheritParams create_pos_ids
80
+ find_start_pos_id <- function (pd , pos , by , direction , after ) {
81
+ if (is.null(pd $ child [[pos ]])) {
82
+ pd $ pos_id [pos ] + by * direction
83
+ } else {
84
+
85
+ find_start_pos_id(
86
+ pd $ child [[pos ]], if_else(after , nrow(pd $ child [[pos ]]), 1L ),
87
+ by , direction , after
88
+ )
89
+ }
90
+ }
91
+
74
92
# ' Validate sequence of new position ids
75
93
# '
76
94
# ' Ids created with `after = TRUE` can have `pos_id` values between x.0 and
0 commit comments