Skip to content

Indention based on square brackets #207

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 3 commits into from
Sep 19, 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
5 changes: 5 additions & 0 deletions R/expr-is.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ is_curly_expr <- function(pd) {
pd$token[1] == "'{'"
}

is_subset_expr <- function(pd) {
if (is.null(pd) || nrow(pd) == 1) return(FALSE)
pd$token[2] == "'['"
}

#' @describeIn pd_is Checks whether `pd` is a function call.
is_function_call <- function(pd) {
if (is.null(pd)) return(FALSE)
Expand Down
18 changes: 6 additions & 12 deletions R/modify_pd.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,18 @@
#' @name update_indention
NULL

#' @describeIn update_indention Inserts indention based on round brackets.
indent_round <- function(pd, indent_by) {
#' @describeIn update_indention Inserts indention based on round, square and
#' curly brackets.
indent_braces <- function(pd, indent_by) {
indent_indices <- compute_indent_indices(
pd, token_opening = "'('", token_closing = "')'"
pd,
token_opening = c("'('", "'['", "'{'"),
token_closing = c("')'", "']'", "'}'")
)
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_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,
Expand Down
4 changes: 2 additions & 2 deletions R/rules-line_break.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ set_line_break_after_opening_if_call_is_multi_line <-
function(pd,
except_token_after = NULL,
except_text_before = NULL) {
if (!is_function_call(pd)) return(pd)
if (!is_function_call(pd) && !is_subset_expr(pd)) return(pd)
npd <- nrow(pd)
seq_x <- seq2(3L, npd - 1L)
is_multi_line <- any(
Expand All @@ -92,7 +92,7 @@ set_line_break_after_opening_if_call_is_multi_line <-
#' @describeIn set_line_break_if_call_is_multi_line Sets line break before
#' closing parenthesis.
set_line_break_before_closing_call <- function(pd, except_token_before) {
if (!is_function_call(pd)) return(pd)
if (!is_function_call(pd) && !is_subset_expr(pd)) return(pd)
npd <- nrow(pd)
is_multi_line <- any(pd$lag_newlines[seq2(3L, npd - 1L)] > 0)
if (!is_multi_line) {
Expand Down
40 changes: 0 additions & 40 deletions R/serialized_tests.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,46 +151,6 @@ style_empty <- function(text) {
transformed_text
}

#' @describeIn test_transformer Transformations for indention based on curly
#' brackets only.
style_indent_curly <- function(text) {

transformers <- list(
# transformer functions
initialize = initialize_attributes,
line_break = NULL,
space = partial(indent_curly, indent_by = 2),
token = NULL,

# transformer options
use_raw_indention = FALSE,
NULL
)
transformed_text <- parse_transform_serialize(text, transformers)
transformed_text
}


#' @describeIn test_transformer Transformations for indention based on curly
#' brackets and round brackets.
style_indent_curly_round <- function(text) {
transformers <- list(
# transformer functions
initialize = initialize_attributes,
line_break = NULL,
space = c(partial(indent_curly, indent_by = 2),
partial(indent_round, indent_by = 2)),
token = NULL,

# transformer options
use_raw_indention = FALSE,
NULL
)

transformed_text <- parse_transform_serialize(text, transformers)
transformed_text
}

#' @describeIn test_transformer Transformations for indention based on operators
style_op <- function(text) {

Expand Down
3 changes: 1 addition & 2 deletions R/style_guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ tidyverse_style <- function(scope = "tokens",

space_manipulators <- if (scope >= "spaces")
lst(
partial(indent_round, indent_by = indent_by),
partial(indent_curly, indent_by = indent_by),
partial(indent_braces, 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),
Expand Down
12 changes: 0 additions & 12 deletions man/test_transformer.Rd

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

10 changes: 4 additions & 6 deletions man/update_indention.Rd

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

Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
{1 + 3}
{2 + sin(pi)}
{
1 + 3
}
{
2 + sin(pi)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
a <- function(x) {
x <- c(1,
2 + 3,
sin(pi)) # FIXME add tidyverse-comliant rule to break after '('
sin(pi))

if(x > 10) {
return("done")
Expand Down

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

Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
a <- function(x) {
x <- c(1,
x <- c(
1,
2 + 3,
sin(pi)) # FIXME add tidyverse-comliant rule to break after '('
sin(pi)
)

if(x > 10) {
if (x > 10) {
return("done")
}
}
4 changes: 3 additions & 1 deletion tests/testthat/indention_curly_brackets/one_line_curly-out.R
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
a <- {1+1}
a <- {
1 + 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ranges[tag == "non_literal" & str_detect(text, ";"),
text := str_replace_all(text, ";", "\n")]

fak[a, b]

fac[a,
b]
fac[
a,
b
]

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ranges[
tag == "non_literal" & str_detect(text, ";"),
text := str_replace_all(text, ";", "\n")
]

fak[a, b]

fac[
a,
b
]
fac[
a,
b
]
Loading