Skip to content

Don't reindent function calls #149

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 8 commits into from
Aug 23, 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
43 changes: 43 additions & 0 deletions R/rules-line_break.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,51 @@ remove_line_break_before_round_closing <- function(pd) {
pd
}

#' @importFrom rlang seq2
add_line_break_after_pipe <- function(pd) {
is_special <- pd$token %in% c("SPECIAL-PIPE")
pd$lag_newlines[lag(is_special)] <- 1L
pd
}


#' Set line break for multi-line function calls
#' @param pd A parse table.
#' @param except_token A character vector with tokens before "'('" that do not
#' @name set_line_break_if_call_is_multi_line
#' @importFrom rlang seq2
NULL

#' @describeIn set_line_break_if_call_is_multi_line Sets line break after
#' opening parenthesis.
set_line_break_after_opening_if_call_is_multi_line <-
function(pd,
except_token = NULL) {
if (!is_function_call(pd)) return(pd)
npd <- nrow(pd)
is_multi_line <- any(pd$lag_newlines[seq2(3, npd - 1)] > 0)
if (!is_multi_line) return(pd)
exception_pos <- which(pd$token %in% except_token)
pd$lag_newlines[setdiff(3, exception_pos)] <- 1L
pd
}


#' @describeIn set_line_break_if_call_is_multi_line Sets line break before
#' closing parenthesis.
set_line_break_before_closing_if_call_is_multi_line <- function(pd) {
if (!is_function_call(pd)) return(pd)
npd <- nrow(pd)
is_multi_line <- any(pd$lag_newlines[seq2(3, npd - 1)] > 0)
if (!is_multi_line) return(pd)
pd$lag_newlines[npd] <- 1L
pd
}

#' @rdname set_line_break_if_call_is_multi_line
remove_line_break_in_empty_fun_call <- function(pd) {
if (is_function_call(pd) && nrow(pd) == 3) {
pd$lag_newlines[3] <- 0L
}
pd
}
1 change: 1 addition & 0 deletions R/rules-spacing.R
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ remove_space_around_colons <- function(pd_flat) {
}

#' Set space between EQ_SUB and "','"
#' @param pd A parse table.
set_space_between_eq_sub_and_comma <- function(pd) {
op_before <- which(pd$token == "EQ_SUB" & lead(pd$token == "','"))
pd$spaces[op_before] <- 1L
Expand Down
8 changes: 7 additions & 1 deletion R/style_guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ tidyverse_style <- function(scope = "tokens",
add_line_break_afer_curly_opening,
if (strict) set_line_break_before_curly_closing else
add_line_break_before_curly_closing,
partial(
set_line_break_after_opening_if_call_is_multi_line,
except_token = "COMMENT"
),
set_line_break_before_closing_if_call_is_multi_line,
remove_line_break_in_empty_fun_call,
add_line_break_after_pipe
)

Expand All @@ -87,7 +93,7 @@ tidyverse_style <- function(scope = "tokens",
indention_modifier <-
c(
update_indention_ref_fun_dec,
update_indention_ref_fun_call
NULL
)

create_style_guide(
Expand Down
32 changes: 32 additions & 0 deletions man/set_line_break_if_call_is_multi_line.Rd

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

14 changes: 14 additions & 0 deletions man/set_space_between_eq_sub_and_comma.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,7 +1,7 @@
b<-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,7 +1,9 @@
b <- function(x) {
x <- c(1,
2 + 3,
sin(pi)) # FIXME add tidyverse-comliant rule to break after '('
x <- c(
1,
2 + 3,
sin(pi)
)

if (x > 10) {
return("done")
Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/indention_multiple/curly_and_round-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ test_that("this is a text", {
{
call(
12, 1 + 1,
26)
26
)
}
}))

Expand Down
6 changes: 4 additions & 2 deletions tests/testthat/indention_multiple/edge_mixed-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
(({
{
{
c(99,
c(
99,
1 + 1, {
"within that suff"
})
}
)
}
}
}))
Expand Down
6 changes: 4 additions & 2 deletions tests/testthat/indention_multiple/overall-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ a <- function(x) {
26 ^ 2, # FIXME ^ operator has to be surrounded by one space (or none?!), never multiple
8,
7
)))
))
)

call(
1, 2,
23 + Inf - 99, call(
16
))
)
)
}
# comments everywhere
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
c(call(2), 1, # choosed first function name (c) such that indention is 2.
c(call(2), 1, # comment
29
)

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,3 +1,4 @@
c(call(2), 1, # choosed first function name (c) such that indention is 2.
c(
call(2), 1, # comment
29
)
2 changes: 1 addition & 1 deletion tests/testthat/indention_operators/eq_assign-in.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ switch(engine,
},
new=(
2
) ) # FIXME: Add line break between parens (#125)
) )

{
a <-
Expand Down
21 changes: 10 additions & 11 deletions tests/testthat/indention_operators/eq_assign-in_tree

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

12 changes: 7 additions & 5 deletions tests/testthat/indention_operators/eq_assign-out.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
switch(engine,
pdftex = {
switch(
engine,
pdftex = {
if (any) {
x
}
},
new = (
2
)) # FIXME: Add line break between parens (#125)
new = (
2
)
)

{
a <-
Expand Down
6 changes: 4 additions & 2 deletions tests/testthat/indention_operators/not_first_trigger-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
) %>%
j()

a <- c(x, y,
z) %>%
a <- c(
x, y,
z
) %>%
k()

a + (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ call(call( # comment
3, 4
))

call(call(1, # comment
3
call(call(
1, # comment
3
))
Loading