Skip to content

Fix indention in if-else statement #200

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

Merged
merged 4 commits into from
Sep 17, 2017
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
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Collate:
'addins.R'
'compat-tidyr.R'
'dplyr.R'
'expr-is.R'
'initialize.R'
'modify_pd.R'
'nested.R'
Expand Down
19 changes: 19 additions & 0 deletions R/expr-is.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#' Check whether a parse table corresponds to a a certain expression
#'
#' @param pd A parse table.
#' @name pd_is
NULL

#' @describeIn pd_is Checks whether `pd` contains an expression wrapped in
#' curly brackets.
is_curly_expr <- function(pd) {
if (is.null(pd)) return(FALSE)
pd$token[1] == "'{'"
}

#' @describeIn pd_is Checks whether `pd` is a function call.
is_function_call <- function(pd) {
if (is.null(pd)) return(FALSE)
if (is.na(pd$token_before[2])) return(FALSE)
pd$token_before[2] == "SYMBOL_FUNCTION_CALL"
}
90 changes: 69 additions & 21 deletions R/modify_pd.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,33 @@ NULL

#' @describeIn update_indention Inserts indention based on round brackets.
indent_round <- function(pd, indent_by) {
indent_indices <- compute_indent_indices(pd, token = "'('")
indent_indices <- compute_indent_indices(
pd, token_opening = "'('", token_closing = "')'"
)
pd$indent[indent_indices] <- pd$indent[indent_indices] + indent_by
set_unindention_child(pd, token = "')'", unindent_by = indent_by)
}

#' @rdname update_indention
indent_curly <- function(pd, indent_by) {
indent_indices <- compute_indent_indices(pd, token = "'{'")
indent_indices <- compute_indent_indices(
pd, token_opening = "'{'", token_closing = "'}'"
)
pd$indent[indent_indices] <- pd$indent[indent_indices] + indent_by
set_unindention_child(pd, token = "'}'", unindent_by = indent_by)
}

