Skip to content

Commit c5cca3a

Browse files
respect knitr chunk headers for spinning.
1 parent c6cab94 commit c5cca3a

File tree

7 files changed

+98
-3
lines changed

7 files changed

+98
-3
lines changed

R/expr-is.R

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,32 @@ is_comment <- function(pd) {
4343
#' @examples
4444
#' style_text("#!/usr/bin/env Rscript")
4545
is_shebang <- function(pd) {
46-
if (is.null(pd)) return(rep(FALSE, nrow(pd)))
4746
is_first_comment <- is_comment(pd) & (pd$pos_id == 1L)
4847
is_first_comment[is_first_comment] <- grepl(
4948
"^#!", pd$text[is_first_comment], perl = TRUE
5049
)
5150
is_first_comment
5251
}
5352

53+
#' Identify spinning code chunk header
54+
#'
55+
#' See https://yihui.name/knitr/demo/stitch/#spin-comment-out-texts for details.
56+
#' @examples
57+
#' style_text(c(
58+
#' "# title",
59+
#' "some_code <- function() {}",
60+
#' "#+ chunk-label, opt1=value1",
61+
#' "call(3, 2, c(3:2))"
62+
#' ))
63+
#' @param pd A parse table.
64+
is_code_chunk_header <- function(pd) {
65+
is_comment <- is_comment(pd)
66+
is_comment[is_comment] <- grepl(
67+
"^#[\\+|\\-]", pd$text[is_comment], perl = TRUE
68+
)
69+
is_comment
70+
}
71+
5472
contains_else_expr <- function(pd) {
5573
any(pd$token == "ELSE")
5674
}
@@ -74,7 +92,6 @@ contains_else_expr_that_needs_braces <- function(pd) {
7492
}
7593
}
7694

77-
7895
is_cond_expr <- function(pd) {
7996
pd$token[1] == "IF"
8097
}

R/rules-spacing.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ set_space_between_levels <- function(pd_flat) {
183183
#' after the regex "^#+'*".
184184
#' @importFrom purrr map_chr
185185
start_comments_with_space <- function(pd, force_one = FALSE) {
186-
comment_pos <- is_comment(pd) & !is_shebang(pd)
186+
comment_pos <- is_comment(pd) & !is_shebang(pd) & !is_code_chunk_header(pd)
187187
if (!any(comment_pos)) return(pd)
188188

189189
comments <- rematch2::re_match(

man/is_code_chunk_header.Rd

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#A comment
2+
a <- function() {
3+
4+
}
5+
6+
#+ chunk-label, opt1=value1
7+
"chunk-content"
8+
9+
#- chunk-label, opt1=value1
10+
call(2, 3)
11+
#21

tests/testthat/parse_comments/spinning_code_chunk_headers-in_tree

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# A comment
2+
a <- function() {
3+
4+
}
5+
6+
#+ chunk-label, opt1=value1
7+
"chunk-content"
8+
9+
#- chunk-label, opt1=value1
10+
call(2, 3)
11+
# 21

tests/testthat/test-parse_comments.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,9 @@ test_that("hashbangs are respected", {
4848
"shebang",
4949
transformer = style_text), NA)
5050
})
51+
52+
test_that("code chunk headers for spinning are respected", {
53+
expect_warning(test_collection("parse_comments",
54+
"spinning_code_chunk_headers",
55+
transformer = style_text), NA)
56+
})

0 commit comments

Comments
 (0)