From a037a050d75c0c03a4d37344490ff77b3a85618b Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Sun, 27 Aug 2017 13:43:24 +0200 Subject: [PATCH] no line breaks after pipe if comment is next token --- R/rules-line_break.R | 2 +- .../line_breaks_and_other/pipe_and_comment-in.R | 2 ++ .../pipe_and_comment-in_tree | 15 +++++++++++++++ .../line_breaks_and_other/pipe_and_comment-out.R | 2 ++ tests/testthat/test-line_breaks_and_other.R | 5 +++++ 5 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 tests/testthat/line_breaks_and_other/pipe_and_comment-in.R create mode 100644 tests/testthat/line_breaks_and_other/pipe_and_comment-in_tree create mode 100644 tests/testthat/line_breaks_and_other/pipe_and_comment-out.R diff --git a/R/rules-line_break.R b/R/rules-line_break.R index 6a8a2e25d..bd90ac187 100644 --- a/R/rules-line_break.R +++ b/R/rules-line_break.R @@ -44,7 +44,7 @@ remove_line_break_before_round_closing <- function(pd) { #' @importFrom rlang seq2 add_line_break_after_pipe <- function(pd) { - is_special <- pd$token %in% c("SPECIAL-PIPE") + is_special <- pd$token == c("SPECIAL-PIPE") & pd$token_after != "COMMENT" pd$lag_newlines[lag(is_special)] <- 1L pd } diff --git a/tests/testthat/line_breaks_and_other/pipe_and_comment-in.R b/tests/testthat/line_breaks_and_other/pipe_and_comment-in.R new file mode 100644 index 000000000..f0dcfb1d5 --- /dev/null +++ b/tests/testthat/line_breaks_and_other/pipe_and_comment-in.R @@ -0,0 +1,2 @@ +1:10 %>% # sum + sum() diff --git a/tests/testthat/line_breaks_and_other/pipe_and_comment-in_tree b/tests/testthat/line_breaks_and_other/pipe_and_comment-in_tree new file mode 100644 index 000000000..f7da1fba1 --- /dev/null +++ b/tests/testthat/line_breaks_and_other/pipe_and_comment-in_tree @@ -0,0 +1,15 @@ +ROOT (token: short_text [lag_newlines/spaces] {id}) + °--expr: [0/0] {17} + ¦--expr: [0/1] {7} + ¦ ¦--expr: [0/0] {2} + ¦ ¦ °--NUM_CONST: 1 [0/0] {1} + ¦ ¦--':': : [0/0] {3} + ¦ °--expr: [0/0] {5} + ¦ °--NUM_CONST: 10 [0/0] {4} + ¦--SPECIAL-PIPE: %>% [0/1] {6} + ¦--COMMENT: # sum [0/2] {8} + °--expr: [1/0] {15} + ¦--expr: [0/0] {12} + ¦ °--SYMBOL_FUNCTION_CALL: sum [0/0] {10} + ¦--'(': ( [0/0] {11} + °--')': ) [0/0] {13} diff --git a/tests/testthat/line_breaks_and_other/pipe_and_comment-out.R b/tests/testthat/line_breaks_and_other/pipe_and_comment-out.R new file mode 100644 index 000000000..f0dcfb1d5 --- /dev/null +++ b/tests/testthat/line_breaks_and_other/pipe_and_comment-out.R @@ -0,0 +1,2 @@ +1:10 %>% # sum + sum() diff --git a/tests/testthat/test-line_breaks_and_other.R b/tests/testthat/test-line_breaks_and_other.R index 254d95721..3854b8d1e 100644 --- a/tests/testthat/test-line_breaks_and_other.R +++ b/tests/testthat/test-line_breaks_and_other.R @@ -26,3 +26,8 @@ test_that("adding and removing line breaks", { transformer = style_text), NA) }) +test_that("no line break after %>% if next token is comment", { + expect_warning(test_collection("line_breaks_and_other", "pipe_and", + transformer = style_text), NA) +}) +