Skip to content

Commit

Permalink
Merge branch 'main' into f209-newline-multiline-args
Browse files Browse the repository at this point in the history
  • Loading branch information
IndrajeetPatil authored Dec 18, 2024
2 parents 27b5eca + 84f250a commit 6df0686
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 79 deletions.
6 changes: 3 additions & 3 deletions R/rules-indention.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ indent_braces <- function(pd, indent_by) {
#' Necessary for consistent indention of the function declaration header.
#' @param pd A parse table.
#' @inheritParams is_single_indent_function_declaration
#' @seealso set_unindention_child update_indention_ref_fun_dec
#' @seealso set_unindention_child update_indention_reference_function_declaration
#' @keywords internal
unindent_fun_dec <- function(pd, indent_by = 2L) {
unindent_function_declaration <- function(pd, indent_by = 2L) {
if (is_function_declaration(pd)) {
idx_closing_brace <- which(pd$token == "')'")
fun_dec_head <- seq2(2L, idx_closing_brace)
Expand Down Expand Up @@ -133,7 +133,7 @@ NULL
#' }
#'
#' @keywords internal
update_indention_ref_fun_dec <- function(pd_nested) {
update_indention_reference_function_declaration <- function(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 @@ -234,7 +234,7 @@ remove_line_break_before_round_closing_after_curly <- function(pd) {
pd
}

remove_line_breaks_in_fun_dec <- function(pd) {
remove_line_breaks_in_function_declaration <- function(pd) {
if (is_function_declaration(pd)) {
is_single_indention <- is_single_indent_function_declaration(pd)
round_after <- (
Expand Down
6 changes: 3 additions & 3 deletions R/rules-spaces.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' @include token-define.R
#' @keywords internal
#' @include token-define.R
set_space_around_op <- function(pd_flat, strict) {
set_space_around_operator <- function(pd_flat, strict) {
# spacing and operator in same function because alternative is
# calling token_is_on_aligned_line() twice because comma and operator spacing
# depends on it.
Expand Down Expand Up @@ -122,7 +122,7 @@ style_space_around_tilde <- function(pd_flat, strict) {
pd_flat
}

remove_space_after_unary_pm_nested <- function(pd) {
remove_space_after_unary_plus_minus_nested <- function(pd) {
if (any(pd$token[1L] %in% c("'+'", "'-'"))) {
pd$spaces[1L] <- 0L
}
Expand Down Expand Up @@ -349,7 +349,7 @@ remove_space_around_dollar <- function(pd_flat) {
pd_flat
}

remove_space_after_fun_dec <- function(pd_flat) {
remove_space_after_function_declaration <- function(pd_flat) {
fun_after <- (pd_flat$token == "FUNCTION") & (pd_flat$lag_newlines == 0L)
pd_flat$spaces[fun_after] <- 0L
pd_flat
Expand Down
4 changes: 2 additions & 2 deletions R/rules-tokens.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ add_brackets_in_pipe_one <- function(pd, pos) {
#' @param indent_by The amount of spaces used to indent an expression in curly
#' braces. Used for unindention.
#' @keywords internal
wrap_if_else_while_for_fun_multi_line_in_curly <- function(pd, indent_by = 2L) {
wrap_if_else_while_for_function_multi_line_in_curly <- function(pd, indent_by = 2L) {
key_token <- NULL

if (is_for_expr(pd)) {
Expand All @@ -88,7 +88,7 @@ wrap_if_else_while_for_fun_multi_line_in_curly <- function(pd, indent_by = 2L) {

#' Wrap a multi-line statement in curly braces
#'
#' @inheritParams wrap_if_else_while_for_fun_multi_line_in_curly
#' @inheritParams wrap_if_else_while_for_function_multi_line_in_curly
#' @inheritParams wrap_subexpr_in_curly
#' @param key_token The token that comes right before the token that contains
#' the expression to be wrapped (ignoring comments). For if and while loops,
Expand Down
30 changes: 15 additions & 15 deletions R/style-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ tidyverse_style <- function(scope = "tokens",
indention_manipulators <- if ("indention" %in% scope) {
list(
indent_braces = partial(indent_braces, indent_by = indent_by),
unindent_fun_dec = unindent_fun_dec,
unindent_function_declaration = unindent_function_declaration,
indent_op = partial(indent_op, indent_by = indent_by),
indent_eq_sub = partial(indent_eq_sub, indent_by = indent_by),
indent_without_paren = partial(indent_without_paren,
indent_by = indent_by
),
update_indention_ref_fun_dec = update_indention_ref_fun_dec
update_indention_reference_function_declaration = update_indention_reference_function_declaration
)
}
space_manipulators <- if ("spaces" %in% scope) {
Expand All @@ -102,19 +102,19 @@ tidyverse_style <- function(scope = "tokens",
style_space_around_tilde,
strict = strict
),
spacing_around_op = purrr::partial(set_space_around_op,
spacing_around_op = purrr::partial(set_space_around_operator,
strict = strict
),
remove_space_after_opening_paren = remove_space_after_opening_paren,
remove_space_after_excl = remove_space_after_excl,
set_space_after_bang_bang = set_space_after_bang_bang,
remove_space_around_dollar = remove_space_around_dollar,
remove_space_after_fun_dec = remove_space_after_fun_dec,
remove_space_after_function_declaration = remove_space_after_function_declaration,
remove_space_around_colons = remove_space_around_colons,
start_comments_with_space = partial(start_comments_with_space,
force_one = start_comments_with_one_space
),
remove_space_after_unary_pm_nested = remove_space_after_unary_pm_nested,
remove_space_after_unary_plus_minus_nested = remove_space_after_unary_plus_minus_nested,
spacing_before_comments = if (strict) {
set_space_before_comments
} else {
Expand All @@ -137,8 +137,8 @@ tidyverse_style <- function(scope = "tokens",
set_line_break_before_curly_opening = set_line_break_before_curly_opening,
remove_line_break_before_round_closing_after_curly =
if (strict) remove_line_break_before_round_closing_after_curly,
remove_line_breaks_in_fun_dec =
if (strict) remove_line_breaks_in_fun_dec,
remove_line_breaks_in_function_declaration =
if (strict) remove_line_breaks_in_function_declaration,
set_line_breaks_between_top_level_exprs =
if (strict) set_line_breaks_between_top_level_exprs,
set_line_breaks_for_multiline_args =
Expand Down Expand Up @@ -182,10 +182,10 @@ tidyverse_style <- function(scope = "tokens",
force_assignment_op = force_assignment_op,
resolve_semicolon = resolve_semicolon,
add_brackets_in_pipe = add_brackets_in_pipe,
wrap_if_else_while_for_fun_multi_line_in_curly =
wrap_if_else_while_for_function_multi_line_in_curly =
if (strict) {
purrr::partial(
wrap_if_else_while_for_fun_multi_line_in_curly,
wrap_if_else_while_for_function_multi_line_in_curly,
indent_by = indent_by
)
}
Expand All @@ -208,23 +208,23 @@ tidyverse_style <- function(scope = "tokens",
remove_space_after_excl = "'!'",
set_space_after_bang_bang = "'!'",
remove_space_around_dollar = "'$'",
remove_space_after_fun_dec = "FUNCTION",
remove_space_after_function_declaration = "FUNCTION",
remove_space_around_colons = c("':'", "NS_GET_INT", "NS_GET"),
start_comments_with_space = "COMMENT",
remove_space_after_unary_pm_nested = c("'+'", "'-'"),
remove_space_after_unary_plus_minus_nested = c("'+'", "'-'"),
spacing_before_comments = "COMMENT",
set_space_in_curly = c("'{'", "'}'")
),
indention = list(
# indent_braces = c("'('", "'['", "'{'", "')'", "']'", "'}'"),
unindent_fun_dec = "FUNCTION",
unindent_function_declaration = "FUNCTION",
indent_eq_sub = c("EQ_SUB", "EQ_FORMALS"), # TODO rename
update_indention_ref_fun_dec = "FUNCTION"
update_indention_reference_function_declaration = "FUNCTION"
),
line_breaks = list(
set_line_break_before_curly_opening = "'{'",
remove_line_break_before_round_closing_after_curly = "'}'",
remove_line_breaks_in_fun_dec = "FUNCTION",
remove_line_breaks_in_function_declaration = "FUNCTION",
set_line_break_around_curly_curly = "'{'",
style_line_break_around_curly = "'{'",
add_line_break_after_pipe = c("SPECIAL-PIPE", "PIPE")
Expand All @@ -233,7 +233,7 @@ tidyverse_style <- function(scope = "tokens",
resolve_semicolon = "';'",
add_brackets_in_pipe = c("SPECIAL-PIPE", "PIPE"),
force_assignment_op = "EQ_ASSIGN",
wrap_if_else_while_for_fun_multi_line_in_curly = c(
wrap_if_else_while_for_function_multi_line_in_curly = c(
"IF", "WHILE", "FOR", "FUNCTION"
)
)
Expand Down
2 changes: 1 addition & 1 deletion R/utils-cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ cache_by_expression <- function(text,
} else {
expressions$stylerignore <- rep(FALSE, length(expressions$text))
}
# TODO base_indention should be set to 0 on write and on read for expressions
# TODO base_indention should be set to 0 on write and on read for expressions
# (only) to make it possible to use the cache for expressions with different
# indention. when not the whole input text is cached, we go trough all
# expressions and check if they are cached, if yes, we take the input (from
Expand Down
17 changes: 0 additions & 17 deletions R/utils-navigate-nest.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,3 @@ next_terminal <- function(pd,
}
}
}


#' Find the index of the last comment in the sequence of comments-only tokens
#' after the token that has position `pos` in `pd`.
#' @param pd A parse table.
#' @param pos The position of the token to start the search from.
#' @keywords internal
extend_if_comment <- function(pd, pos) {
if (pos == nrow(pd)) {
return(pos)
}
if (pd$token[pos + 1L] == "COMMENT") {
extend_if_comment(pd, pos + 1L)
} else {
pos
}
}
19 changes: 0 additions & 19 deletions man/extend_if_comment.Rd

This file was deleted.

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

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

6 changes: 3 additions & 3 deletions man/update_indention_ref.Rd

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

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

5 changes: 0 additions & 5 deletions tests/testthat/test-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,3 @@ test_that("can lookup tokens", {
lookup_new_special()
})
})

test_that("can extend non-comment", {
pd <- compute_parse_data_nested(c("if (TRUE) # \n call(34)"))
expect_equal(extend_if_comment(pd$child[[1]], 4), 5)
})

0 comments on commit 6df0686

Please # to comment.