@@ -19,7 +19,8 @@ indent_braces <- function(pd, indent_by) {
1919 set_unindention_child(pd , token = " ')'" , unindent_by = indent_by )
2020}
2121
22- # ' @describeIn update_indention Indents operators
22+ # ' @describeIn update_indention Indents *all* tokens after `token` - including
23+ # ' the last token.
2324indent_op <- function (pd ,
2425 indent_by ,
2526 token = c(
@@ -35,13 +36,29 @@ indent_op <- function(pd,
3536 pd
3637}
3738
39+ # ' Revert the indention of function declaration header
40+ # '
41+ # ' Necessary for consistent indention of the function declaration header.
42+ # ' @param pd A parse table.
43+ # ' @seealso set_unindention_child update_indention_ref_fun_dec
44+ unindent_fun_dec <- function (pd ) {
45+ if (is_function_dec(pd )) {
46+ idx_closing_brace <- which(pd $ token %in% " ')'" )
47+ fun_dec_head <- seq2(2L , idx_closing_brace )
48+ pd $ indent [fun_dec_head ] <- 0L
49+ }
50+ pd
51+ }
52+
3853# ' @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
54+ # ' from [indent_op()] in the sense that not all subsequent tokens in the parse
55+ # ' table are necessarily indented, as `EQ_SUB` and `EQ_FORMALS` can occur
56+ # ' multiple times in a parse table.
4057# ' occurs is not indented (see[compute_indent_indices()])
4158indent_eq_sub <- function (pd ,
4259 indent_by ,
43- token = " EQ_SUB" ) {
44- eq_sub <- which(pd $ token == " EQ_SUB " )
60+ token = c( " EQ_SUB" , " EQ_FORMALS " ) ) {
61+ eq_sub <- which(pd $ token %in% token )
4562 if (length(eq_sub ) == 0 ) return (pd )
4663 has_line_break <- which(pd $ lag_newlines > 0 )
4764 indent_indices <- intersect(eq_sub + 1 , has_line_break )
@@ -126,7 +143,9 @@ compute_indent_indices <- function(pd,
126143 token_closing = NULL ) {
127144 npd <- nrow(pd )
128145 potential_triggers <- which(pd $ token %in% token_opening )
129- needs_indention <- needs_indention(pd , potential_triggers )
146+ needs_indention <- needs_indention(pd , potential_triggers ,
147+ other_trigger_tokens = c(" EQ_SUB" , " EQ_FORMALS" )
148+ )
130149 trigger <- potential_triggers [needs_indention ][1 ]
131150 if (is.na(trigger )) return (numeric (0 ))
132151 start <- trigger + 1
@@ -144,28 +163,55 @@ compute_indent_indices <- function(pd,
144163# '
145164# ' Checks for each potential trigger token in `pd` whether it actually should
146165# ' cause indention.
147- # ' @param potential_triggers A vector with indices of the potential trigger
166+ # ' @param potential_triggers_pos A vector with indices of the potential trigger
148167# ' tokens in `pd`.
149168# ' @inheritParams needs_indention_one
150- needs_indention <- function (pd , potential_triggers ) {
151- map_lgl(potential_triggers , needs_indention_one , pd = pd )
169+ needs_indention <- function (pd ,
170+ potential_triggers_pos ,
171+ other_trigger_tokens = NULL ) {
172+ map_lgl(potential_triggers_pos , needs_indention_one ,
173+ pd = pd , other_trigger_tokens = other_trigger_tokens
174+ )
152175}
153176
154177
155178# ' Check whether indention is needed
156179# '
157- # ' Indention is needed if and only if there is no multi-line token between the
158- # ' trigger and the first line break.
180+ # ' Indention is needed if the two conditions apply:
181+ # '
182+ # ' * there is no multi-line token between the trigger and the first line break.
183+ # ' * there is no other token between the potential trigger and the first line
184+ # ' break that is going to cause indention.
185+ # '
159186# ' @param pd A parse table.
160- # ' @param potential_trigger the index of the token in the parse table
187+ # ' @param potential_trigger_pos the index of the token in the parse table
161188# ' for which it should be checked whether it should trigger indention.
162189# ' @return Returns `TRUE` if indention is needed, `FALSE` otherwise.
190+ # ' @param other_trigger_tokens Other tokens that are going to cause indention
191+ # ' if on the same line as the token corresponding to `potential_trigger`.
163192# ' @return `TRUE` if indention is needed, `FALSE` otherwise.
164193# ' @importFrom rlang seq2
165- needs_indention_one <- function (pd , potential_trigger ) {
194+ needs_indention_one <- function (pd ,
195+ potential_trigger_pos ,
196+ other_trigger_tokens ) {
166197 before_first_break <- which(pd $ lag_newlines > 0 )[1 ] - 1
167198 if (is.na(before_first_break )) return (FALSE )
168- ! any(pd $ multi_line [seq2(potential_trigger , before_first_break )])
199+ row_idx_between_trigger_and_line_break <- seq2(
200+ potential_trigger_pos , before_first_break
201+ )
202+ multi_line_token <- pd_is_multi_line(
203+ pd [row_idx_between_trigger_and_line_break , ]
204+ )
205+ remaining_row_idx_between_trigger_and_line_break <- setdiff(
206+ row_idx_between_trigger_and_line_break ,
207+ potential_trigger_pos
208+ )
209+
210+ other_trigger_on_same_line <-
211+ pd $ token [remaining_row_idx_between_trigger_and_line_break ] %in%
212+ other_trigger_tokens
213+
214+ ! any(multi_line_token ) & ! any(other_trigger_on_same_line )
169215}
170216
171217
0 commit comments