Skip to content

Commit

Permalink
preserve status quo and prevent new line breaks with {{.
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzwalthert committed Jun 29, 2019
1 parent 42f1a41 commit afbfc1e
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 16 deletions.
43 changes: 39 additions & 4 deletions R/rules-line-break.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ set_line_break_around_comma <- function(pd) {
pd
}

style_line_break_around_curly <- function(strict, pd) {
style_line_break_around_curly <- function(strict, curly_curly_has_linebreak, pd) {
if (is_curly_expr(pd) && nrow(pd) > 2) {
closing_before <- pd$token == "'}'"
opening_before <- (pd$token == "'{'") & (pd$token_after != "COMMENT")
to_break <- lag(opening_before, default = FALSE) | closing_before
to_break <- to_break_curly_inner(pd, strict, curly_curly_has_linebreak) &
to_break_curly_outer(pd, strict, curly_curly_has_linebreak)
len_to_break <- sum(to_break)
pd$lag_newlines[to_break] <- ifelse(rep(strict, len_to_break),
1L,
Expand All @@ -30,6 +29,42 @@ style_line_break_around_curly <- function(strict, pd) {
pd
}


to_break_curly_inner <- function(pd, strict, curly_curly_has_linebreak) {
closing_before <- (pd$token == "'}'") &
if (!curly_curly_has_linebreak) {
pd$token_after != "'}'"
} else {
TRUE
}
opening_before <- (pd$token == "'{'") &
(pd$token_after != "COMMENT") &
if (!curly_curly_has_linebreak) {
pd$token_before != "'{'"
} else {
TRUE
}
lag(opening_before, default = FALSE) | closing_before
}

to_break_curly_outer <- function(pd, strict, curly_curly_has_linebreak) {
closing_before <- (pd$token == "'}'") &
if (!curly_curly_has_linebreak) {
pd$token_before != "'}'"
} else {
TRUE
}

opening_before <- (pd$token == "'{'") &
(pd$token_after != "COMMENT") &
if (!curly_curly_has_linebreak) {
pd$token_after != "'{'"
} else {
TRUE
}
lag(opening_before, default = FALSE) | closing_before
}

# if ) follows on }, don't break line
remove_line_break_before_round_closing_after_curly <- function(pd) {
round_after_curly <- pd$token == "')'" & (pd$token_before == "'}'")
Expand Down
2 changes: 1 addition & 1 deletion R/style-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ tidyverse_style <- function(scope = "tokens",
remove_line_break_before_round_closing_fun_dec =
if (strict) remove_line_break_before_round_closing_fun_dec,
style_line_break_around_curly = partial(style_line_break_around_curly,
strict
strict = strict, curly_curly_has_linebreak = !strict
),
set_line_break_after_opening_if_call_is_multi_line = if (strict)
partial(
Expand Down
20 changes: 20 additions & 0 deletions tests/testthat/helpers-curly-culry.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
style_text_without_curly_curly <- function(text,
...,
style = tidyverse_style,
transformers = style(...),
include_roxygen_examples = TRUE) {
dots <- list(...)
if ("strict" %in% names(dots)) {
strict <- dots$strict
} else {
strict <- TRUE
}
transformers$line_break$style_line_break_around_curly <-
purrr::partial(style_line_break_around_curly,
curly_curly_has_linebreak = TRUE, strict = strict
)
style_text(text, ...,
style = NULL, transformers = transformers,
include_roxygen_examples = include_roxygen_examples
)
}
10 changes: 5 additions & 5 deletions tests/testthat/test-indention_curly.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ context("indent curly brackets")
test_that("indention on one-liner curley only is not changed", {
expect_warning(test_collection("indention_curly_brackets",
"one_line_curly",
transformer = style_text), NA)
transformer = style_text_without_curly_curly), NA)

})

test_that("indention with multi-line curley only is correct", {
expect_warning(test_collection("indention_curly_brackets",
"multi_line_curly_only",
transformer = style_text), NA)
transformer = style_text_without_curly_curly), NA)

})


test_that("indention with multi-line curley and round is correct", {
expect_warning(test_collection("indention_curly_brackets",
"multi_line_curly_round_only",
transformer = style_text), NA)
transformer = style_text_without_curly_curly), NA)

})

Expand All @@ -28,10 +28,10 @@ test_that(paste("complete styling via top level api is correct",
"(round, curly, spacing)"), {
expect_warning(test_collection("indention_curly_brackets",
"multi_line_curly_round_spacing",
transformer = style_text), NA)
transformer = style_text_without_curly_curly), NA)

expect_warning(test_collection("indention_curly_brackets",
"multi_line_curly_while_for_if_fun",
transformer = style_text), NA)
transformer = style_text_without_curly_curly), NA)

})
6 changes: 3 additions & 3 deletions tests/testthat/test-indention_multiple.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ test_that("multiple round brackets don't cause extraindention", {
test_that("multiple curly brackets don't cause extraindention", {
expect_warning(test_collection("indention_multiple",
"curly_only",
transformer = style_text), NA)
transformer = style_text_without_curly_curly), NA)

})


test_that("multiple curly and round brackets don't cause extraindention", {
expect_warning(test_collection("indention_multiple",
"curly_and_round",
transformer = style_text), NA)
transformer = style_text_without_curly_curly), NA)

})

Expand All @@ -46,7 +46,7 @@ test_that("if and ifelse interacting with curly braces works", {
test_that("edge cases work", {
expect_warning(test_collection("indention_multiple",
"edge_strict",
transformer = style_text), NA)
transformer = style_text_without_curly_curly), NA)
})

test_that("token / braces interaction works", {
Expand Down
5 changes: 3 additions & 2 deletions tests/testthat/test-token_adding_removing.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ test_that("other manipulations are correct (add braces, semi-colon etc.)", {

test_that("braces in if-else clause are added correctly", {
expect_warning(test_collection("token_adding_removing", "if_else_strict",
transformer = style_text), NA)
transformer = style_text_without_curly_curly), NA)
expect_warning(test_collection("token_adding_removing", "if_else_non_strict",
transformer = style_text, strict = FALSE), NA)
transformer = style_text,
strict = FALSE), NA)
expect_warning(test_collection("token_adding_removing", "if-else-comma",
transformer = style_text, strict = TRUE), NA)

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-unindention.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ context("unindention")
test_that("round brackets are unindented correctly", {
expect_warning(test_collection("unindention",
"mixed",
transformer = style_text,
transformer = style_text_without_curly_curly,
write_back = TRUE), NA)
})

Expand Down

0 comments on commit afbfc1e

Please sign in to comment.