From 284ee2965d1b171867ecd4070cf2f3ea86608ba5 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Thu, 17 Mar 2022 21:11:44 +0100 Subject: [PATCH 1/4] support qmd file format, and treat it internally as R markdown --- NEWS.md | 6 ++ R/set-assert-args.R | 5 +- R/transform-code.R | 2 +- R/ui-styling.R | 23 ++++++-- R/utils-files.R | 5 ++ man/combine_children.Rd | 4 +- man/prettify_any.Rd | 4 +- man/rds_to_version.Rd | 2 +- man/style_dir.Rd | 3 +- man/style_file.Rd | 2 +- man/style_pkg.Rd | 3 +- tests/testthat/public-api/xyzfile_qmd/new.qmd | 19 ++++++ .../public-api/xyzpackage-qmd/DESCRIPTION | 15 +++++ .../public-api/xyzpackage-qmd/NAMESPACE | 1 + .../public-api/xyzpackage-qmd/R/RcppExports.R | 6 ++ .../public-api/xyzpackage-qmd/R/hello-world.R | 3 + .../public-api/xyzpackage-qmd/README.Rmd | 33 +++++++++++ .../public-api/xyzpackage-qmd/new.qmd | 19 ++++++ .../public-api/xyzpackage-qmd/src/.gitignore | 3 + .../xyzpackage-qmd/src/RcppExports.cpp | 28 +++++++++ .../xyzpackage-qmd/src/timesTwo.cpp | 27 +++++++++ .../xyzpackage-qmd/tests/testthat.R | 4 ++ .../tests/testthat/test-package-xyz.R | 5 ++ .../xyzpackage-qmd/vignettes/random.Rmarkdown | 58 +++++++++++++++++++ .../xyzpackage-qmd/vignettes/random.Rmd | 58 +++++++++++++++++++ .../xyzpackage-qmd/xyzpackage.Rproj | 16 +++++ tests/testthat/test-public_api.R | 45 ++++++++++++++ 27 files changed, 383 insertions(+), 16 deletions(-) create mode 100644 tests/testthat/public-api/xyzfile_qmd/new.qmd create mode 100644 tests/testthat/public-api/xyzpackage-qmd/DESCRIPTION create mode 100644 tests/testthat/public-api/xyzpackage-qmd/NAMESPACE create mode 100644 tests/testthat/public-api/xyzpackage-qmd/R/RcppExports.R create mode 100644 tests/testthat/public-api/xyzpackage-qmd/R/hello-world.R create mode 100644 tests/testthat/public-api/xyzpackage-qmd/README.Rmd create mode 100644 tests/testthat/public-api/xyzpackage-qmd/new.qmd create mode 100644 tests/testthat/public-api/xyzpackage-qmd/src/.gitignore create mode 100644 tests/testthat/public-api/xyzpackage-qmd/src/RcppExports.cpp create mode 100644 tests/testthat/public-api/xyzpackage-qmd/src/timesTwo.cpp create mode 100644 tests/testthat/public-api/xyzpackage-qmd/tests/testthat.R create mode 100644 tests/testthat/public-api/xyzpackage-qmd/tests/testthat/test-package-xyz.R create mode 100644 tests/testthat/public-api/xyzpackage-qmd/vignettes/random.Rmarkdown create mode 100644 tests/testthat/public-api/xyzpackage-qmd/vignettes/random.Rmd create mode 100644 tests/testthat/public-api/xyzpackage-qmd/xyzpackage.Rproj diff --git a/NEWS.md b/NEWS.md index 25003e04b..35724d0f9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,9 @@ +# styler 1.7.0.9000 (Development version) + +* `filetype` `.qmd` is now supported, but not turned on by default (#931). + + + # styler 1.7.0 **API changes** diff --git a/R/set-assert-args.R b/R/set-assert-args.R index f1b318f10..49f981777 100644 --- a/R/set-assert-args.R +++ b/R/set-assert-args.R @@ -71,10 +71,11 @@ set_and_assert_arg_filetype <- function(filetype) { #' @importFrom rlang abort #' @keywords internal assert_filetype <- function(lowercase_filetype) { - if (!all(lowercase_filetype %in% c("r", "rmd", "rmarkdown", "rnw", "rprofile"))) { + allowed_types <- c("r", "rmd", "rmarkdown", "rnw", "rprofile", "qmd") + if (!all(lowercase_filetype %in% allowed_types)) { abort(paste( "filetype must not contain other values than 'R', 'Rprofile',", - "'Rmd', 'Rmarkdown' or 'Rnw' (case is ignored)." + "'Rmd', 'Rmarkdown', 'qmd' or 'Rnw' (case is ignored)." )) } } diff --git a/R/transform-code.R b/R/transform-code.R index 11406acc5..83b7bc797 100644 --- a/R/transform-code.R +++ b/R/transform-code.R @@ -11,7 +11,7 @@ transform_code <- function(path, fun, ..., dry) { if (is_plain_r_file(path) || is_rprofile_file(path)) { transform_utf8(path, fun = fun, ..., dry = dry) - } else if (is_rmd_file(path)) { + } else if (is_rmd_file(path) || is_qmd_file(path)) { transform_utf8(path, fun = partial(transform_mixed, transformer_fun = fun, filetype = "Rmd"), ..., dry = dry diff --git a/R/ui-styling.R b/R/ui-styling.R index bd035c336..57745f4f1 100644 --- a/R/ui-styling.R +++ b/R/ui-styling.R @@ -7,7 +7,8 @@ NULL #' #' Performs various substitutions in all `.R` files in a package #' (code and tests). One can also (optionally) style `.Rmd`, `.Rmarkdown` and/or -#' `.Rnw` files (vignettes and readme) by changing the `filetype` argument. +#' `.qmd`, `.Rnw` files (vignettes and readme) by changing the `filetype` +#' argument. #' Carefully examine the results after running this function! #' #' @param pkg Path to a (subdirectory of an) R package. @@ -161,6 +162,17 @@ prettify_pkg <- function(transformers, ) ) } + + if ("\\.qmd" %in% filetype_) { + vignette_files <- append( + vignette_files, + dir_without_.( + path = ".", + pattern = "\\.qmd$" + ) + ) + } + files <- setdiff( c(r_files, rprofile_files, vignette_files, readme), exclude_files @@ -214,7 +226,8 @@ style_text <- function(text, #' Prettify arbitrary R code #' -#' Performs various substitutions in all `.R`, `.Rmd`, `.Rmarkdown` and/or `.Rnw` files +#' Performs various substitutions in all `.R`, `.Rmd`, `.Rmarkdown`, `qmd` +#' and/or `.Rnw` files #' in a directory (by default only `.R` files are styled - see `filetype` argument). #' Carefully examine the results after running this function! #' @param path Path to a directory with files to transform. @@ -263,8 +276,8 @@ style_dir <- function(path = ".", #' #' This is a helper function for style_dir. #' @inheritParams style_pkg -#' @param recursive A logical value indicating whether or not files in subdirectories -#' should be styled as well. +#' @param recursive A logical value indicating whether or not files in +#' subdirectories should be styled as well. #' @keywords internal prettify_any <- function(transformers, filetype, @@ -298,7 +311,7 @@ prettify_any <- function(transformers, ) } -#' Style `.R`, `.Rmd`, `.Rmarkdown` or `.Rnw` files +#' Style `.R`, `.Rmd`, `.Rmarkdown`, `.qmd` or `.Rnw` files #' #' Performs various substitutions in the files specified. #' Carefully examine the results after running this function! diff --git a/R/utils-files.R b/R/utils-files.R index 8e92471da..e1890cd60 100644 --- a/R/utils-files.R +++ b/R/utils-files.R @@ -13,6 +13,11 @@ is_rnw_file <- function(path) { grepl("\\.Rnw$", path, ignore.case = TRUE) } +is_qmd_file <- function(path) { + grepl("\\.qmd$", path, ignore.case = TRUE) +} + + is_unsaved_file <- function(path) { path == "" } diff --git a/man/combine_children.Rd b/man/combine_children.Rd index f587b9aaf..fef5d4a3a 100644 --- a/man/combine_children.Rd +++ b/man/combine_children.Rd @@ -16,8 +16,8 @@ Binds two parse tables together and arranges them so that the tokens are in the correct order. } \details{ -Essentially, this is a wrapper around \code{\link[dplyr:bind_rows]{dplyr::bind_rows()}}, but -returns \code{NULL} if the result of \code{\link[dplyr:bind_rows]{dplyr::bind_rows()}} is a data frame with +Essentially, this is a wrapper around \code{\link[dplyr:bind]{dplyr::bind_rows()}}, but +returns \code{NULL} if the result of \code{\link[dplyr:bind]{dplyr::bind_rows()}} is a data frame with zero rows. } \keyword{internal} diff --git a/man/prettify_any.Rd b/man/prettify_any.Rd index 09056dec0..c13e2a716 100644 --- a/man/prettify_any.Rd +++ b/man/prettify_any.Rd @@ -25,8 +25,8 @@ be styled. Case is ignored, and the \code{.} is optional, e.g. \code{c(".R", ".Rmd")}, or \code{c("r", "rmd")}. Supported values (after standardization) are: "r", "rprofile", "rmd", "rmarkdown", "rnw". Rmarkdown is treated as Rmd.} -\item{recursive}{A logical value indicating whether or not files in subdirectories -should be styled as well.} +\item{recursive}{A logical value indicating whether or not files in +subdirectories should be styled as well.} \item{exclude_files}{Character vector with paths to files that should be excluded from styling.} diff --git a/man/rds_to_version.Rd b/man/rds_to_version.Rd index 5a4b236a4..59d9d15b7 100644 --- a/man/rds_to_version.Rd +++ b/man/rds_to_version.Rd @@ -12,6 +12,6 @@ rds_to_version(path, version = 2) \item{version}{The target version.} } \description{ -Needed to make \code{\link[testthat:expect_known_value]{testthat::expect_known_value()}} work on R < 3.6. +Needed to make \code{\link[testthat:expect_known_output]{testthat::expect_known_value()}} work on R < 3.6. } \keyword{internal} diff --git a/man/style_dir.Rd b/man/style_dir.Rd index 1ce1c6310..9d94f5b11 100644 --- a/man/style_dir.Rd +++ b/man/style_dir.Rd @@ -61,7 +61,8 @@ of styling. "off", the default, writes back if the input and output of styling are not identical.} } \description{ -Performs various substitutions in all \code{.R}, \code{.Rmd}, \code{.Rmarkdown} and/or \code{.Rnw} files +Performs various substitutions in all \code{.R}, \code{.Rmd}, \code{.Rmarkdown}, \code{qmd} +and/or \code{.Rnw} files in a directory (by default only \code{.R} files are styled - see \code{filetype} argument). Carefully examine the results after running this function! } diff --git a/man/style_file.Rd b/man/style_file.Rd index 95fec034e..0924568f7 100644 --- a/man/style_file.Rd +++ b/man/style_file.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/ui-styling.R \name{style_file} \alias{style_file} -\title{Style \code{.R}, \code{.Rmd}, \code{.Rmarkdown} or \code{.Rnw} files} +\title{Style \code{.R}, \code{.Rmd}, \code{.Rmarkdown}, \code{.qmd} or \code{.Rnw} files} \usage{ style_file( path, diff --git a/man/style_pkg.Rd b/man/style_pkg.Rd index f7f51fc1f..63851bcdf 100644 --- a/man/style_pkg.Rd +++ b/man/style_pkg.Rd @@ -60,7 +60,8 @@ styling are not identical.} \description{ Performs various substitutions in all \code{.R} files in a package (code and tests). One can also (optionally) style \code{.Rmd}, \code{.Rmarkdown} and/or -\code{.Rnw} files (vignettes and readme) by changing the \code{filetype} argument. +\code{.qmd}, \code{.Rnw} files (vignettes and readme) by changing the \code{filetype} +argument. Carefully examine the results after running this function! } \section{Warning}{ diff --git a/tests/testthat/public-api/xyzfile_qmd/new.qmd b/tests/testthat/public-api/xyzfile_qmd/new.qmd new file mode 100644 index 000000000..eda390a3e --- /dev/null +++ b/tests/testthat/public-api/xyzfile_qmd/new.qmd @@ -0,0 +1,19 @@ +--- +output: + github_document: + html_preview: true +--- + + + +Some text +```{r} +# Some R code +f <- function(x) { + x +} +``` +Final text +```{r} +1 + 2 +``` diff --git a/tests/testthat/public-api/xyzpackage-qmd/DESCRIPTION b/tests/testthat/public-api/xyzpackage-qmd/DESCRIPTION new file mode 100644 index 000000000..69b7e9773 --- /dev/null +++ b/tests/testthat/public-api/xyzpackage-qmd/DESCRIPTION @@ -0,0 +1,15 @@ +Package: xyzpackage +Title: What the Package Does (one line, title case) +Version: 0.0.0.9000 +Authors@R: person("First", "Last", email = "first.last@example.com", role = c("aut", "cre")) +Description: What the package does (one paragraph). +Depends: R (>= 3.3.2) +License: What license is it under? +Encoding: UTF-8 +LazyData: true +Suggests: testthat +LinkingTo: + Rcpp +Imports: + Rcpp +RoxygenNote: 6.0.1.9000 diff --git a/tests/testthat/public-api/xyzpackage-qmd/NAMESPACE b/tests/testthat/public-api/xyzpackage-qmd/NAMESPACE new file mode 100644 index 000000000..e651b9448 --- /dev/null +++ b/tests/testthat/public-api/xyzpackage-qmd/NAMESPACE @@ -0,0 +1 @@ +# Generated by roxygen2: do not edit by hand diff --git a/tests/testthat/public-api/xyzpackage-qmd/R/RcppExports.R b/tests/testthat/public-api/xyzpackage-qmd/R/RcppExports.R new file mode 100644 index 000000000..7773bec0d --- /dev/null +++ b/tests/testthat/public-api/xyzpackage-qmd/R/RcppExports.R @@ -0,0 +1,6 @@ +# Generated by using Rcpp::compileAttributes() -> do not edit by hand +# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 + +timesTwo <- function(x) { + .Call("_xyzpackage_timesTwo", PACKAGE = "xyzpackage", x) +} diff --git a/tests/testthat/public-api/xyzpackage-qmd/R/hello-world.R b/tests/testthat/public-api/xyzpackage-qmd/R/hello-world.R new file mode 100644 index 000000000..d2cb60dff --- /dev/null +++ b/tests/testthat/public-api/xyzpackage-qmd/R/hello-world.R @@ -0,0 +1,3 @@ +hello_world <- function() { + print("hello, world") +} diff --git a/tests/testthat/public-api/xyzpackage-qmd/README.Rmd b/tests/testthat/public-api/xyzpackage-qmd/README.Rmd new file mode 100644 index 000000000..1183cea92 --- /dev/null +++ b/tests/testthat/public-api/xyzpackage-qmd/README.Rmd @@ -0,0 +1,33 @@ +--- +output: github_document +--- + + + +```{r setup, include = FALSE} +knitr::opts_chunk$set( + collapse = TRUE, + comment = "#>", + fig.path = "man/figures/README-" +) +``` +# styler + +The goal of styler is to ... + +## Installation + +You can install styler from github with: + +```{r gh-installation, eval = FALSE} +# install.packages("devtools") +devtools::install_github("jonmcalder/styler") +``` + +## Example + +This is a basic example which shows you how to solve a common problem: + +```{r example} +## basic example code +``` diff --git a/tests/testthat/public-api/xyzpackage-qmd/new.qmd b/tests/testthat/public-api/xyzpackage-qmd/new.qmd new file mode 100644 index 000000000..eda390a3e --- /dev/null +++ b/tests/testthat/public-api/xyzpackage-qmd/new.qmd @@ -0,0 +1,19 @@ +--- +output: + github_document: + html_preview: true +--- + + + +Some text +```{r} +# Some R code +f <- function(x) { + x +} +``` +Final text +```{r} +1 + 2 +``` diff --git a/tests/testthat/public-api/xyzpackage-qmd/src/.gitignore b/tests/testthat/public-api/xyzpackage-qmd/src/.gitignore new file mode 100644 index 000000000..2f843a5dc --- /dev/null +++ b/tests/testthat/public-api/xyzpackage-qmd/src/.gitignore @@ -0,0 +1,3 @@ +*.dll +*.o +*.so diff --git a/tests/testthat/public-api/xyzpackage-qmd/src/RcppExports.cpp b/tests/testthat/public-api/xyzpackage-qmd/src/RcppExports.cpp new file mode 100644 index 000000000..fcecddd2a --- /dev/null +++ b/tests/testthat/public-api/xyzpackage-qmd/src/RcppExports.cpp @@ -0,0 +1,28 @@ +// Generated by using Rcpp::compileAttributes() -> do not edit by hand +// Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 + +#include + +using namespace Rcpp; + +// timesTwo +NumericVector timesTwo(NumericVector x); +RcppExport SEXP _xyzpackage_timesTwo(SEXP xSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< NumericVector >::type x(xSEXP); + rcpp_result_gen = Rcpp::wrap(timesTwo(x)); + return rcpp_result_gen; +END_RCPP +} + +static const R_CallMethodDef CallEntries[] = { + {"_xyzpackage_timesTwo", (DL_FUNC) &_xyzpackage_timesTwo, 1}, + {NULL, NULL, 0} +}; + +RcppExport void R_init_xyzpackage(DllInfo *dll) { + R_registerRoutines(dll, NULL, CallEntries, NULL, NULL); + R_useDynamicSymbols(dll, FALSE); +} diff --git a/tests/testthat/public-api/xyzpackage-qmd/src/timesTwo.cpp b/tests/testthat/public-api/xyzpackage-qmd/src/timesTwo.cpp new file mode 100644 index 000000000..b650533e0 --- /dev/null +++ b/tests/testthat/public-api/xyzpackage-qmd/src/timesTwo.cpp @@ -0,0 +1,27 @@ +#include +using namespace Rcpp; + +// This is a simple example of exporting a C++ function to R. You can +// source this function into an R session using the Rcpp::sourceCpp +// function (or via the Source button on the editor toolbar). Learn +// more about Rcpp at: +// +// http://www.rcpp.org/ +// http://adv-r.had.co.nz/Rcpp.html +// http://gallery.rcpp.org/ +// + +// [[Rcpp::export]] +NumericVector timesTwo(NumericVector x) { + return x * 2; +} + + +// You can include R code blocks in C++ files processed with sourceCpp +// (useful for testing and development). The R code will be automatically +// run after the compilation. +// + +/*** R +timesTwo(42) +*/ diff --git a/tests/testthat/public-api/xyzpackage-qmd/tests/testthat.R b/tests/testthat/public-api/xyzpackage-qmd/tests/testthat.R new file mode 100644 index 000000000..89b573e70 --- /dev/null +++ b/tests/testthat/public-api/xyzpackage-qmd/tests/testthat.R @@ -0,0 +1,4 @@ +library(testthat) +library(xyzpackage) + +test_check("xyzpackage") diff --git a/tests/testthat/public-api/xyzpackage-qmd/tests/testthat/test-package-xyz.R b/tests/testthat/public-api/xyzpackage-qmd/tests/testthat/test-package-xyz.R new file mode 100644 index 000000000..23dadd10a --- /dev/null +++ b/tests/testthat/public-api/xyzpackage-qmd/tests/testthat/test-package-xyz.R @@ -0,0 +1,5 @@ +context("testing styler on package") + +test_that("hi there", { + I(am(a(package(x)))) +}) diff --git a/tests/testthat/public-api/xyzpackage-qmd/vignettes/random.Rmarkdown b/tests/testthat/public-api/xyzpackage-qmd/vignettes/random.Rmarkdown new file mode 100644 index 000000000..aace6af45 --- /dev/null +++ b/tests/testthat/public-api/xyzpackage-qmd/vignettes/random.Rmarkdown @@ -0,0 +1,58 @@ +--- +title: "Vignette Title" +author: "Vignette Author" +date: "`r Sys.Date()`" +output: rmarkdown::html_vignette +vignette: > + %\VignetteIndexEntry{Vignette Title} + %\VignetteEngine{knitr::rmarkdown} + %\VignetteEncoding{UTF-8} +--- + +Vignettes are long form documentation commonly included in packages. Because they are part of the distribution of the package, they need to be as compact as possible. The `html_vignette` output type provides a custom style sheet (and tweaks some options) to ensure that the resulting html is as small as possible. The `html_vignette` format: + +- Never uses retina figures +- Has a smaller default figure size +- Uses a custom CSS stylesheet instead of the default Twitter Bootstrap style + +## Vignette Info + +Note the various macros within the `vignette` section of the metadata block above. These are required in order to instruct R how to build the vignette. Note that you should change the `title` field and the `\VignetteIndexEntry` to match the title of your vignette. + +## Styles + +The `html_vignette` template includes a basic CSS theme. To override this theme you can specify your own CSS in the document metadata as follows: + + output: + rmarkdown::html_vignette: + css: mystyles.css + +## Figures + +The figure sizes have been customised so that you can easily put two images side-by-side. + +```{r, fig.show='hold'} +plot(1:10) +plot(10:1) +``` + +You can enable figure captions by `fig_caption: yes` in YAML: + + output: + rmarkdown::html_vignette: + fig_caption: yes + +Then you can use the chunk option `fig.cap = "Your figure caption."` in **knitr**. + +## More Examples + +You can write math expressions, e.g. $Y = X\beta + \epsilon$, footnotes^[A footnote here.], and tables, e.g. using `knitr::kable()`. + +```{r, echo=FALSE, results='asis'} +knitr::kable(head(mtcars, 10)) +``` + +Also a quote using `>`: + +> "He who gives up [code] safety for [code] speed deserves neither." +([via](https://twitter.com/hadleywickham/status/504368538874703872)) diff --git a/tests/testthat/public-api/xyzpackage-qmd/vignettes/random.Rmd b/tests/testthat/public-api/xyzpackage-qmd/vignettes/random.Rmd new file mode 100644 index 000000000..aace6af45 --- /dev/null +++ b/tests/testthat/public-api/xyzpackage-qmd/vignettes/random.Rmd @@ -0,0 +1,58 @@ +--- +title: "Vignette Title" +author: "Vignette Author" +date: "`r Sys.Date()`" +output: rmarkdown::html_vignette +vignette: > + %\VignetteIndexEntry{Vignette Title} + %\VignetteEngine{knitr::rmarkdown} + %\VignetteEncoding{UTF-8} +--- + +Vignettes are long form documentation commonly included in packages. Because they are part of the distribution of the package, they need to be as compact as possible. The `html_vignette` output type provides a custom style sheet (and tweaks some options) to ensure that the resulting html is as small as possible. The `html_vignette` format: + +- Never uses retina figures +- Has a smaller default figure size +- Uses a custom CSS stylesheet instead of the default Twitter Bootstrap style + +## Vignette Info + +Note the various macros within the `vignette` section of the metadata block above. These are required in order to instruct R how to build the vignette. Note that you should change the `title` field and the `\VignetteIndexEntry` to match the title of your vignette. + +## Styles + +The `html_vignette` template includes a basic CSS theme. To override this theme you can specify your own CSS in the document metadata as follows: + + output: + rmarkdown::html_vignette: + css: mystyles.css + +## Figures + +The figure sizes have been customised so that you can easily put two images side-by-side. + +```{r, fig.show='hold'} +plot(1:10) +plot(10:1) +``` + +You can enable figure captions by `fig_caption: yes` in YAML: + + output: + rmarkdown::html_vignette: + fig_caption: yes + +Then you can use the chunk option `fig.cap = "Your figure caption."` in **knitr**. + +## More Examples + +You can write math expressions, e.g. $Y = X\beta + \epsilon$, footnotes^[A footnote here.], and tables, e.g. using `knitr::kable()`. + +```{r, echo=FALSE, results='asis'} +knitr::kable(head(mtcars, 10)) +``` + +Also a quote using `>`: + +> "He who gives up [code] safety for [code] speed deserves neither." +([via](https://twitter.com/hadleywickham/status/504368538874703872)) diff --git a/tests/testthat/public-api/xyzpackage-qmd/xyzpackage.Rproj b/tests/testthat/public-api/xyzpackage-qmd/xyzpackage.Rproj new file mode 100644 index 000000000..d848a9ff5 --- /dev/null +++ b/tests/testthat/public-api/xyzpackage-qmd/xyzpackage.Rproj @@ -0,0 +1,16 @@ +Version: 1.0 + +RestoreWorkspace: No +SaveWorkspace: No +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +Encoding: UTF-8 + +AutoAppendNewline: Yes +StripTrailingWhitespace: Yes + +BuildType: Package +PackageUseDevtools: Yes +PackageInstallArgs: --no-multiarch --with-keep.source +PackageRoxygenize: rd,collate,namespace diff --git a/tests/testthat/test-public_api.R b/tests/testthat/test-public_api.R index cda27b549..e03a6b9e5 100644 --- a/tests/testthat/test-public_api.R +++ b/tests/testthat/test-public_api.R @@ -163,6 +163,22 @@ test_that("styler can style Rmarkdown file", { expect_false(styled$changed) }) + +test_that("styler can style qmd file", { + capture_output(expect_false({ + out <- style_file( + testthat_file("public-api", "xyzfile_qmd", "new.qmd"), + strict = FALSE + ) + out$changed + })) + + capture_output(expect_warning( + styled <- style_file(testthat_file("public-api", "xyzfile_rmd", "random2.Rmarkdown"), strict = FALSE) + )) + expect_false(styled$changed) +}) + test_that("styler handles malformed Rmd file and invalid R code in chunk", { capture_output(expect_warning( style_file(testthat_file("public-api", "xyzfile_rmd", "invalid4.Rmd"), strict = FALSE), @@ -291,6 +307,35 @@ test_that("styler can style R and Rmd files via style_pkg()", { expect_false(any(grepl("RcppExports.R", msg, fixed = TRUE))) }) +test_that("style_pkg() does not style qmd files by default", { + msg <- capture_output( + style_pkg(testthat_file("public-api", "xyzpackage-qmd")) + ) + expect_true(any(grepl("hello-world.R", msg, fixed = TRUE))) + expect_true(any(grepl("test-package-xyz.R", msg, fixed = TRUE))) + expect_false(any(grepl("random.Rmd", msg, fixed = TRUE))) + expect_false(any(grepl("random.Rmarkdown", msg, fixed = TRUE))) + expect_false(any(grepl("README.Rmd", msg, fixed = TRUE))) + expect_false(any(grepl("RcppExports.R", msg, fixed = TRUE))) + expect_false(any(grepl("new.qmd", msg, fixed = TRUE))) +}) + +test_that("style_pkg() can find qmd anywhere", { + msg <- capture_output( + style_pkg(testthat_file("public-api", "xyzpackage-qmd"), + filetype = ".Qmd" + ) + ) + expect_false(any(grepl("hello-world.R", msg, fixed = TRUE))) + expect_false(any(grepl("test-package-xyz.R", msg, fixed = TRUE))) + expect_false(any(grepl("random.Rmd", msg, fixed = TRUE))) + expect_false(any(grepl("random.Rmarkdown", msg, fixed = TRUE))) + expect_false(any(grepl("README.Rmd", msg, fixed = TRUE))) + expect_false(any(grepl("RcppExports.R", msg, fixed = TRUE))) + expect_true(any(grepl("new.qmd", msg, fixed = TRUE))) +}) + + test_that("styler can style Rmd files only via style_pkg()", { msg <- capture_output( style_pkg(testthat_file("public-api", "xyzpackage-rmd"), From 85159be7c451116e2a1210c4a0b5bfa0dc7b84dd Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Thu, 17 Mar 2022 20:16:22 +0000 Subject: [PATCH 2/4] pre-commit --- man/combine_children.Rd | 4 ++-- man/rds_to_version.Rd | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/man/combine_children.Rd b/man/combine_children.Rd index fef5d4a3a..f587b9aaf 100644 --- a/man/combine_children.Rd +++ b/man/combine_children.Rd @@ -16,8 +16,8 @@ Binds two parse tables together and arranges them so that the tokens are in the correct order. } \details{ -Essentially, this is a wrapper around \code{\link[dplyr:bind]{dplyr::bind_rows()}}, but -returns \code{NULL} if the result of \code{\link[dplyr:bind]{dplyr::bind_rows()}} is a data frame with +Essentially, this is a wrapper around \code{\link[dplyr:bind_rows]{dplyr::bind_rows()}}, but +returns \code{NULL} if the result of \code{\link[dplyr:bind_rows]{dplyr::bind_rows()}} is a data frame with zero rows. } \keyword{internal} diff --git a/man/rds_to_version.Rd b/man/rds_to_version.Rd index 59d9d15b7..5a4b236a4 100644 --- a/man/rds_to_version.Rd +++ b/man/rds_to_version.Rd @@ -12,6 +12,6 @@ rds_to_version(path, version = 2) \item{version}{The target version.} } \description{ -Needed to make \code{\link[testthat:expect_known_output]{testthat::expect_known_value()}} work on R < 3.6. +Needed to make \code{\link[testthat:expect_known_value]{testthat::expect_known_value()}} work on R < 3.6. } \keyword{internal} From 68e846d23e9893ed897ba8b894f65dbb15cde369 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Fri, 18 Mar 2022 08:25:44 +0100 Subject: [PATCH 3/4] also activate for addin --- R/addins.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/addins.R b/R/addins.R index 5e265b11f..5ed0f53c7 100644 --- a/R/addins.R +++ b/R/addins.R @@ -60,7 +60,7 @@ style_active_file <- function() { is_rprofile_file(context$path) ) - if (is_rmd_file(context$path)) { + if (is_rmd_file(context$path) || is_qmd_file(context$path)) { out <- transform_mixed(context$contents, transformer, filetype = "Rmd") } else if (is_rnw_file(context$path)) { out <- transform_mixed(context$contents, transformer, filetype = "Rnw") From 7219ef4f5c501fcee8ca72bc129e5c170924f6c2 Mon Sep 17 00:00:00 2001 From: Lorenz Walthert Date: Tue, 22 Mar 2022 08:33:12 +0100 Subject: [PATCH 4/4] use testthat regex expectations --- tests/testthat/test-public_api.R | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/testthat/test-public_api.R b/tests/testthat/test-public_api.R index e03a6b9e5..22f1262e4 100644 --- a/tests/testthat/test-public_api.R +++ b/tests/testthat/test-public_api.R @@ -326,13 +326,13 @@ test_that("style_pkg() can find qmd anywhere", { filetype = ".Qmd" ) ) - expect_false(any(grepl("hello-world.R", msg, fixed = TRUE))) - expect_false(any(grepl("test-package-xyz.R", msg, fixed = TRUE))) - expect_false(any(grepl("random.Rmd", msg, fixed = TRUE))) - expect_false(any(grepl("random.Rmarkdown", msg, fixed = TRUE))) - expect_false(any(grepl("README.Rmd", msg, fixed = TRUE))) - expect_false(any(grepl("RcppExports.R", msg, fixed = TRUE))) - expect_true(any(grepl("new.qmd", msg, fixed = TRUE))) + expect_no_match(msg, "hello-world.R", fixed = TRUE) + expect_no_match(msg, "test-package-xyz.R", fixed = TRUE) + expect_no_match(msg, "random.Rmd", fixed = TRUE) + expect_no_match(msg, "random.Rmarkdown", fixed = TRUE) + expect_no_match(msg, "README.Rmd", fixed = TRUE) + expect_no_match(msg, "RcppExports.R", fixed = TRUE) + expect_match(msg, "new.qmd", fixed = TRUE) })