@@ -91,15 +91,14 @@ apply_ref_indention <- function(flattened_pd) {
91
91
# ' @param target_token The index of the token from which the indention level
92
92
# ' should be applied to other tokens.
93
93
apply_ref_indention_one <- function (flattened_pd , target_token ) {
94
- token_points_to_ref <-
95
- flattened_pd $ indention_ref_pos_id == flattened_pd $ pos_id [target_token ]
96
- first_token_on_line <- flattened_pd $ lag_newlines > 0L
97
- token_to_update <- which(token_points_to_ref & first_token_on_line )
98
94
95
+ token_to_update <- find_tokens_to_update(flattened_pd , target_token )
99
96
# udate spaces
100
97
copied_spaces <- flattened_pd $ col2 [target_token ]
101
98
old_spaces <- flattened_pd $ lag_spaces [token_to_update [1 ]]
102
- shift <- copied_spaces - old_spaces
99
+ if (length(token_to_update ) < 1 ) return (flattened_pd )
100
+ zero_ref <- min(flattened_pd $ lag_spaces [token_to_update ])
101
+ shift <- copied_spaces - zero_ref
103
102
flattened_pd $ lag_spaces [token_to_update ] <-
104
103
flattened_pd $ lag_spaces [token_to_update ] + shift
105
104
@@ -110,6 +109,34 @@ apply_ref_indention_one <- function(flattened_pd, target_token) {
110
109
flattened_pd
111
110
}
112
111
112
+ # ' Find the tokens to update when applying a reference indention
113
+ # '
114
+ # ' Given a target token and a flattened parse table, the token for which the
115
+ # ' spacing information needs to be updated are computed. Since indention is
116
+ # ' already embeded in the column `lag_spaces`, only tokens at the beginning of
117
+ # ' a line are of concern.
118
+ # ' This function is currently tailored targeted at
119
+ # ' re-indention of function delcaration.
120
+ # ' @param flattened_pd A flattened parse table.
121
+ # ' @param token_to_update An integer vector with positions of tokens to update.
122
+ # ' @seealso apply_ref_indention_one()
123
+ # ' @examples
124
+ # ' style_text("function(a =
125
+ # ' b,
126
+ # ' dd
127
+ # ' ) {}", scope = "indention")
128
+ # ' style_text("function(a,
129
+ # ' b,
130
+ # ' dd
131
+ # ' ) {}", scope = "indention")
132
+ find_tokens_to_update <- function (flattened_pd , target_token ) {
133
+ token_points_to_ref <-
134
+ flattened_pd $ indention_ref_pos_id == flattened_pd $ pos_id [target_token ]
135
+ first_token_on_line <- flattened_pd $ lag_newlines > 0L
136
+ which(token_points_to_ref & first_token_on_line )
137
+ }
138
+
139
+
113
140
# ' Set indention of tokens that match regex
114
141
# '
115
142
# ' Force the level of indention of tokens whose text matches a regular
0 commit comments