Skip to content

Commit 53ae5e1

Browse files
Merge pull request #372 from lorenzwalthert/fix-inention
- Fix indention (#372).
2 parents 01681bc + 1b5ee66 commit 53ae5e1

17 files changed

+245
-12
lines changed

R/indent.R

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,21 +188,32 @@ needs_indention <- function(pd,
188188

189189
#' Check whether indention is needed
190190
#'
191+
#' Determine whether the tokens corresponding to `potential_trigger_pos` should
192+
#' cause indention, considering that there might be other potential triggers
193+
#' `other_trigger_tokens` that are going to cause indention.
191194
#' Indention is needed if the two conditions apply:
192195
#'
193196
#' * there is no multi-line token between the trigger and the first line break.
194197
#' * there is no other token between the potential trigger and the first line
195-
#' break that is going to cause indention.
196-
#'
198+
#' break that is going to cause indention. Note that such an other trigger
199+
#' only causes indention if there is a line break after that other triggering
200+
#' token, not otherwise. If it causes indention, it is said to be an active
201+
#' trigger, if it does not, it is called an inactive trigger.
202+
#' See 'Details' for an example where there is an other trigger token, but
203+
#' since the next token is on the same line as the other trigger,
204+
#' the trigger is passive.
197205
#' @param pd A parse table.
198206
#' @param potential_trigger_pos the index of the token in the parse table
199207
#' for which it should be checked whether it should trigger indention.
200208
#' @return Returns `TRUE` if indention is needed, `FALSE` otherwise.
201209
#' @param other_trigger_tokens Other tokens that are going to cause indention
202-
#' if on the same line as the token corresponding to `potential_trigger`.
210+
#' if on the same line as the token corresponding to `potential_trigger` and
211+
#' directly followed by a line break.
203212
#' @return `TRUE` if indention is needed, `FALSE` otherwise.
204213
#' @importFrom rlang seq2
205214
#' @keywords internal
215+
#' @examples
216+
#' style_text("call(named = c, \nnamed = b)", strict = FALSE)
206217
needs_indention_one <- function(pd,
207218
potential_trigger_pos,
208219
other_trigger_tokens) {
@@ -219,11 +230,17 @@ needs_indention_one <- function(pd,
219230
potential_trigger_pos
220231
)
221232

222-
other_trigger_on_same_line <-
223-
pd$token[remaining_row_idx_between_trigger_and_line_break] %in%
224-
other_trigger_tokens
233+
other_trigger_on_same_line <- (
234+
pd$token[remaining_row_idx_between_trigger_and_line_break] %in%
235+
other_trigger_tokens
236+
)
237+
line_break_after_other_trigger <-
238+
pd$lag_newlines[remaining_row_idx_between_trigger_and_line_break + 1L] > 0L
239+
240+
active_trigger_on_same_line <-
241+
other_trigger_on_same_line & line_break_after_other_trigger
225242

226-
!any(multi_line_token) & !any(other_trigger_on_same_line)
243+
!any(multi_line_token) & !any(active_trigger_on_same_line)
227244
}
228245

229246

man/needs_indention.Rd

Lines changed: 2 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: 15 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
call(a,
2+
b)
3+
4+
call(a,
5+
b = 3)
6+
7+
call(a = 1, b =
8+
3)
9+
10+
call(a =
11+
1, b = 3)
12+
13+
call(a = 1,
14+
b = 3
15+
)
16+
17+
call(
18+
a = 1,
19+
b = 3
20+
)
21+
22+
call(
23+
a =
24+
1,
25+
b = 3
26+
)
27+
28+
call(
29+
a =
30+
1, b = 3
31+
)
32+
33+
call(
34+
a =
35+
1, b =
36+
3
37+
)

tests/testthat/indention_fun_calls/non_strict_calls-in_tree

Lines changed: 121 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
call(a,
2+
b)
3+
4+
call(a,
5+
b = 3)
6+
7+
call(a = 1, b =
8+
3)
9+
10+
call(a =
11+
1, b = 3)
12+
13+
call(a = 1,
14+
b = 3
15+
)
16+
17+
call(
18+
a = 1,
19+
b = 3
20+
)
21+
22+
call(
23+
a =
24+
1,
25+
b = 3
26+
)
27+
28+
call(
29+
a =
30+
1, b = 3
31+
)
32+
33+
call(
34+
a =
35+
1, b =
36+
3
37+
)

0 commit comments

Comments
 (0)