diff --git a/NEWS.md b/NEWS.md index 4dbff9603..c9ab7e303 100644 --- a/NEWS.md +++ b/NEWS.md @@ -26,6 +26,9 @@ editor_options: - `{` is not put on a new line after `=` and in `function() {` for some edge cases (#939). +- `while ({})` statements are now handled the same way as function statements + with regards to breaking lines (#967). + - Parsing of {roxygen2} example comments now also works for edge cases when there is no literal code immediately following after the end of the example section (#940). diff --git a/R/rules-line-breaks.R b/R/rules-line-breaks.R index ac2214f59..c9f5bfd96 100644 --- a/R/rules-line-breaks.R +++ b/R/rules-line-breaks.R @@ -65,8 +65,8 @@ set_line_break_before_curly_opening <- function(pd) { ~ next_terminal(pd[.x, ], vars = "token_after")$token_after ) != "'{'" last_expr_idx <- max(which(pd$token == "expr")) - is_last_expr <- ifelse(pd$token[1] == "IF", - # rule not applicable for IF + is_last_expr <- ifelse(pd$token[1] %in% c("IF", "WHILE"), + # rule not applicable for if and while TRUE, (line_break_to_set_idx + 1L) == last_expr_idx ) diff --git a/tests/testthat/line_breaks_and_other/braces-fun-calls2-in.R b/tests/testthat/line_breaks_and_other/braces-fun-calls2-in.R index 64741bb93..fe6003476 100644 --- a/tests/testthat/line_breaks_and_other/braces-fun-calls2-in.R +++ b/tests/testthat/line_breaks_and_other/braces-fun-calls2-in.R @@ -76,3 +76,29 @@ tetst( x }, 1 + +1 ) + +while ({ + x +}) { + f() +} + +while ({ + x +} +) { + f() +} + +while ( + { + x +}) { + f() +} + +while ( + {x +}) { + f() +} diff --git a/tests/testthat/line_breaks_and_other/braces-fun-calls2-out.R b/tests/testthat/line_breaks_and_other/braces-fun-calls2-out.R index 527a0d7c2..14e0fadc3 100644 --- a/tests/testthat/line_breaks_and_other/braces-fun-calls2-out.R +++ b/tests/testthat/line_breaks_and_other/braces-fun-calls2-out.R @@ -84,3 +84,27 @@ tetst( }, 1 + +1 ) + +while ({ + x +}) { + f() +} + +while ({ + x +}) { + f() +} + +while ({ + x +}) { + f() +} + +while ({ + x +}) { + f() +}