#' @describeIn update_indention Indents operators
indent_op <- function(pd,
indent_by,
token = c(math_token,
logical_token,
special_token,
"LEFT_ASSIGN",
"'$'")) {
indent_indices <- compute_indent_indices(pd, token, indent_last = TRUE)
token = c(
math_token,
logical_token,
special_token,
"LEFT_ASSIGN",
"'$'")
) {
indent_indices <- compute_indent_indices(pd, token)
pd$indent[indent_indices] <- pd$indent[indent_indices] + indent_by
pd
}
Expand All @@ -52,41 +58,83 @@ indent_eq_sub <- function(pd,
#' @describeIn update_indention Same as indent_op, but only indents one token
#' after `token`, not all remaining.
indent_assign <- function(pd, indent_by, token = NULL) {
indent_indices <- compute_indent_indices(pd, token, indent_last = TRUE)
indent_indices <- compute_indent_indices(pd, token)
pd$indent[indent_indices] <- pd$indent[indent_indices] + indent_by
pd
}

#' @describeIn update_indention Is used to indent if / while / for statements
#' that do not have curly brackets.
#' @describeIn update_indention Is used to indent for / while / if / if-else
#' statements that do not have curly parenthesis.
indent_without_paren <- function(pd, indent_by = 2) {
pd %>%
indent_without_paren_for_while(indent_by) %>%
indent_without_paren_if_else(indent_by)
}

#' @describeIn update_indention Is used to indent for and while statements.
indent_without_paren_for_while <- function(pd, indent_by) {
nrow <- nrow(pd)
if (!(pd$token[1] %in% c("IF", "FOR", "WHILE"))) return(pd)
if (!(pd$token[1] %in% c("FOR", "WHILE"))) return(pd)
if (pd$lag_newlines[nrow] == 0) return(pd)
pd$indent[nrow] <- indent_by
pd
}

#' @describeIn update_indention Is used to indent if and if-else statements.
#' @importFrom rlang seq2
indent_without_paren_if_else <- function(pd, indent_by) {
has_if_without_curly <-
pd$token[1] %in% c("IF", "WHILE") && pd$child[[5]]$token[1] != "'{'"
if (has_if_without_curly) {
pd$indent[5] <- indent_by
}

has_else_without_curly_or_else_chid <-
any(pd$token == "ELSE") &&
pd$child[[7]]$token[1] != "'{'" &&
pd$child[[7]]$token[1] != "IF"
if (has_else_without_curly_or_else_chid) {
pd$indent[seq(7, nrow(pd))] <- indent_by
}
pd
}

#' Compute the indices that need indention
#'
#' Based on `token`, find the rows in `pd` that need to be indented.
#' @param pd A parse table.
#' @param token A character vector with tokens.
#' @param indent_last Flag to indicate whether the last token in `pd` should
#' be indented or not. See 'Details'.
#' @param token_opening A character vector with tokens that could induce
#' indention for subsequent tokens.
#' @param token_closing A character vector with tokens that could terminate
#' indention for previous tokens. If `NULL` (the default), indention should
#' end with the last token in the parse table.
#' @details
#' For example when `token` is a parenthesis, the closing parenthesis does not
#' need indention, but if token is something else, for example a plus (+), the
#' last token in `pd` needs indention.
#' Two cases are fundamentally different:
#'
#' * Indention based on operators (e.g '+'), where all subsequent tokens should
#' be indented.
#' * Indention based on braces (e.g. '('), where just the tokens between the
#' opening and the closing brace have to be indented.
#'
#' To cover the second case, we need `token_closing` because it cannot be taken
#' for granted that `token_closing` is always the last token in `pd`. For
#' example in if-else expressions, this is not the case and indenting
#' everything between '(' and the penultimate token would result in the wrong
#' formatting.
#' @importFrom rlang seq2
compute_indent_indices <- function(pd, token = "'('", indent_last = FALSE) {
compute_indent_indices <- function(pd,
token_opening,
token_closing = NULL) {
npd <- nrow(pd)
potential_triggers <- which(pd$token %in% token)
potential_triggers <- which(pd$token %in% token_opening)
needs_indention <- needs_indention(pd, potential_triggers)
trigger <- potential_triggers[needs_indention][1]
if (is.na(trigger)) return(numeric(0))
start <- trigger + 1
stop <- npd - ifelse(indent_last, 0, 1)
stop <- ifelse(is.null(token_closing),
npd,
which(pd$token %in% token_closing) - 1)

seq2(start, stop)
}

Expand Down
21 changes: 0 additions & 21 deletions R/reindent.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,6 @@ update_indention_ref_fun_dec <- function(pd_nested) {
pd_nested
}

#' Check whether a parse table corresponds to a a certain expression
#'
#' @param pd A parse table.
#' @name pd_is
NULL

#' @describeIn pd_is Checks whether `pd` contains an expression wrapped in
#' curly brackets.
is_curly_expr <- function(pd) {
if (is.null(pd)) return(FALSE)
pd$token[1] == "'{'"
}

#' @describeIn pd_is Checks whether `pd` is a function call.
is_function_call <- function(pd) {
if (is.null(pd)) return(FALSE)
if (is.na(pd$token_before[2])) return(FALSE)
pd$token_before[2] == "SYMBOL_FUNCTION_CALL"
}


#' Apply reference indention to tokens
#'
#' Applies the reference indention created with functions
Expand Down
26 changes: 19 additions & 7 deletions man/compute_indent_indices.Rd

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

2 changes: 1 addition & 1 deletion man/pd_is.Rd

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

14 changes: 12 additions & 2 deletions man/update_indention.Rd

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

29 changes: 29 additions & 0 deletions tests/testthat/indention_multiple/if_else_curly-in.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
if (TRUE)
3
else
4
}

{
if (TRUE) {
3
} else
4
}

{
if (TRUE)
3
else {
4
}
}

{
if (TRUE) {
3
} else {
4
}
}
69 changes: 69 additions & 0 deletions tests/testthat/indention_multiple/if_else_curly-in_tree

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

Loading