Skip to content

Commit 337cd9f

Browse files
EQ_FORMALS can cause indention in eq_sub()
needs_indention.*() takes an additional argument other_trigger_tokens which is used to determine whether a trigger causes indention. The new criteria is that (1) there is no multi-line token between the trigger and the first line break and (2) there is no other token between the potential trigger and the first line break that is going to cause indention. Aims to solve #352 and #351.
1 parent 7675e91 commit 337cd9f

File tree

4 files changed

+65
-18
lines changed

4 files changed

+65
-18
lines changed

R/indent.R

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
2324
indent_op <- function(pd,
2425
indent_by,
2526
token = c(
@@ -36,12 +37,14 @@ indent_op <- function(pd,
3637
}
3738

3839
#' @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.
4043
#' occurs is not indented (see[compute_indent_indices()])
4144
indent_eq_sub <- function(pd,
4245
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)
4548
if (length(eq_sub) == 0) return(pd)
4649
has_line_break <- which(pd$lag_newlines > 0)
4750
indent_indices <- intersect(eq_sub + 1, has_line_break)
@@ -126,7 +129,9 @@ compute_indent_indices <- function(pd,
126129
token_closing = NULL) {
127130
npd <- nrow(pd)
128131
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+
)
130135
trigger <- potential_triggers[needs_indention][1]
131136
if (is.na(trigger)) return(numeric(0))
132137
start <- trigger + 1
@@ -147,25 +152,52 @@ compute_indent_indices <- function(pd,
147152
#' @param potential_triggers A vector with indices of the potential trigger
148153
#' tokens in `pd`.
149154
#' @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+
)
152161
}
153162

154163

155164
#' Check whether indention is needed
156165
#'
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+
#'
159172
#' @param pd A parse table.
160173
#' @param potential_trigger the index of the token in the parse table
161174
#' for which it should be checked whether it should trigger indention.
162175
#' @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`.
163178
#' @return `TRUE` if indention is needed, `FALSE` otherwise.
164179
#' @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) {
166183
before_first_break <- which(pd$lag_newlines > 0)[1] - 1
167184
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)
169201
}
170202

171203

man/needs_indention.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/needs_indention_one.Rd

Lines changed: 12 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/update_indention.Rd

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)