Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove leading blank lines when strict = TRUE #1056

Merged
merged 22 commits into from
Dec 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
cf8feae
Update transform-files.R
IndrajeetPatil Nov 8, 2022
75544d0
get started
IndrajeetPatil Nov 9, 2022
21ab4e8
pre-commit
github-actions[bot] Nov 9, 2022
89fd790
minor
IndrajeetPatil Nov 9, 2022
b37d847
add `is_roxygen_code_example` parameter
IndrajeetPatil Nov 10, 2022
d89f51d
fix regression
IndrajeetPatil Nov 10, 2022
7ea5656
comment out for now
IndrajeetPatil Nov 10, 2022
4fe3083
pre-commit
github-actions[bot] Nov 10, 2022
74eee56
Update tests-cache-require-serial.R
IndrajeetPatil Nov 10, 2022
5065352
Update R/transform-files.R
IndrajeetPatil Nov 10, 2022
a6f346e
simplify if statement
IndrajeetPatil Nov 10, 2022
aa282b1
pre-commit
github-actions[bot] Nov 10, 2022
b2cb5f2
Merge branch '1014_rm_leading_blank_line' of https://github.com/r-lib…
IndrajeetPatil Nov 10, 2022
6750556
Update .Rbuildignore
IndrajeetPatil Nov 10, 2022
0af4be5
pre-commit
github-actions[bot] Nov 10, 2022
4f29805
Merge branch 'main' into 1014_rm_leading_blank_line
IndrajeetPatil Nov 19, 2022
bcb62e5
Merge branch 'main' into 1014_rm_leading_blank_line
IndrajeetPatil Nov 23, 2022
697bf76
don't comment out failing tests
IndrajeetPatil Dec 10, 2022
617c70d
Merge branch 'main' into 1014_rm_leading_blank_line
IndrajeetPatil Dec 10, 2022
26be437
ensure that line breaks ony removed for top of file
lorenzwalthert Dec 24, 2022
e177051
Merge branch 'main' into 1014_rm_leading_blank_line
lorenzwalthert Dec 24, 2022
e143717
Merge branch 'main' into 1014_rm_leading_blank_line
IndrajeetPatil Dec 24, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions R/roxygen-examples.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ style_roxygen_code_example_segment <- function(one_dont,
}
dont_seqs <- find_dont_seqs(one_dont)
split_segments <- split_roxygen_segments(one_dont, unlist(dont_seqs))
is_dont <-
seq2(1L, length(split_segments$separated)) %in% split_segments$selectors
is_dont <- seq2(1L, length(split_segments$separated)) %in% split_segments$selectors

map2(split_segments$separated, is_dont,
style_roxygen_example_snippet,
Expand Down Expand Up @@ -117,8 +116,11 @@ style_roxygen_example_snippet <- function(code_snippet,
)
if (!is_cached || !cache_is_active) {
code_snippet <- code_snippet %>%
parse_transform_serialize_r(transformers,
base_indention = base_indention, warn_empty = FALSE
parse_transform_serialize_r(
transformers,
base_indention = base_indention,
warn_empty = FALSE,
is_roxygen_code_example = TRUE
)
}

Expand Down
5 changes: 4 additions & 1 deletion R/transform-block.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ parse_transform_serialize_r_block <- function(pd_nested,
) %>%
unlist(use.names = FALSE)
}
c(rep("", start_line - 1L), serialized_transformed_text)
c(
rep("", start_line - as.integer(start_line > 0L)),
serialized_transformed_text
)
}

#' Find the groups of expressions that should be processed together
Expand Down
16 changes: 14 additions & 2 deletions R/transform-files.R
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ split_roxygen_segments <- function(text, roxygen_examples) {
#' Wrapper function for the common three operations.
#' @param warn_empty Whether or not a warning should be displayed when `text`
#' does not contain any tokens.
#' @param is_roxygen_code_example Is code a roxygen examples block?
#' @inheritParams compute_parse_data_nested
#' @inheritParams parse_transform_serialize_r_block
#' @seealso [parse_transform_serialize_roxygen()]
Expand All @@ -239,7 +240,8 @@ split_roxygen_segments <- function(text, roxygen_examples) {
parse_transform_serialize_r <- function(text,
transformers,
base_indention,
warn_empty = TRUE) {
warn_empty = TRUE,
is_roxygen_code_example = FALSE) {
more_specs <- cache_more_specs(
include_roxygen_examples = TRUE, base_indention = base_indention
)
Expand All @@ -257,14 +259,24 @@ parse_transform_serialize_r <- function(text,
transformers
)

strict <- transformers$more_specs_style_guide$strict %||% TRUE
pd_split <- unname(split(pd_nested, pd_nested$block))
pd_blank <- find_blank_lines_to_next_block(pd_nested)

text_out <- vector("list", length(pd_split))
for (i in seq_along(pd_split)) {
# if the first block: only preserve for roxygen or not strict
# if a later block: always preserve line breaks
start_line <- if (i == 1L) {
if (is_roxygen_code_example || !strict) pd_blank[[i]] else 1L
} else {
pd_blank[[i]]
}


text_out[[i]] <- parse_transform_serialize_r_block(
pd_split[[i]],
pd_blank[[i]],
start_line = start_line,
transformers = transformers,
base_indention = base_indention
)
Expand Down
5 changes: 4 additions & 1 deletion man/parse_transform_serialize_r.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion tests/testthat/curly-curly/mixed-out.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

## ............................................................................
## line breaks ####
# not inserting line breaks
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/indention_curly_brackets/custom-out.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

value <- 5
if (value > 0) {
print(value)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

c(a |> b())

c(a + b |> c())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


test_that("hi there", {
I(am(a(package(x))))
})
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


test_that("hi there", {
I(am(a(package(x))))
})
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


test_that("hi there", {
I(am(a(package(x))))
})
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


test_that("hi there", {
I(am(a(package(x))))
})
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


test_that("hi there", {
I(am(a(package(x))))
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#' Create a style guide
#'
#' @param reindention A list of parameters for regex re-indention, most
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#' Create a style guide
#'
#' @param reindention A list of parameters for regex re-indention, most
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#' Create a style guide
#'
#' @param reindention A list of parameters for regex re-indention, most
Expand Down
5 changes: 0 additions & 5 deletions tests/testthat/start_line/comment-out.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@





# a comment
a <- function(x) {
x
Expand Down
5 changes: 0 additions & 5 deletions tests/testthat/start_line/no_comment-out.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@





a <- function(x) {
x
}
1 change: 0 additions & 1 deletion tests/testthat/stylerignore/simple-out.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

call(1 ) # styler: off
# styler: off
# also if there are more comments
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-parse_comments.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
library("testthat")


test_that("spacing within comments is done correctly", {
expect_warning(test_collection("parse_comments",
"within_spacing_with_force",
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/tests-cache-require-serial.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

test_that("top-level test: Caches top-level expressions efficiently on style_text()", {
local_test_setup(cache = TRUE)
text <- test_path("cache-with-r-cache/mlflow-1-in.R") %>%
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

print_out <- function(x, ...) {
lines <- m(y, ..., print = TRUE)
paste(lines, sep = "\n")
Expand Down