Skip to content

Commit db90329

Browse files
Merge pull request #700 from lorenzwalthert/issue-699
- Support xaringan (#700).
2 parents f4c395e + 5c62494 commit db90329

File tree

11 files changed

+123
-18
lines changed

11 files changed

+123
-18
lines changed

.github/workflows/benchmarking.yaml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,14 @@ jobs:
4444
writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version")
4545
shell: Rscript {0}
4646
- name: Cache R packages
47-
if: ${{ github.event_name == 'pull_request' }}
48-
if: runner.os != 'Windows'
47+
if: ${{ github.event_name == 'pull_request' }} && runner.os != 'Windows'
4948
uses: actions/cache@v1
5049
with:
5150
path: ${{ env.R_LIBS_USER }}
5251
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-3-${{ hashFiles('.github/depends.Rds') }}
5352
restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-3-
5453
- name: Install system dependencies
55-
if: ${{ github.event_name == 'pull_request' }}
56-
if: runner.os == 'Linux'
54+
if: ${{ github.event_name == 'pull_request' }} && runner.os == 'Linux'
5755
env:
5856
RHUB_PLATFORM: linux-x86_64-ubuntu-gcc
5957
run: |

.pre-commit-config.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ repos:
3030
.*\.rds|
3131
.*\.Rds|
3232
.*\.sh|
33-
.*\.RData
33+
.*\.RData|
34+
.*-in_tree
3435
)$
3536
- id: readme-rmd-rendered
3637
- id: parsable-R

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
- speed improvements: (~10%) when cache is activated because transformers are not
2222
captured as character anymore (#679), ~ 3% in low-level optimization (#691).
2323
Require magrittr 2.0 gives about 7% speed improvement (#681).
24+
- `#<<` is now recognized as the xaringan marker and no space is added after`#`
25+
(#700).
2426

2527
## Minor changes and fixes
2628

R/expr-is.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,18 @@ is_subset_expr <- function(pd) {
110110
#' style_text("#!/usr/bin/env Rscript")
111111
#' @keywords internal
112112
is_shebang <- function(pd) {
113-
is_first_comment <- is_comment(pd) & (pd$pos_id == 1L)
113+
is_first_comment <- pd$pos_id == 1L
114114
is_first_comment[is_first_comment] <- grepl(
115115
"^#!", pd$text[is_first_comment],
116116
perl = TRUE
117117
)
118118
is_first_comment
119119
}
120120

121-
#' Identify spinning code chunk header
121+
#' Identify spinning code chunk header or xaringan
122122
#'
123+
#' Wrongly identifies a comment without a preceding line break as a code chunk
124+
#' header.
123125
#' See https://yihui.name/knitr/demo/stitch/#spin-comment-out-texts for details.
124126
#' @examples
125127
#' style_text(c(
@@ -130,10 +132,10 @@ is_shebang <- function(pd) {
130132
#' ))
131133
#' @param pd A parse table.
132134
#' @keywords internal
133-
is_code_chunk_header <- function(pd) {
135+
is_code_chunk_header_or_xaringan <- function(pd) {
134136
is_comment <- is_comment(pd)
135137
is_comment[is_comment] <- grepl(
136-
"^#[\\+|\\-]", pd$text[is_comment],
138+
"^#[\\+|\\-|<<]", pd$text[is_comment],
137139
perl = TRUE
138140
)
139141
is_comment

R/rules-spacing.R

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,19 @@ set_space_between_levels <- function(pd_flat) {
294294
#' @importFrom purrr map_chr
295295
#' @keywords internal
296296
start_comments_with_space <- function(pd, force_one = FALSE) {
297-
comment_pos <- is_comment(pd) & !is_shebang(pd) & !is_code_chunk_header(pd)
298-
if (!any(comment_pos)) {
297+
is_comment <- is_comment(pd)
298+
299+
if (any(is_comment)) {
300+
is_comment <- is_comment & !is_shebang(pd) & !is_code_chunk_header_or_xaringan(pd)
301+
if (!any(is_comment)) {
302+
return(pd)
303+
}
304+
} else {
299305
return(pd)
300306
}
301307

302308
comments <- rematch2::re_match(
303-
pd$text[comment_pos],
309+
pd$text[is_comment],
304310
"^(?<prefix>#+['\\*]*)(?<space_after_prefix> *)(?<text>.*)$"
305311
)
306312
comments$space_after_prefix <- nchar(
@@ -312,14 +318,14 @@ start_comments_with_space <- function(pd, force_one = FALSE) {
312318
force_one
313319
)
314320

315-
pd$text[comment_pos] <-
321+
pd$text[is_comment] <-
316322
paste0(
317323
comments$prefix,
318324
map_chr(comments$space_after_prefix, rep_char, char = " "),
319325
comments$text
320326
) %>%
321327
trimws("right")
322-
pd$short[comment_pos] <- substr(pd$text[comment_pos], 1, 5)
328+
pd$short[is_comment] <- substr(pd$text[is_comment], 1, 5)
323329
pd
324330
}
325331

inst/WORDLIST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ Walthert
203203
withr
204204
writeLines
205205
www
206+
xaringan
206207
xenial
207208
xfun
208209
Xie

man/is_code_chunk_header.Rd renamed to man/is_code_chunk_header_or_xaringan.Rd

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
foo(
2+
data = mtcars,
3+
x = cyl,
4+
y = wt #<<
5+
)
6+
7+
8+
library(ggplot2)
9+
10+
ggplot(aes(x, y), data) +
11+
geom_point() + #<<
12+
scale_x_continuous() #<<

tests/testthat/parse_comments/xaringan-in_tree

Lines changed: 62 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
foo(
2+
data = mtcars,
3+
x = cyl,
4+
y = wt #<<
5+
)
6+
7+
8+
library(ggplot2)
9+
10+
ggplot(aes(x, y), data) +
11+
geom_point() + #<<
12+
scale_x_continuous() #<<

0 commit comments

Comments
 (0)