Skip to content

Commit

Permalink
Merge branch 'r-lib:main' into 963_change_defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
IndrajeetPatil authored Aug 6, 2022
2 parents b6a2b18 + a3b69e4 commit dc3c099
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 8 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ editor_options:
**Features**

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

Expand Down
36 changes: 34 additions & 2 deletions R/detect-alignment-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,38 @@ alignment_drop_comments <- function(pd_by_line) {
compact()
}


#' Remove last expression
#'
#' In a *nest*, if the last token is an `expr`, the *nest* represents either
#' an if, while or for statement or a function call. We don't call about that
#' part, in fact it's important to remove it for alignment. See 'Examples'.
#'
#' @examples
#' if (FALSE) {
#' call(
#' x = 12,
#' y = 3,
#' )
#'
#' function(a = 33,
#' qq = 4) {
#' # we don't care about this part for alignment detection
#' }
#' }
#' @keywords internal
alignment_drop_last_expr <- function(pds_by_line) {
# TODO could be skipped if we know it's not a function dec
pd_last_line <- pds_by_line[[length(pds_by_line)]]
last_two_lines <- pd_last_line$token[c(nrow(pd_last_line) - 1, nrow(pd_last_line))]
if (identical(last_two_lines, c("')'", "expr"))) {
pd_last_line <- pd_last_line[-nrow(pd_last_line), ]
}
pds_by_line[[length(pds_by_line)]] <- pd_last_line
pds_by_line
}


#' Ensure last pd has a trailing comma
#'
#' Must be after [alignment_ensure_no_closing_brace()] because if it comes after
Expand Down Expand Up @@ -83,9 +115,9 @@ alignment_col1_all_named <- function(relevant_pd_by_line) {
return(FALSE)
}
x$token[3] == "expr" &&
x$token[1] %in% c("SYMBOL_SUB", "STR_CONST") &&
x$token[1] %in% c("SYMBOL_SUB", "STR_CONST", "SYMBOL_FORMALS") &&
x$token[2] %in% c(
"EQ_SUB", "SPECIAL-IN", "LT", "GT", "EQ", "NE"
"EQ_SUB", "EQ_FORMALS", "SPECIAL-IN", "LT", "GT", "EQ", "NE"
)
}) %>%
all()
Expand Down
6 changes: 4 additions & 2 deletions R/detect-alignment.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,13 @@ token_is_on_aligned_line <- function(pd_flat) {
return(FALSE)
}

pd_by_line <- alignment_drop_comments(pd_by_line) %>%
alignment_ensure_no_closing_brace(last_line_is_closing_brace_only)
pd_by_line <- alignment_drop_comments(pd_by_line)
if (length(pd_by_line) < 1) {
return(TRUE)
}
pd_by_line <- alignment_drop_last_expr(pd_by_line) %>%
alignment_ensure_no_closing_brace(last_line_is_closing_brace_only)

pd_by_line <- pd_by_line %>%
alignment_ensure_trailing_comma()
# now, pd only contains arguments separated by values, ideal for iterating
Expand Down
11 changes: 7 additions & 4 deletions R/rules-spaces.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ set_space_around_op <- function(pd_flat, strict) {
if (!any(op_after)) {
return(pd_flat)
}
if (sum(pd_flat$lag_newlines) > 2 &&
is_function_call(pd_flat) &&
any(pd_flat$token %in% c("EQ_SUB", "','")) &&
!getOption("styler.ignore_alignment", FALSE)
if (
!getOption("styler.ignore_alignment", FALSE) &&
(
(is_function_call(pd_flat) && sum(pd_flat$lag_newlines) > 2) ||
(is_function_dec(pd_flat) && sum(pd_flat$lag_newlines) > 1)
) &&
any(pd_flat$token %in% c("EQ_SUB", "','", "EQ_FORMALS"))
) {
is_on_aligned_line <- token_is_on_aligned_line(pd_flat)
} else {
Expand Down
27 changes: 27 additions & 0 deletions man/alignment_drop_last_expr.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions tests/testthat/alignment/fun-decs-in.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# aligned
function(x = NULL,
tt = NULL,
ayz = NULL) {}


# aligned
k <- function(x = NULL,
aq = NULL,
ayz = NULL) {}


# aligned, eq right
function(x = 2,
tt = 1,
ayz = 99) {}

# aligned, eq left
function(x = 2,
tt = 1,
ayz = 99) {}


# not aligned
k <- function(x = fish,
aq = 21,
ayz = t(322)) {}

# aligned
k <- function(x = flus(we),
aq = x - 22, k = 22,
ayz = m(jk5), xfea = 3) {}


# aligned
k <- function(x = flus(we),
aq = x - 22, k = 22,
ayz = m(jk5), xfea = 3) {}
38 changes: 38 additions & 0 deletions tests/testthat/alignment/fun-decs-out.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# aligned
function(x = NULL,
tt = NULL,
ayz = NULL) {}


# aligned
k <- function(x = NULL,
aq = NULL,
ayz = NULL) {}


# aligned, eq right
function(x = 2,
tt = 1,
ayz = 99) {}

# aligned, eq left
function(x = 2,
tt = 1,
ayz = 99) {}


# not aligned
k <- function(x = fish,
aq = 21,
ayz = t(322)) {}

# aligned
k <- function(x = flus(we),
aq = x - 22, k = 22,
ayz = m(jk5), xfea = 3) {}


# aligned
k <- function(x = flus(we),
aq = x - 22, k = 22,
ayz = m(jk5), xfea = 3) {}

0 comments on commit dc3c099

Please sign in to comment.