Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Remove excessive line breaks between top-level expressions #1239

Merged
merged 17 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# All available hooks: https://pre-commit.com/hooks.html
# R specific hooks: https://github.com/lorenzwalthert/precommit
default_stages: [commit]
default_stages: [pre-commit]
default_language_version:
python: python3

repos:
- repo: https://github.com/lorenzwalthert/precommit
rev: v0.4.2
rev: v0.4.3.9003
hooks:
- id: style-files
args:
Expand Down
25 changes: 25 additions & 0 deletions R/rules-line-breaks.R
Original file line number Diff line number Diff line change
Expand Up @@ -447,3 +447,28 @@ remove_empty_lines_after_opening_and_before_closing_braces <- function(pd) {

pd
}


#' Reduce multiple blank lines to a maximum number of allowed blank lines
#' @param pd_flat A flat parse table.
#' @param allowed_blank_lines The maximum number of allowed blank lines between code elements. Default is `2L`.
#' @keywords internal
reduce_extra_blank_lines_between_scopes <- function(pd_flat, allowed_blank_lines = 2L) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Style guide recommends one line breaks, but only for function calls. But that itself felt quite aggressive, given the huge numbers of changes in test outputs.

2L nicely aligns with Python and feels "natural" as well.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Lets limit to two blank lines as towards the style guide and decide later if we'll go all the way to one line break. Also, can you please apply the 2L limit to strict = TRUE only?

# Calculate the maximum allowed lag_newlines
IndrajeetPatil marked this conversation as resolved.
Show resolved Hide resolved
max_lag_newlines <- allowed_blank_lines + 1L # +1 accounts for the line with the previous token

# Create a copy of lag_newlines to track modifications
modified_lag_newlines <- pd_flat$lag_newlines

# Iterate through the dataframe to reduce consecutive blank lines
for (i in seq_along(modified_lag_newlines)) {
IndrajeetPatil marked this conversation as resolved.
Show resolved Hide resolved
if (modified_lag_newlines[i] > max_lag_newlines) {
modified_lag_newlines[i] <- max_lag_newlines
}
}

# Update the original data frame
pd_flat$lag_newlines <- modified_lag_newlines
IndrajeetPatil marked this conversation as resolved.
Show resolved Hide resolved

return(pd_flat)
IndrajeetPatil marked this conversation as resolved.
Show resolved Hide resolved
}
1 change: 1 addition & 0 deletions R/style-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ tidyverse_style <- function(scope = "tokens",
if (strict) remove_line_break_before_round_closing_after_curly,
remove_line_breaks_in_fun_dec =
if (strict) remove_line_breaks_in_fun_dec,
reduce_extra_blank_lines_between_scopes = reduce_extra_blank_lines_between_scopes,
style_line_break_around_curly = partial(
style_line_break_around_curly,
strict
Expand Down
17 changes: 17 additions & 0 deletions man/reduce_extra_blank_lines_between_scopes.Rd

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

1 change: 0 additions & 1 deletion tests/testthat/alignment/named-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ call(
)



# if all col1 arguments are named, col1 must also be aligned
# not aligned
fell(
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/indention_curly_brackets/custom-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ if (value > 0) {
}



if (value > 0) {
print(value)
}
2 changes: 0 additions & 2 deletions tests/testthat/line_breaks_and_other/assignment-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ x <-
3



ImportantDataFrame$ImportantColumn1 <-
ImportantDataFrame$ImportantColumn2 <-
ComplicatedFunction(ImportantDataFrame$InputColumn)
Expand All @@ -32,6 +31,5 @@ ImportantDataFrame$ImportantColumn1 <-
ImportantDataFrame$ImportantColumn2 <- ComplicatedFunction(ImportantDataFrame$InputColumn)



ImportantDataFrame$ImportantColumn1 <-
ImportantDataFrame$ImportantColumn2 <- ComplicatedFunction(ImportantDataFrame$InputColumn)
9 changes: 9 additions & 0 deletions tests/testthat/line_breaks_and_other/curly-in.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,12 @@ while (TRUE){

while (TRUE){#
}


for (i in 1:10) {}





while (TRUE) {}
6 changes: 6 additions & 0 deletions tests/testthat/line_breaks_and_other/curly-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,9 @@ while (TRUE) {

while (TRUE) { #
}


for (i in 1:10) {}


while (TRUE) {}
9 changes: 9 additions & 0 deletions tests/testthat/line_breaks_and_other/pipe_and_comment-in.R
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
1:10 %>% # sum
sum()


f %>% g()





h %>% i()
6 changes: 6 additions & 0 deletions tests/testthat/line_breaks_and_other/pipe_and_comment-out.R
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
1:10 %>% # sum
sum()


f %>% g()


h %>% i()
4 changes: 4 additions & 0 deletions tests/testthat/line_breaks_fun_call/blank-non-strict-in.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ call(
1
)





call(
x = 2,
1,
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/line_breaks_fun_call/blank-non-strict-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ call(
1
)


call(
x = 2,
1,
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/line_breaks_fun_call/blank-strict-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ call(
2,



# comment

1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ switch( #
)



switch(x,
a = 2, #
y = 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ call(call(
))



# no more barcket on same line ->
call(call(
3, 4
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/parse_comments/with_indention-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ call(
# new comment




a()
# I think it gets boring
# new_line here
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/roxygen-examples-complete/13-empty-lines-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#' # two
#'
#'
#'
#'
#' (
#' # more
#' a <- 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ NULL
NULL



#' this
#'
#' empty line after example
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#' Empty line in examples
#'
#' @examples
1






#' Empty line in examples
#'
#' @examples
#' \dontrun{
#' }
2
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#' Empty line in examples
#'
#' @examples
1


#' Empty line in examples
#'
#' @examples
#' \dontrun{
#'
#' }
2
5 changes: 5 additions & 0 deletions tests/testthat/test-roxygen-examples-complete-30.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# NB: DO NOT EDIT. Auto-generated by ./tests/dev/generate_roxygen_tests.R.

test_that("analogous to test-roxygen-examples-complete: 30", {
expect_warning(test_collection("roxygen-examples-complete", "^30-", transformer = style_text), NA)
})
1 change: 1 addition & 0 deletions tests/testthat/test-transformers-drop.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ test_that("tidyverse transformers are correctly dropped", {

names_line_break <- c(
"remove_empty_lines_after_opening_and_before_closing_braces",
"reduce_extra_blank_lines_between_scopes",
"set_line_break_around_comma_and_or",
"set_line_break_after_assignment",
"set_line_break_after_opening_if_call_is_multi_line",
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/token_adding_removing/mixed_token-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ d
'text with "quotes"'



# adding brackets in pipes
a %>%
b() %>%
Expand Down
Loading