Skip to content

Commit dc3c099

Browse files
Merge branch 'r-lib:main' into 963_change_defaults
2 parents b6a2b18 + a3b69e4 commit dc3c099

File tree

7 files changed

+150
-8
lines changed

7 files changed

+150
-8
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ editor_options:
1515
**Features**
1616

1717
- `filetype` `.qmd` is now supported, but not turned on by default (#931).
18+
- alignment is now detected for function declaration in a similar way as for
19+
function calls (#968).
1820
- new R option `styler.ignore_alignment` controls if alignment should be
1921
detected (and preserved) or not (#932).
2022

R/detect-alignment-utils.R

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,38 @@ alignment_drop_comments <- function(pd_by_line) {
4141
compact()
4242
}
4343

44+
45+
#' Remove last expression
46+
#'
47+
#' In a *nest*, if the last token is an `expr`, the *nest* represents either
48+
#' an if, while or for statement or a function call. We don't call about that
49+
#' part, in fact it's important to remove it for alignment. See 'Examples'.
50+
#'
51+
#' @examples
52+
#' if (FALSE) {
53+
#' call(
54+
#' x = 12,
55+
#' y = 3,
56+
#' )
57+
#'
58+
#' function(a = 33,
59+
#' qq = 4) {
60+
#' # we don't care about this part for alignment detection
61+
#' }
62+
#' }
63+
#' @keywords internal
64+
alignment_drop_last_expr <- function(pds_by_line) {
65+
# TODO could be skipped if we know it's not a function dec
66+
pd_last_line <- pds_by_line[[length(pds_by_line)]]
67+
last_two_lines <- pd_last_line$token[c(nrow(pd_last_line) - 1, nrow(pd_last_line))]
68+
if (identical(last_two_lines, c("')'", "expr"))) {
69+
pd_last_line <- pd_last_line[-nrow(pd_last_line), ]
70+
}
71+
pds_by_line[[length(pds_by_line)]] <- pd_last_line
72+
pds_by_line
73+
}
74+
75+
4476
#' Ensure last pd has a trailing comma
4577
#'
4678
#' Must be after [alignment_ensure_no_closing_brace()] because if it comes after
@@ -83,9 +115,9 @@ alignment_col1_all_named <- function(relevant_pd_by_line) {
83115
return(FALSE)
84116
}
85117
x$token[3] == "expr" &&
86-
x$token[1] %in% c("SYMBOL_SUB", "STR_CONST") &&
118+
x$token[1] %in% c("SYMBOL_SUB", "STR_CONST", "SYMBOL_FORMALS") &&
87119
x$token[2] %in% c(
88-
"EQ_SUB", "SPECIAL-IN", "LT", "GT", "EQ", "NE"
120+
"EQ_SUB", "EQ_FORMALS", "SPECIAL-IN", "LT", "GT", "EQ", "NE"
89121
)
90122
}) %>%
91123
all()

R/detect-alignment.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,13 @@ token_is_on_aligned_line <- function(pd_flat) {
9292
return(FALSE)
9393
}
9494

95-
pd_by_line <- alignment_drop_comments(pd_by_line) %>%
96-
alignment_ensure_no_closing_brace(last_line_is_closing_brace_only)
95+
pd_by_line <- alignment_drop_comments(pd_by_line)
9796
if (length(pd_by_line) < 1) {
9897
return(TRUE)
9998
}
99+
pd_by_line <- alignment_drop_last_expr(pd_by_line) %>%
100+
alignment_ensure_no_closing_brace(last_line_is_closing_brace_only)
101+
100102
pd_by_line <- pd_by_line %>%
101103
alignment_ensure_trailing_comma()
102104
# now, pd only contains arguments separated by values, ideal for iterating

R/rules-spaces.R

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ set_space_around_op <- function(pd_flat, strict) {
1616
if (!any(op_after)) {
1717
return(pd_flat)
1818
}
19-
if (sum(pd_flat$lag_newlines) > 2 &&
20-
is_function_call(pd_flat) &&
21-
any(pd_flat$token %in% c("EQ_SUB", "','")) &&
22-
!getOption("styler.ignore_alignment", FALSE)
19+
if (
20+
!getOption("styler.ignore_alignment", FALSE) &&
21+
(
22+
(is_function_call(pd_flat) && sum(pd_flat$lag_newlines) > 2) ||
23+
(is_function_dec(pd_flat) && sum(pd_flat$lag_newlines) > 1)
24+
) &&
25+
any(pd_flat$token %in% c("EQ_SUB", "','", "EQ_FORMALS"))
2326
) {
2427
is_on_aligned_line <- token_is_on_aligned_line(pd_flat)
2528
} else {

man/alignment_drop_last_expr.Rd

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# aligned
2+
function(x = NULL,
3+
tt = NULL,
4+
ayz = NULL) {}
5+
6+
7+
# aligned
8+
k <- function(x = NULL,
9+
aq = NULL,
10+
ayz = NULL) {}
11+
12+
13+
# aligned, eq right
14+
function(x = 2,
15+
tt = 1,
16+
ayz = 99) {}
17+
18+
# aligned, eq left
19+
function(x = 2,
20+
tt = 1,
21+
ayz = 99) {}
22+
23+
24+
# not aligned
25+
k <- function(x = fish,
26+
aq = 21,
27+
ayz = t(322)) {}
28+
29+
# aligned
30+
k <- function(x = flus(we),
31+
aq = x - 22, k = 22,
32+
ayz = m(jk5), xfea = 3) {}
33+
34+
35+
# aligned
36+
k <- function(x = flus(we),
37+
aq = x - 22, k = 22,
38+
ayz = m(jk5), xfea = 3) {}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# aligned
2+
function(x = NULL,
3+
tt = NULL,
4+
ayz = NULL) {}
5+
6+
7+
# aligned
8+
k <- function(x = NULL,
9+
aq = NULL,
10+
ayz = NULL) {}
11+
12+
13+
# aligned, eq right
14+
function(x = 2,
15+
tt = 1,
16+
ayz = 99) {}
17+
18+
# aligned, eq left
19+
function(x = 2,
20+
tt = 1,
21+
ayz = 99) {}
22+
23+
24+
# not aligned
25+
k <- function(x = fish,
26+
aq = 21,
27+
ayz = t(322)) {}
28+
29+
# aligned
30+
k <- function(x = flus(we),
31+
aq = x - 22, k = 22,
32+
ayz = m(jk5), xfea = 3) {}
33+
34+
35+
# aligned
36+
k <- function(x = flus(we),
37+
aq = x - 22, k = 22,
38+
ayz = m(jk5), xfea = 3) {}

0 commit comments

Comments
 (0)