Skip to content

Commit

Permalink
whenver ther is an empty brace expression, remove line breaks
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzwalthert committed Oct 8, 2024
1 parent 9d076fc commit e1455c6
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 66 deletions.
24 changes: 15 additions & 9 deletions R/rules-line-breaks.R
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,21 @@ set_line_break_around_comma_and_or <- function(pd, strict) {
}

style_line_break_around_curly <- function(strict, pd) {
if (is_curly_expr(pd) && nrow(pd) > 2L) {
closing_before <- pd$token == "'}'"
opening_before <- (pd$token == "'{'")
to_break <- lag(opening_before, default = FALSE) | closing_before
pd$lag_newlines[to_break] <- ifelse(
pd$token[to_break] == "COMMENT",
pmin(1L, pd$lag_newlines[to_break]),
if (strict) 1L else pmax(1L, pd$lag_newlines[to_break])
)
if (is_curly_expr(pd)) {
n_row <- nrow(pd)
if (n_row > 2L) {
closing_before <- pd$token == "'}'"
opening_before <- (pd$token == "'{'")
to_break <- lag(opening_before, default = FALSE) | closing_before
pd$lag_newlines[to_break] <- ifelse(
pd$token[to_break] == "COMMENT",
pmin(1L, pd$lag_newlines[to_break]),
if (strict) 1L else pmax(1L, pd$lag_newlines[to_break])
)
} else if (n_row == 2L) {
# pd represents {}
pd$lag_newlines[2L] <- 0L
}
} else {
is_else <- pd$token == "ELSE"
if (any(pd$token_before[is_else] == "'}'")) {
Expand Down
3 changes: 1 addition & 2 deletions tests/testthat/indention_multiple/edge_strict_mixed-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@
)))


function(x, y, z) {
}
function(x, y, z) {}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,4 @@ function(a =
f =
d, c =
3, d =
4) {

}
4) {}
28 changes: 7 additions & 21 deletions tests/testthat/line_breaks_and_other/braces-fun-calls2-out.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
test(
"x",
{

},
{},
a + b,
{
s(x = sd)
Expand All @@ -11,9 +9,7 @@ test(

test(
"x",
{

},
{},
a + b,
{
s(x = sd)
Expand All @@ -22,9 +18,7 @@ test(

test(
"x",
{

},
{},
a + b,
{
s(x = sd)
Expand All @@ -34,9 +28,7 @@ test(

test(
"x",
{

},
{},
a + b,
{
s(x = sd)
Expand All @@ -45,9 +37,7 @@ test(

test(
"x",
{

}, # h
{}, # h
a + b,
{
s(x = sd)
Expand All @@ -56,9 +46,7 @@ test(

test(
"x",
{

}, # h
{}, # h
a + b,
# k
{
Expand All @@ -68,9 +56,7 @@ test(

test(
"x",
{

},
{},
a + b, # k
{
s(x = sd)
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/line_breaks_and_other/curly-in.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,15 @@ while (TRUE) { }

while (TRUE)
{ }

while (TRUE){

}

while (TRUE){
#
}


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

while (TRUE) {}

while (TRUE) {}

while (TRUE) {
#
}


while (TRUE) { #
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ call(call(
call(call(1,
2))
# multi-line: no indention based on first vall
call(a(b(c({
}))))
call(a(b(c({}))))

call(call(
2),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ call(call(
2
))
# multi-line: no indention based on first vall
call(a(b(c({
}))))
call(a(b(c({}))))

call(
call(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# A comment
a <- function() {

}
a <- function() {}

#+ chunk-label, opt1=value1
"chunk-content"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# random
this(is_a_call(x))
if (x) {
}
if (x) {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# random
this(is_a_call(x))
if (x) {
}
if (x) {}
6 changes: 2 additions & 4 deletions tests/testthat/spacing/round_curly-out.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
a <- function(x) {
}
a <- function(x) {}

if (a) {
3
Expand All @@ -13,5 +12,4 @@ if (x) {
y
} else if (x) {
x
} else {
}
} else {}
12 changes: 4 additions & 8 deletions tests/testthat/strict/non_strict-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,13 @@ test <- function() {
call
})

braced("unnamed reduces space", {
})
braced("unnamed reduces space", {})

braced("unnamed adds space space", {
})
braced("unnamed adds space space", {})

braced(named_reduces_space = {
})
braced(named_reduces_space = {})

braced(named_adds_space = {
})
braced(named_adds_space = {})

braced({
empty_removes_space
Expand Down
12 changes: 4 additions & 8 deletions tests/testthat/strict/strict-out.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,13 @@ test <- function() {
call
})

braced("unnamed reduces space", {
})
braced("unnamed reduces space", {})

braced("unnamed adds space space", {
})
braced("unnamed adds space space", {})

braced(named_reduces_space = {
})
braced(named_reduces_space = {})

braced(named_adds_space = {
})
braced(named_adds_space = {})

braced({
empty_removes_space
Expand Down

0 comments on commit e1455c6

Please sign in to comment.