Skip to content

Commit

Permalink
indention must be relative, not absolute
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzwalthert committed Jul 16, 2023
1 parent acd0b46 commit de0f8f4
Show file tree
Hide file tree
Showing 6 changed files with 424 additions and 5 deletions.
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
)
)
Loading

0 comments on commit de0f8f4

Please sign in to comment.