Skip to content
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

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 9 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
11 changes: 11 additions & 0 deletions R/rules-line-breaks.R
Original file line number Diff line number Diff line change
Expand Up @@ -447,3 +447,14 @@ remove_empty_lines_after_opening_and_before_closing_braces <- function(pd) {

pd
}


reduce_extra_blank_lines_between_scopes <- function(pd, allowed_blank_lines = 2L) {
# Calculate the maximum allowed lag_newlines
max_lag_newlines <- allowed_blank_lines + 1L # +1 accounts for the line with the previous token

# cap lag_newlines at max_lag_newlines
pd$lag_newlines <- pmin(pd$lag_newlines, max_lag_newlines)

pd
}
2 changes: 2 additions & 0 deletions R/style-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ 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 =
if (strict) reduce_extra_blank_lines_between_scopes,
style_line_break_around_curly = partial(
style_line_break_around_curly,
strict
Expand Down
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) {}
1 change: 1 addition & 0 deletions tests/testthat/line_breaks_and_other/ggplot2-in.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) +
ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) + ggplot2::geom_point() + g() # comment



# add when comment
ggplot(data = mtcars, mapping = aes(x = mpg, y = vs)) +
ggplot2::geom_point() + g() + geom_oint() # comment
Expand Down
20 changes: 20 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,22 @@
1:10 %>% # sum
sum()


f %>% g()





h %>% i()





# some comment




j %>% k()
12 changes: 12 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,14 @@
1:10 %>% # sum
sum()


f %>% g()


h %>% i()


# some comment


j %>% k()
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
4 changes: 4 additions & 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,10 @@ 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