From f808f1cc10d388abec09b35e18e52185bda73d83 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Tue, 27 Dec 2022 16:02:04 +0100 Subject: [PATCH 1/2] never remove line breaks when it moves code behind a comment --- R/rules-line-breaks.R | 4 ++-- tests/testthat/curly-curly/mixed-in.R | 14 ++++++++++++++ tests/testthat/curly-curly/mixed-out.R | 13 +++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/R/rules-line-breaks.R b/R/rules-line-breaks.R index ee1344d08..9475dad08 100644 --- a/R/rules-line-breaks.R +++ b/R/rules-line-breaks.R @@ -211,8 +211,8 @@ set_line_break_around_curly_curly <- function(pd) { closing_before <- (pd$token == "'}'") & (pd$token_after == "'}'" | pd$token_before == "'}'") if (any(opening_before) && any(closing_before)) { - pd$lag_newlines[lag(opening_before, default = FALSE)] <- 0L - pd$lag_newlines[closing_before] <- 0L + pd$lag_newlines[lag(opening_before, default = FALSE) & pd$token != "COMMENT"] <- 0L + pd$lag_newlines[closing_before & pd$token_before != "COMMENT"] <- 0L } } pd diff --git a/tests/testthat/curly-curly/mixed-in.R b/tests/testthat/curly-curly/mixed-in.R index 727c2ef89..e99eb810c 100644 --- a/tests/testthat/curly-curly/mixed-in.R +++ b/tests/testthat/curly-curly/mixed-in.R @@ -98,3 +98,17 @@ call( {{ x }}, {{ y}} := 3, f(bk) ) + +call({{ + # + 1 +}}) + +call({{ + # +}}) + + +{{ + # +}} diff --git a/tests/testthat/curly-curly/mixed-out.R b/tests/testthat/curly-curly/mixed-out.R index aa1760b4e..b0553e7b8 100644 --- a/tests/testthat/curly-curly/mixed-out.R +++ b/tests/testthat/curly-curly/mixed-out.R @@ -97,3 +97,16 @@ call( {{ x }}, {{ y }} := 3, f(bk) ) + +call({{ + # + 1 }}) + +call({{ + # +}}) + + +{{ + # +}} From 22119aca237e4999ee6bd45592621e7763eab818 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Tue, 27 Dec 2022 21:48:15 +0100 Subject: [PATCH 2/2] symmetry for opening and closing --- R/rules-line-breaks.R | 8 ++++++-- tests/testthat/curly-curly/mixed-out.R | 3 ++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/R/rules-line-breaks.R b/R/rules-line-breaks.R index 9475dad08..32177bfdc 100644 --- a/R/rules-line-breaks.R +++ b/R/rules-line-breaks.R @@ -211,8 +211,12 @@ set_line_break_around_curly_curly <- function(pd) { closing_before <- (pd$token == "'}'") & (pd$token_after == "'}'" | pd$token_before == "'}'") if (any(opening_before) && any(closing_before)) { - pd$lag_newlines[lag(opening_before, default = FALSE) & pd$token != "COMMENT"] <- 0L - pd$lag_newlines[closing_before & pd$token_before != "COMMENT"] <- 0L + pos_opening_idx <- lag(opening_before, default = FALSE) & pd$token != "COMMENT" + pd$lag_newlines[pos_opening_idx] <- 0L + if (any(pos_opening_idx)) { + # if line is broken with opening `{`, also break it with closing + pd$lag_newlines[closing_before & pd$token_after != "COMMENT"] <- 0L + } } } pd diff --git a/tests/testthat/curly-curly/mixed-out.R b/tests/testthat/curly-curly/mixed-out.R index b0553e7b8..a18134391 100644 --- a/tests/testthat/curly-curly/mixed-out.R +++ b/tests/testthat/curly-curly/mixed-out.R @@ -100,7 +100,8 @@ call( call({{ # - 1 }}) + 1 +}}) call({{ #