Skip to content

Supporting more indention patterns #120

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 10 commits into from
Aug 13, 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
2 changes: 2 additions & 0 deletions R/get_transformers.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ get_transformers_flat <- function(strict = TRUE,
remove_space_before_comma,
remove_space_after_opening_paren,
remove_space_after_excl,
remove_space_before_dollar,
partial(start_comments_with_space,
force_one = start_comments_with_one_space),
NULL)
Expand Down Expand Up @@ -85,6 +86,7 @@ get_transformers_nested <- function(
partial(indent_round, indent_by = indent_by),
partial(indent_curly, indent_by = indent_by),
partial(indent_op, indent_by = indent_by),
partial(indent_eq_sub, indent_by = indent_by),
partial(indent_without_paren, indent_by = indent_by),
get_transformers_flat(strict, start_comments_with_one_space),
remove_space_after_unary_pm_nested,
Expand Down
26 changes: 21 additions & 5 deletions R/modify_pd.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,31 @@ indent_curly <- function(pd, indent_by) {
set_unindention_child(pd, token = "'}'", unindent_by = indent_by)
}

#' @rdname update_indention
indent_op <- function(pd, indent_by, token = c(math_token,
"SPECIAL-PIPE",
"LEFT_ASSIGN")) {
#' @describeIn update_indention Indents operatos
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)
pd$indent[indent_indices] <- pd$indent[indent_indices] + indent_by
pd
}

#' @describeIn update_indention Upates indention for token EQ_SUB. Only differs
#' from ident_op in the sense that the last token on the talbe where EQ_SUB
#' occurs is not indented (see[compute_indent_indices()])
indent_eq_sub <- function(pd,
indent_by,
token = "EQ_SUB") {
indent_indices <- compute_indent_indices(pd, token, indent_last = FALSE)
pd$indent[indent_indices] <- pd$indent[indent_indices] + indent_by
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) {
Expand Down Expand Up @@ -117,7 +133,7 @@ token_is_multi_line <- function(pd) {
#' (R/rules-line_break.R) but we need `newlines` to determine
#' whether or not to set `spaces` (R/rules-spacing.R), we have to update the
#' attribute. We cannot simply use `dplyr::lead(pd$lag_newlines)` since we would
#' loose information for the last token. `spaces` is left as is in
#' lose information for the last token. `spaces` is left as is in
#' R/rules-spacing.R for tokens at the end of a line since this allows styling
#' without touching indention.
#' @param pd A parse table.
Expand Down
4 changes: 2 additions & 2 deletions R/relevel.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ flatten_operators <- function(pd_nested) {
#' @param pd_nested A nested parse table.
#' @include token.R
flatten_operators_one <- function(pd_nested) {
pd_token_left <- c(special_token, math_token)
pd_token_right <- c(special_token, left_assignment_token, "'+'", "'-'")
pd_token_left <- c(special_token, math_token, "'$'")
pd_token_right <- c(special_token, "LEFT_ASSIGN", "'+'", "'-'")
bound <- pd_nested %>%
flatten_pd(pd_token_left, left = TRUE) %>%
flatten_pd(pd_token_right, left = FALSE)
Expand Down
8 changes: 8 additions & 0 deletions R/rules-spacing.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,11 @@ remove_space_after_excl <- function(pd_flat) {
pd_flat$spaces[excl] <- 0L
pd_flat
}


remove_space_before_dollar <- function(pd_flat) {
dollar_after <- (pd_flat$token == "'$'") & (pd_flat$lag_newlines == 0L)
dollar_before <- lead(dollar_after, default = FALSE)
pd_flat$spaces[dollar_before] <- 0L
pd_flat
}
1 change: 1 addition & 0 deletions R/token.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ token <- tribble(
">=" , "logical" , "GE",
"!=" , "logical" , "NE",
"==" , "logical" , "EQ",
"=" , "assign_left" , "EQ_SUB",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between "EQ_SUB" and "EQ_ASSIGN"? (Just curious.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data_frame(a = 3))
# vs.
a = 3

"=" , "assign_left" , "EQ_ASSIGN",
"<-" , "assign_left" , "LEFT_ASSIGN",
"->" , "assign_right", "RIGHT_ASSIGN",
Expand Down
12 changes: 11 additions & 1 deletion man/update_indention.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/update_newlines.Rd

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

Loading