Skip to content

Commit

Permalink
double -> single
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed Oct 19, 2024
1 parent 5e5eb3a commit 8b9a4a0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
13 changes: 7 additions & 6 deletions R/rules-indention.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ indent_braces <- function(pd, indent_by) {
#'
#' Necessary for consistent indention of the function declaration header.
#' @param pd A parse table.
#' @inheritParams is_double_indent_function_declaration
#' @inheritParams is_single_indent_function_declaration
#' @seealso set_unindention_child update_indention_ref_fun_dec
#' @keywords internal
unindent_fun_dec <- function(pd, indent_by = 2L) {
if (is_function_declaration(pd)) {
idx_closing_brace <- which(pd$token == "')'")
fun_dec_head <- seq2(2L, idx_closing_brace)
if (is_double_indent_function_declaration(pd, indent_by = indent_by)) {
if (is_single_indent_function_declaration(pd, indent_by = indent_by)) {
pd$indent[fun_dec_head] <- indent_by
pd$indent[idx_closing_brace] <- 0L
} else {
Expand All @@ -32,20 +32,21 @@ unindent_fun_dec <- function(pd, indent_by = 2L) {
pd
}

#' Is the function declaration double indented?
#' Is the function declaration single indented?
#'
#' Assumes you already checked if it's a function with
#' `is_function_declaration`. It is double indented if the first token
#' `is_function_declaration`. It is single indented if the first token
#' after the first line break that is a `"SYMBOL_FORMALS"`.
#' @param pd A parse table.
#' @inheritParams tidyverse_style
#' @keywords internal
is_double_indent_function_declaration <- function(pd, indent_by = 2L) {
is_single_indent_function_declaration <- function(pd, indent_by = 2L) {
head_pd <- vec_slice(pd, -nrow(pd))
line_break_in_header <- which(head_pd$lag_newlines > 0L & head_pd$token == "SYMBOL_FORMALS")
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)
# The 2L factor is kept to convert double indent to single indent
pd$spaces[line_break_in_header[1L] - 1L] <= 2L * indent_by
} else {
FALSE
Expand Down Expand Up @@ -133,7 +134,7 @@ NULL
#'
#' @keywords internal
update_indention_ref_fun_dec <- function(pd_nested) {
if (is_function_declaration(pd_nested) && !is_double_indent_function_declaration(pd_nested)) {
if (is_function_declaration(pd_nested) && !is_single_indent_function_declaration(pd_nested)) {
seq <- seq2(3L, nrow(pd_nested) - 2L)
pd_nested$indention_ref_pos_id[seq] <- pd_nested$pos_id[2L]
}
Expand Down
2 changes: 1 addition & 1 deletion R/rules-line-breaks.R
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ remove_line_break_before_round_closing_after_curly <- function(pd) {

remove_line_breaks_in_fun_dec <- function(pd) {
if (is_function_declaration(pd)) {
is_double_indention <- is_double_indent_function_declaration(pd)
is_double_indention <- is_single_indent_function_declaration(pd)
round_after <- (
pd$token == "')'" | pd$token_before == "'('"
) &
Expand Down

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

0 comments on commit 8b9a4a0

Please sign in to comment.