@@ -19,7 +19,8 @@ indent_braces <- function(pd, indent_by) {
19
19
set_unindention_child(pd , token = " ')'" , unindent_by = indent_by )
20
20
}
21
21
22
- # ' @describeIn update_indention Indents operators
22
+ # ' @describeIn update_indention Indents *all* tokens after `token` - including
23
+ # ' the last token.
23
24
indent_op <- function (pd ,
24
25
indent_by ,
25
26
token = c(
@@ -36,12 +37,14 @@ indent_op <- function(pd,
36
37
}
37
38
38
39
# ' @describeIn update_indention Updates indention for token EQ_SUB. Only differs
39
- # ' from indent_op in the sense that the last token on the table where EQ_SUB
40
+ # ' from [indent_op()] in the sense that not all subsequent tokens in the parse
41
+ # ' table are necessarily indented, as `EQ_SUB` and `EQ_FORMALS` can occur
42
+ # ' multiple times in a parse table.
40
43
# ' occurs is not indented (see[compute_indent_indices()])
41
44
indent_eq_sub <- function (pd ,
42
45
indent_by ,
43
- token = " EQ_SUB" ) {
44
- eq_sub <- which(pd $ token == " EQ_SUB " )
46
+ token = c( " EQ_SUB" , " EQ_FORMALS " ) ) {
47
+ eq_sub <- which(pd $ token %in% token )
45
48
if (length(eq_sub ) == 0 ) return (pd )
46
49
has_line_break <- which(pd $ lag_newlines > 0 )
47
50
indent_indices <- intersect(eq_sub + 1 , has_line_break )
@@ -126,7 +129,9 @@ compute_indent_indices <- function(pd,
126
129
token_closing = NULL ) {
127
130
npd <- nrow(pd )
128
131
potential_triggers <- which(pd $ token %in% token_opening )
129
- needs_indention <- needs_indention(pd , potential_triggers )
132
+ needs_indention <- needs_indention(
133
+ pd , potential_triggers , other_trigger_tokens = c(" EQ_SUB" , " EQ_FORMALS" )
134
+ )
130
135
trigger <- potential_triggers [needs_indention ][1 ]
131
136
if (is.na(trigger )) return (numeric (0 ))
132
137
start <- trigger + 1
@@ -147,25 +152,52 @@ compute_indent_indices <- function(pd,
147
152
# ' @param potential_triggers A vector with indices of the potential trigger
148
153
# ' tokens in `pd`.
149
154
# ' @inheritParams needs_indention_one
150
- needs_indention <- function (pd , potential_triggers ) {
151
- map_lgl(potential_triggers , needs_indention_one , pd = pd )
155
+ needs_indention <- function (pd ,
156
+ potential_triggers_pos ,
157
+ other_trigger_tokens = NULL ) {
158
+ map_lgl(potential_triggers_pos , needs_indention_one ,
159
+ pd = pd , other_trigger_tokens = other_trigger_tokens
160
+ )
152
161
}
153
162
154
163
155
164
# ' Check whether indention is needed
156
165
# '
157
- # ' Indention is needed if and only if there is no multi-line token between the
158
- # ' trigger and the first line break.
166
+ # ' Indention is needed if the two conditions apply:
167
+ # '
168
+ # ' * there is no multi-line token between the trigger and the first line break.
169
+ # ' * there is no other token between the potential trigger and the first line
170
+ # ' break that is going to cause indention.
171
+ # '
159
172
# ' @param pd A parse table.
160
173
# ' @param potential_trigger the index of the token in the parse table
161
174
# ' for which it should be checked whether it should trigger indention.
162
175
# ' @return Returns `TRUE` if indention is needed, `FALSE` otherwise.
176
+ # ' @param other_trigger_tokens Other tokens that are going to cause indention
177
+ # ' if on the same line as the token corresponding to `potential_trigger`.
163
178
# ' @return `TRUE` if indention is needed, `FALSE` otherwise.
164
179
# ' @importFrom rlang seq2
165
- needs_indention_one <- function (pd , potential_trigger ) {
180
+ needs_indention_one <- function (pd ,
181
+ potential_trigger_pos ,
182
+ other_trigger_tokens ) {
166
183
before_first_break <- which(pd $ lag_newlines > 0 )[1 ] - 1
167
184
if (is.na(before_first_break )) return (FALSE )
168
- ! any(pd $ multi_line [seq2(potential_trigger , before_first_break )])
185
+ row_idx_between_trigger_and_line_break <- seq2(
186
+ potential_trigger_pos , before_first_break
187
+ )
188
+ multi_line_token <- pd_is_multi_line(
189
+ pd [row_idx_between_trigger_and_line_break ,]
190
+ )
191
+ remaining_row_idx_between_trigger_and_line_break <- setdiff(
192
+ row_idx_between_trigger_and_line_break ,
193
+ potential_trigger_pos
194
+ )
195
+
196
+ other_trigger_on_same_line <-
197
+ pd [remaining_row_idx_between_trigger_and_line_break ,]$ token %in%
198
+ other_trigger_tokens
199
+
200
+ ! any(multi_line_token ) & ! any(other_trigger_on_same_line )
169
201
}
170
202
171
203
0 commit comments