Skip to content

Commit 1aaae90

Browse files
Merge pull request #814 from lorenzwalthert/issue-813
Indent between comment and line break
2 parents 14aebe0 + f22501d commit 1aaae90

File tree

5 files changed

+56
-16
lines changed

5 files changed

+56
-16
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
## Minor changes and fixes
5656

5757
* line breaks between `}` and `else` are removed (#793).
58+
* in function calls, code after `= #\n` is indented correctly (#814).
5859
* styler won't format code chunks with explicit `tidy = FALSE` in an Rmd or Rnw
5960
code header anymore. This can be handy when the code can't be parsed, e.g.
6061
within a learnr tutorial (#790).

R/rules-indention.R

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,21 @@ indent_op <- function(pd,
5454
indent_eq_sub <- function(pd,
5555
indent_by,
5656
token = c("EQ_SUB", "EQ_FORMALS")) {
57-
eq_sub <- which(pd$token %in% token)
58-
if (length(eq_sub) == 0) {
57+
eq_sub <- pd$token %in% token
58+
if (!any(eq_sub)) {
5959
return(pd)
6060
}
61-
has_line_break <- which(pd$lag_newlines > 0)
62-
indent_indices <- intersect(eq_sub + 1, has_line_break)
61+
has_line_break <- pd$lag_newlines > 0 | pd$token == "COMMENT"
62+
indent_indices <- which(lag(eq_sub, default = FALSE) & has_line_break)
63+
if (any(pd$token[indent_indices] == "COMMENT")) {
64+
indent_indices <- purrr::map_int(indent_indices, function(idx) {
65+
if (pd$token[idx] == "COMMENT") {
66+
next_non_comment(pd, idx)
67+
} else {
68+
idx
69+
}
70+
})
71+
}
6372
pd$indent[indent_indices] <- pd$indent[indent_indices] + indent_by
6473
pd
6574
}

tests/testthat/indention_operators/eq_sub_complex_tokens-in.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,10 @@ call(a =
66
5,
77
b
88
)
9+
10+
c(
11+
a =
12+
1,
13+
b = # comment here
14+
2
15+
)

tests/testthat/indention_operators/eq_sub_complex_tokens-in_tree

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

tests/testthat/indention_operators/eq_sub_complex_tokens-out.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,10 @@ call(
99
5,
1010
b
1111
)
12+
13+
c(
14+
a =
15+
1,
16+
b = # comment here
17+
2
18+
)

0 commit comments

Comments
 (0)