Skip to content

Commit 2530f99

Browse files
Add rule to reduce blank lines between scopes
1 parent fa9d9f4 commit 2530f99

File tree

13 files changed

+20
-13
lines changed

13 files changed

+20
-13
lines changed

R/rules-line-breaks.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,3 +447,21 @@ remove_empty_lines_after_opening_and_before_closing_braces <- function(pd) {
447447

448448
pd
449449
}
450+
451+
452+
#' Reduce multiple blank lines to a maximum number of allowed blank lines
453+
#' @param pd_flat A flat parse table.
454+
#' @param allowed_blank_lines The maximum number of allowed blank lines between code elements. Default is `2L`.
455+
#' @keywords internal
456+
reduce_extra_blank_lines_between_scopes <- function(pd_flat, allowed_blank_lines = 2L) {
457+
# Calculate the maximum allowed lag_newlines
458+
max_lag_newlines <- allowed_blank_lines + 1L # +1 accounts for the line with the previous token
459+
460+
# Identify positions where lag_newlines exceed the maximum allowed
461+
idx <- which(pd_flat$lag_newlines > max_lag_newlines)
462+
463+
# Reduce lag_newlines to the maximum allowed at those positions
464+
pd_flat$lag_newlines[idx] <- max_lag_newlines
465+
466+
return(pd_flat)
467+
}

R/style-guides.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ tidyverse_style <- function(scope = "tokens",
139139
if (strict) remove_line_break_before_round_closing_after_curly,
140140
remove_line_breaks_in_fun_dec =
141141
if (strict) remove_line_breaks_in_fun_dec,
142+
reduce_extra_blank_lines_between_scopes = reduce_extra_blank_lines_between_scopes,
142143
style_line_break_around_curly = partial(
143144
style_line_break_around_curly,
144145
strict

tests/testthat/alignment/named-out.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ call(
102102
)
103103

104104

105-
106105
# if all col1 arguments are named, col1 must also be aligned
107106
# not aligned
108107
fell(

tests/testthat/indention_curly_brackets/custom-out.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ if (value > 0) {
44
}
55

66

7-
87
if (value > 0) {
98
print(value)
109
}

tests/testthat/line_breaks_and_other/assignment-out.R

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ x <-
2222
3
2323

2424

25-
2625
ImportantDataFrame$ImportantColumn1 <-
2726
ImportantDataFrame$ImportantColumn2 <-
2827
ComplicatedFunction(ImportantDataFrame$InputColumn)
@@ -32,6 +31,5 @@ ImportantDataFrame$ImportantColumn1 <-
3231
ImportantDataFrame$ImportantColumn2 <- ComplicatedFunction(ImportantDataFrame$InputColumn)
3332

3433

35-
3634
ImportantDataFrame$ImportantColumn1 <-
3735
ImportantDataFrame$ImportantColumn2 <- ComplicatedFunction(ImportantDataFrame$InputColumn)

tests/testthat/line_breaks_fun_call/blank-strict-out.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ call(
1919
2,
2020

2121

22-
2322
# comment
2423

2524
1,

tests/testthat/line_breaks_fun_call/switch_ifelse_etc_no_line_break-out.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ switch( #
1616
)
1717

1818

19-
2019
switch(x,
2120
a = 2, #
2221
y = 3

tests/testthat/line_breaks_fun_call/token_dependent_mixed-out.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ call(call(
1616
))
1717

1818

19-
2019
# no more barcket on same line ->
2120
call(call(
2221
3, 4

tests/testthat/parse_comments/with_indention-out.R

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ call(
1818
# new comment
1919

2020

21-
22-
2321
a()
2422
# I think it gets boring
2523
# new_line here

tests/testthat/roxygen-examples-complete/13-empty-lines-out.R

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
#' # two
1010
#'
1111
#'
12-
#'
13-
#'
1412
#' (
1513
#' # more
1614
#' a <- 3

0 commit comments

Comments
 (0)