Skip to content

Commit 7f6570a

Browse files
committed
Address PR review comments & refactor Rmd & Rnw styling
1 parent ca8f294 commit 7f6570a

13 files changed

+61
-151
lines changed

R/addins.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ style_active_file <- function() {
2525
)
2626
context <- get_rstudio_context()
2727
if (is_rmd_file(context$path)) {
28-
out <- transform_rmd(context$contents, transformer)
28+
out <- transform_mixed(context$contents, transformer, filetype = "Rmd")
29+
} else if (is_rnw_file(context$path)) {
30+
out <- transform_mixed(context$contents, transformer, filetype = "Rnw")
2931
} else if (is_plain_r_file(context$path) | is_unsaved_file(context$path)) {
3032
out <- try_transform_as_r_file(context, transformer)
3133
} else {
32-
stop("Can only style .R and .Rmd files.", call. = FALSE)
34+
stop("Can only style .R, .Rmd and .Rnw files.", call. = FALSE)
3335
}
3436
rstudioapi::modifyRange(
3537
c(1, 1, length(context$contents) + 1, 1),

R/transform-code.R

Lines changed: 29 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ transform_code <- function(path, fun, verbose = FALSE, ...) {
1212
enc::transform_lines_enc(path, fun = fun, ..., verbose = verbose)
1313
} else if (is_rmd_file(path)) {
1414
enc::transform_lines_enc(path,
15-
fun = partial(transform_rmd, transformer_fun = fun), ...,
15+
fun = partial(transform_mixed, transformer_fun = fun, filetype = "Rmd"), ...,
1616
verbose = verbose
1717
)
1818
} else if (is_rnw_file(path)) {
1919
enc::transform_lines_enc(path,
20-
fun = partial(transform_rnw, transformer_fun = fun), ...,
20+
fun = partial(transform_mixed, transformer_fun = fun, filetype = "Rnw"), ...,
2121
verbose = verbose
2222
)
2323
} else {
@@ -31,61 +31,30 @@ transform_code <- function(path, fun, verbose = FALSE, ...) {
3131
#' an Rmd or Rnw file and recombines the resulting (styled) code chunks with the text
3232
#' chunks.
3333
#'
34-
#' @param lines A character vector of lines from an Rmd or Rnw file
35-
#' @param transformer_fun A styler transformer function
36-
#' @param filetype A string indicating the filetype (Rmd or Rnw)
34+
#' @param lines A character vector of lines from an Rmd or Rnw file.
35+
#' @param transformer_fun A styler transformer function.
36+
#' @param filetype A string indicating the filetype (Rmd or Rnw).
3737
#' @importFrom purrr flatten_chr
3838
#' @keywords internal
39-
transform_mixed <- function(lines, transformer_fun, filetype = "Rmd") {
39+
transform_mixed <- function(lines, transformer_fun, filetype) {
4040
chunks <- separate_chunks(lines, filetype)
4141
chunks$r_chunks <- map(chunks$r_chunks, transformer_fun)
4242

4343
map2(chunks$text_chunks, c(chunks$r_chunks, list(character(0))), c) %>%
4444
flatten_chr()
4545
}
4646

47-
#' Transform Rmd contents
48-
#'
49-
#' Applies the supplied transformer function to code chunks identified within
50-
#' an Rmd file and recombines the resulting (styled) code chunks with the text
51-
#' chunks.
52-
#'
53-
#' @param lines A character vector of lines from an Rmd file
54-
#' @param transformer_fun A styler transformer function
55-
#' @importFrom purrr flatten_chr
56-
#' @keywords internal
57-
transform_rmd <- function(lines, transformer_fun) {
58-
transform_mixed(lines, transformer_fun, filetype = "Rmd")
59-
}
60-
61-
#' Transform Rnw contents
62-
#'
63-
#' Applies the supplied transformer function to code chunks identified within
64-
#' an Rnw file and recombines the resulting (styled) code chunks with the text
65-
#' chunks.
66-
#'
67-
#' @param lines A character vector of lines from an Rnw file
68-
#' @param transformer_fun A styler transformer function
69-
#' @importFrom purrr flatten_chr
70-
#' @keywords internal
71-
transform_rnw <- function(lines, transformer_fun) {
72-
transform_mixed(lines, transformer_fun, filetype = "Rnw")
73-
}
74-
7547
#' Separate chunks within Rmd and Rnw contents
7648
#'
7749
#' Identifies and separates the code and text chunks (the latter includes non-R
7850
#' code) within an Rmd or Rnw file, and returns these separately.
79-
#' @param lines a character vector of lines from an Rmd file
51+
#' @param lines A character vector of lines from an Rmd or Rnw file.
52+
#' @param filetype A string indicating the filetype - either 'Rmd' or 'Rnw'.
8053
#' @importFrom purrr map2
8154
#' @importFrom rlang seq2
8255
#' @keywords internal
83-
separate_chunks <- function(lines, filetype = "Rmd") {
84-
if(filetype == "Rmd") {
85-
r_raw_chunks <- identify_rmd_raw_chunks(lines)
86-
} else {
87-
r_raw_chunks <- identify_rnw_raw_chunks(lines)
88-
}
56+
separate_chunks <- function(lines, filetype) {
57+
r_raw_chunks <- identify_raw_chunks(lines, filetype = filetype)
8958

9059
r_chunks <- map2(
9160
r_raw_chunks$starts, r_raw_chunks$ends, ~lines[seq2(.x + 1, .y - 1)]
@@ -98,55 +67,40 @@ separate_chunks <- function(lines, filetype = "Rmd") {
9867
lst(r_chunks, text_chunks)
9968
}
10069

101-
#' Identifies raw Rmd code chunks
70+
#' Identifies raw Rmd or Rnw code chunks
10271
#'
10372
#' Raw in the sense that these chunks don't contain pure R code, but they
10473
#' contain a header and footer of markdown. Only code chunks that have an engine
10574
#' whose name matches `engine-pattern` are considered as R code.
10675
#' @inheritParams separate_chunks
10776
#' @param engine_pattern A regular expression that must match the engine name.
10877
#' @keywords internal
109-
identify_rmd_raw_chunks <- function(lines, engine_pattern = get_engine_pattern()) {
110-
pattern <- get_knitr_pattern(lines, filetype = "Rmd")
78+
identify_raw_chunks <- function(lines, filetype, engine_pattern = get_engine_pattern()) {
79+
pattern <- get_knitr_pattern(filetype)
11180
if (is.null(pattern$chunk.begin) || is.null(pattern$chunk.end)) {
11281
stop("Unrecognized chunk pattern!", call. = FALSE)
11382
}
114-
chunks <- grep("^[\t >]*```+\\s*", lines, perl = TRUE)
115-
starts <- odd(chunks)
116-
ends <- even(chunks)
11783

118-
if (length(starts) != length(ends)) {
119-
stop("Malformed file!", call. = FALSE)
120-
}
121-
122-
is_r_code <- grepl(
123-
paste0("^[\t >]*```+\\s*\\{\\s*", engine_pattern, "[\\s\\},]"),
124-
lines[starts],
125-
perl = TRUE
126-
)
127-
list(starts = starts[is_r_code], ends = ends[is_r_code])
128-
}
129-
130-
#' Identifies raw Rnw code chunks
131-
#'
132-
#' Raw in the sense that these chunks don't contain pure R code, but they
133-
#' contain the Rnw chunk header and footer. All Rnw code chunks are assumed
134-
#' to contain R code.
135-
#' @inheritParams separate_chunks
136-
#' @keywords internal
137-
identify_rnw_raw_chunks <- function(lines) {
138-
pattern <- get_knitr_pattern(lines, filetype = "Rnw")
139-
if (is.null(pattern$chunk.begin) || is.null(pattern$chunk.end)) {
140-
stop("Unrecognized chunk pattern!", call. = FALSE)
84+
if (filetype == "Rmd") {
85+
chunks <- grep("^[\t >]*```+\\s*", lines, perl = TRUE)
86+
starts <- odd(chunks)
87+
ends <- even(chunks)
88+
is_r_code <- grepl(
89+
paste0("^[\t >]*```+\\s*\\{\\s*", engine_pattern, "[\\s\\},]"),
90+
lines[starts],
91+
perl = TRUE
92+
)
93+
} else if (filetype == "Rnw") {
94+
starts <- grep(pattern$chunk.begin, lines, perl = TRUE)
95+
ends <- grep(pattern$chunk.end, lines, perl = TRUE)
96+
is_r_code <- rep(TRUE, length(start))
14197
}
142-
starts <- grep(pattern$chunk.begin, lines, perl = TRUE)
143-
ends <- grep(pattern$chunk.end, lines, perl = TRUE)
14498

14599
if (length(starts) != length(ends)) {
146100
stop("Malformed file!", call. = FALSE)
147101
}
148102

149-
list(starts = starts, ends = ends)
103+
list(starts = starts[is_r_code], ends = ends[is_r_code])
150104
}
151105

152106
#' What's the engine pattern for rmd code chunks?
@@ -170,10 +124,10 @@ get_engine_pattern <- function() {
170124
#'
171125
#' @inheritParams separate_chunks
172126
#' @keywords internal
173-
get_knitr_pattern <- function(lines, filetype = "Rmd") {
127+
get_knitr_pattern <- function(filetype) {
174128
if(filetype == "Rnw") {
175129
knitr::all_patterns[["rnw"]]
176-
} else {
130+
} else if (filetype == "Rmd") {
177131
knitr::all_patterns[["md"]]
178132
}
179133
}

R/ui.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ prettify_any <- function(transformers,
188188
)
189189
}
190190

191-
#' Style `.R` and/or `.Rmd` and.or `.Rnw` files
191+
#' Style `.R`, `.Rmd` or `.Rnw` files
192192
#'
193193
#' Performs various substitutions in the files specified.
194194
#' Carefully examine the results after running this function!

man/get_knitr_pattern.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/identify_rmd_raw_chunks.Rd renamed to man/identify_raw_chunks.Rd

Lines changed: 8 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/identify_rnw_raw_chunks.Rd

Lines changed: 0 additions & 17 deletions
This file was deleted.

man/separate_chunks.Rd

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/style_file.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/transform_mixed.Rd

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/transform_rmd.Rd

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)