Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion R/expr-is.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ is_subset_expr <- function(pd) {
if (is.null(pd) || nrow(pd) == 1) {
return(FALSE)
}
pd$token[2L] == "'['"
pd$token[2L] %in% subset_token_opening
}


Expand Down
10 changes: 9 additions & 1 deletion R/indent.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,15 @@ indent_without_paren_if_else <- function(pd, indent_by) {
#' example in if-else expressions, this is not the case and indenting
#' everything between '(' and the penultimate token would result in the wrong
#' formatting.
#' @section Handing of `[[`:
#' Since text `[[` has token `"LBB"` and text `]]` is parsed as two independent
#' `]` (see 'Examples'), indention has to stop at the first `]`.
# one token earlier
#' @importFrom rlang seq2
#' @keywords internal
#' @examples
#' styler:::parse_text("a[1]")
#' styler:::parse_text("a[[1\n]]")
compute_indent_indices <- function(pd,
token_opening,
token_closing = NULL) {
Expand All @@ -105,7 +112,8 @@ compute_indent_indices <- function(pd,
if (is.null(token_closing)) {
stop <- npd
} else {
stop <- last(which(pd$token %in% token_closing)[needs_indention]) - 1
offset <- if (any(pd$token == "LBB")) 2L else 1L
stop <- last(which(pd$token %in% token_closing)[needs_indention]) - offset
}

seq2(start, stop)
Expand Down
2 changes: 1 addition & 1 deletion R/rules-indention.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
indent_braces <- function(pd, indent_by) {
indent_indices <- compute_indent_indices(
pd,
token_opening = c("'('", "'['", "'{'"),
token_opening = c("'('", "'['", "'{'", "LBB"),
token_closing = c("')'", "']'", "'}'")
)
pd$indent[indent_indices] <- pd$indent[indent_indices] + indent_by
Expand Down
11 changes: 8 additions & 3 deletions R/rules-line-breaks.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ set_line_break_around_comma_and_or <- function(pd, strict) {
(pd$token %in% ops) &
(pd$lag_newlines > 0L) &
(pd$token_before != "COMMENT") &
(lag(pd$token) != "'['")
!(lag(pd$token) %in% subset_token_opening)

pd$lag_newlines[comma_with_line_break_that_can_be_removed_before] <- 0L
pd$lag_newlines[lag(comma_with_line_break_that_can_be_removed_before)] <- 1L
Expand All @@ -137,7 +137,7 @@ set_line_break_around_comma_and_or <- function(pd, strict) {
(pd$token == "EQ_SUB") &
(pd$lag_newlines > 0L) &
(pd$token_before != "COMMENT") &
(lag(pd$token) != "'['")
!(lag(pd$token) %in% subset_token_opening)
)

pd$lag_newlines[comma_with_line_break_that_can_be_moved_two_tokens_left] <- 0L
Expand Down Expand Up @@ -362,7 +362,12 @@ set_line_break_before_closing_call <- function(pd, except_token_before) {
pd$lag_newlines[setdiff(npd, exception)] <- 0L
return(pd)
}
pd$lag_newlines[npd] <- 1L
idx_non_comment <- previous_non_comment(pd, npd)
if (pd$token[idx_non_comment] == "']'") {
pd$lag_newlines[idx_non_comment] <- 1L
} else {
pd$lag_newlines[npd] <- 1L
}
pd
}

Expand Down
2 changes: 2 additions & 0 deletions R/token-define.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,5 @@ op_token <- c(
"EQ_SUB", "ELSE", "IN",
"EQ_FORMALS"
)

subset_token_opening <- c("'['", "LBB")
10 changes: 10 additions & 0 deletions man/compute_indent_indices.Rd

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
a[[b]]


a[[
2
]
]

a[[
2
]]


a[[
2
]]


a[[
2
] #
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
a[[b]]


a[[
2
]
]

a[[
2
]]


a[[
2
]]


a[[
2
] #
]
1 change: 0 additions & 1 deletion tests/testthat/test-square_brackets.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
test_that("square brackets cause indention", {
expect_warning(test_collection(
"indention_square_brackets",
"square_brackets_line_break",
transformer = style_text
), NA)
})