Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix double indention for function declaration if they have a base indention #1137

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
25 changes: 22 additions & 3 deletions R/rules-indention.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ unindent_fun_dec <- function(pd, indent_by = 2L) {
#' Is the function declaration double indented?
#'
#' Assumes you already checked if it's a function with
#' `is_function_declaration`. It is double indented if the first token
#' after the first line break that is a `"SYMBOL_FORMALS"`.
#' `is_function_declaration`. It is double indented if the body of the function
#' is indented less than the first argument of the function.
#' @param pd A parse table.
#' @inheritParams tidyverse_style
#' @keywords internal
Expand All @@ -45,7 +45,26 @@ is_double_indent_function_declaration <- function(pd, indent_by = 2L) {
if (length(line_break_in_header) > 0L) {
# indent results from applying the rules, spaces is the initial spaces
# (which is indention if a newline is ahead)
pd$spaces[line_break_in_header[1L] - 1L] <= 2L * indent_by

idx_line_break <- last(which(pd$newlines > 0L))
if (length(idx_line_break) > 0L && idx_line_break + 1L == nrow(pd)) {
# function() #
# { <- measure indention on opening brace
# if last line break is at last token ("'{'")
indention_child <- pd$spaces[idx_line_break]
} else {
# function() { #
# stuff <-measure indention inside the brace
child <- pd$child[[nrow(pd)]]
# even with comments, first is {, otherwise it's first case
# child$token == "'{'" & child$lag_newlines > 0

idx_first_line_break_in_child <- which(child$newlines > 0L)[1L]
indention_child <- child$spaces[idx_first_line_break_in_child]
}


pd$spaces[line_break_in_header[1L] - 1L] > indention_child
} else {
FALSE
}
Expand Down
4 changes: 2 additions & 2 deletions man/is_double_indent_function_declaration.Rd

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

62 changes: 62 additions & 0 deletions tests/testthat/indention_operators/r6-double-in.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# normal cases
R6Class(
"MyClass",
public = list(
initialize = function(
my_long_parameter = getOption("default_long_parameter", 7)) { #
self$param <- my_long_paramete
}
)
)


R6Class(
"MyClass",
public = list(
initialize = function(
my_long_parameter = getOption("default_long_parameter", 7)) {
self$param <- my_long_paramete
}
)
)


R6Class("MyClass",
public = list(
initialize = function(
my_long_parameter = getOption("default_long_parameter", 7))
{
self$param <- my_long_paramete
}
)
)

R6Class(
"MyClass",
public = list(
initialize = function(
my_long_parameter = getOption("default_long_parameter", 7))
#
{
self$param <- my_long_paramete
}
)
)

R6Class(
"MyClass",
public = list(
initialize = function(
my_long_parameter = getOption("default_long_parameter", 7))
{self$param <- my_long_paramete}
)
)

R6Class(
"MyClass",
public = list(
initialize = function(
my_long_parameter = getOption("default_long_parameter", 7))
NULL
)
)
63 changes: 63 additions & 0 deletions tests/testthat/indention_operators/r6-double-out.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# normal cases
R6Class(
"MyClass",
public = list(
initialize = function(
my_long_parameter = getOption("default_long_parameter", 7)) { #
self$param <- my_long_paramete
}
)
)


R6Class(
"MyClass",
public = list(
initialize = function(
my_long_parameter = getOption("default_long_parameter", 7)) {
self$param <- my_long_paramete
}
)
)


R6Class("MyClass",
public = list(
initialize = function(
my_long_parameter = getOption("default_long_parameter", 7)) {
self$param <- my_long_paramete
}
)
)

R6Class(
"MyClass",
public = list(
initialize = function(
my_long_parameter = getOption("default_long_parameter", 7))
#
{
self$param <- my_long_paramete
}
)
)

R6Class(
"MyClass",
public = list(
initialize = function(
my_long_parameter = getOption("default_long_parameter", 7)) {
self$param <- my_long_paramete
}
)
)

R6Class(
"MyClass",
public = list(
initialize = function(
my_long_parameter = getOption("default_long_parameter", 7)) {
NULL
}
)
)
7 changes: 7 additions & 0 deletions tests/testthat/test-indention_operators.R
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,10 @@ test_that("overall", {
transformer = style_text
), NA)
})

test_that("double-indent-r6", {
expect_warning(test_collection("indention_operators",
"r6",
transformer = style_text
), NA)
})
Loading