-
Notifications
You must be signed in to change notification settings - Fork 73
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
Changes from 2 commits
2ed63fa
01256e7
2d34511
48f6866
44b50c4
6e8b482
cd805de
ae7c748
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 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) | ||
if (npd == 3) { | ||
pd$lag_newlines[3] <- 0L | ||
return(pd) | ||
} | ||
is_multi_line <- any(pd$lag_newlines[seq2(3, npd - 1)] > 0) | ||
if (!is_multi_line) return(pd) | ||
exeption_pos <- which(pd$token %in% except_token) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exept -> except |
||
pd$lag_newlines[setdiff(3, exeption_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) | ||
if (npd == 3) { | ||
pd$lag_newlines[3] <- 0L | ||
return(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 | ||
} |
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.
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 |
---|---|---|
|
@@ -6,7 +6,8 @@ test_that("this is a text", { | |
{ | ||
call( | ||
12, 1 + 1, | ||
26) | ||
26 | ||
) | ||
} | ||
})) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,12 @@ | |
(({ | ||
{ | ||
{ | ||
c(99, | ||
c( | ||
99, | ||
1 + 1, { | ||
"within that suff" | ||
}) | ||
} | ||
) | ||
} | ||
} | ||
})) | ||
|
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 | ||
) |
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 | ||
) | ||
) # FIXME: Add line break between parens (#125) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we still need this FIXME? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope. |
||
|
||
{ | ||
a <- | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,10 @@ | |
) %>% | ||
j() | ||
|
||
a <- c(x, y, | ||
z) %>% | ||
a <- c( | ||
x, y, | ||
z | ||
) %>% | ||
k() | ||
|
||
a + ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ call(call( # comment | |
3, 4 | ||
)) | ||
|
||
call(call(1, # comment | ||
3 | ||
call(call( | ||
1, # comment | ||
3 | ||
)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,30 +2,39 @@ call(call( | |
2 | ||
)) | ||
|
||
call(call(1, | ||
2)) | ||
call(call( | ||
1, | ||
2 | ||
)) | ||
# multi-line: no indention based on first vall | ||
call(a(b(c({ | ||
})))) | ||
|
||
call( | ||
call( | ||
2 | ||
), | ||
5 | ||
) | ||
|
||
|
||
call(call( | ||
2), | ||
5) | ||
|
||
|
||
call(call(1, | ||
2, c( | ||
3 | ||
))) | ||
|
||
call(1, | ||
call2(3, 4, call(3, | ||
4, call(5, 6, call( | ||
2 | ||
) | ||
) | ||
) | ||
) | ||
1, | ||
2, c( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We seem to need a rule that requires a newline before multi-line function arguments. Do we have a self-contained writeup for the new function call rules? Maybe open a PR to the tidystyle repo so that it's not lost? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, we don't have such a rule yet. So if I understand you correctly, you think call(
2,
3, c(
2,
3
)) Should be styled as call(
3,
3,
c(
very_long_such,
that_we_cant_put_it_on_one_line
)
) If we can't put the whole sub-expression There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. |
||
3 | ||
) | ||
)) | ||
|
||
call( | ||
1, | ||
call2(3, 4, call( | ||
3, | ||
4, call(5, 6, call( | ||
2 | ||
) | ||
) | ||
) | ||
) | ||
) | ||
|
||
# comment lala | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this shortcut important?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because
seq2(3, npd - 1)
would throw an error otherwise.npd = 3
is the case of empty function calls likecall()
. Just making sure there this does not happen:We could circumvent that using
seq2(3, npd)
instead, can't actually remember why I did not do that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uups, no error then, sorry. Buth by dropping the if condition, we cannot solve the problem because if
npd = 3
, andwe want to remove the line break before the closing parent, which we can't using
seq2(a, b)
witha>b
. Also, my suggestion to useseq(3, npd)
does not make sense sincecall(2 )
would then be classified as multi-line, which I think does not make sense. We should not take the closing parenthesis into account. What do you think? Not sure whether there is another way to do that then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks clearer to me to remove the shortcut and add a separate rule to remove new lines for empty calls. This new rule seems to be very low priority.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, sure.