From 301a8619c4bb978cc89fc80f0dad0d0f284cf7ad Mon Sep 17 00:00:00 2001 From: lorenzwalthert Date: Sun, 28 Apr 2019 15:09:50 +0200 Subject: [PATCH 01/15] let user specify transformers, not just style argument for flexibility. Rename option styler.addins.style -> styler.addins_style_transformer. Overhaul documentumentation for Addins, add life cycle. --- .Rbuildignore | 1 + R/addins.R | 121 ++++++++++-------- inst/rstudio/addins.dcf | 4 +- ...d => get_addins_style_transformer_name.Rd} | 6 +- man/prompt_style.Rd | 12 -- man/style_selection.Rd | 12 ++ man/styler_addins.Rd | 39 ++++-- tests/testmanual/addins/non-r.py | 2 + tests/testmanual/addins/r-invalid.R | 1 + tests/testmanual/addins/r-valid.R | 1 + tests/testmanual/addins/rmd-invalid.Rmd | 30 +++++ tests/testmanual/addins/rmd-valid.Rmd | 30 +++++ tests/testmanual/addins/rnw-invalid.Rnw | 28 ++++ tests/testmanual/addins/rnw-valid.Rnw | 28 ++++ tests/testmanual/tests | 16 +++ 15 files changed, 253 insertions(+), 78 deletions(-) rename man/{get_addins_style_name.Rd => get_addins_style_transformer_name.Rd} (68%) delete mode 100644 man/prompt_style.Rd create mode 100644 man/style_selection.Rd create mode 100644 tests/testmanual/addins/non-r.py create mode 100644 tests/testmanual/addins/r-invalid.R create mode 100644 tests/testmanual/addins/r-valid.R create mode 100644 tests/testmanual/addins/rmd-invalid.Rmd create mode 100644 tests/testmanual/addins/rmd-valid.Rmd create mode 100644 tests/testmanual/addins/rnw-invalid.Rnw create mode 100644 tests/testmanual/addins/rnw-valid.Rnw create mode 100644 tests/testmanual/tests diff --git a/.Rbuildignore b/.Rbuildignore index 753470c98..a4912951e 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -16,3 +16,4 @@ CONTRIBUTING.md ^gitsum$ revdep ^cran-comments\.md$ +^tests/testmanual$ diff --git a/R/addins.R b/R/addins.R index ac63567ed..b7407f524 100644 --- a/R/addins.R +++ b/R/addins.R @@ -1,7 +1,24 @@ #' Stylers for RStudio Addins #' #' Helper functions for styling via RStudio Addins. -#' +#' @section Addins: +#' * Set style: Select the style transformers to use. For flexibility, the user +#' input is passed to the `transformers` argument, not the `style` argument, so +#' enter `styler::tidyverse_style(scope = "spaces")` in the Addin is +#' equivalent to `styler::style_text("1+1", scope = "spaces")` and +#' `styler::style_text("1+1", transformers = styler::tidyverse_style(scope = "spaces"))` +#' if the text to style is `1 + 1`. The style is memorized within an R +#' session. If you want it to persist over a session, set the option +#' `styler.addins_style_transformer` in your `.Rprofile`. +#' * Style active file: Styles the active file, by default with +#' [ tidyverse_style()] and `strict = TRUE`. You can set the style +#' with the Addin `Set style`. +#' @section Life cycle: +#' The way of specifying the style in the Addin is experimental. We currently +#' think about letting the user specify the defaults for other style APIs like +#' [styler::style_text()], either via R options, config files or other ways as +#' well. See [r-lib/styler#319](https://github.com/r-lib/styler/issues/319) for +#' the current status of this. #' @section Auto-Save Option: #' By default, both of the RStudio Addins will apply styling to the (selected) #' file contents without saving changes. Automatic saving can be enabled by @@ -16,14 +33,13 @@ #' @seealso [Sys.setenv()] NULL -#' @describeIn styler_addins Styles the active file with [tidyverse_style()] and -#' `strict = TRUE`. + #' @importFrom rlang abort #' @keywords internal style_active_file <- function() { communicate_addins_style() context <- get_rstudio_context() - transformer <- make_transformer(get_addins_style_fun()(), + transformer <- make_transformer(get_addins_style_fun(), include_roxygen_examples = TRUE, warn_empty = is_plain_r_file(context$path) ) @@ -47,38 +63,7 @@ style_active_file <- function() { rstudioapi::setCursorPosition(context$selection[[1]]$range) } -#' Style a file as if it was an .R file -#' -#' If not successful, the file is most -#' likely not a .R file, so saving the file and try styling again will work if -#' the file is an .Rmd file. Otherwise, we can throw an error that the file must -#' be a .R or .Rmd file. -#' @param context The context from `styler:::get_rstudio_context()`. -#' @param transformer A transformer function most conveniently constructed with -#' [make_transformer()]. -#' @importFrom rlang with_handlers abort -#' @keywords internal -try_transform_as_r_file <- function(context, transformer) { - with_handlers( - transformer(context$contents), - error = function(e) { - preamble_for_unsaved <- paste( - "Styling of unsaved files is only supported for R files with valid code.", - "Please save the file (as .R or .Rmd) and make sure that the R code in it", - "can be parsed. Then, try to style again." - ) - - if (context$path == "") { - abort(paste0(preamble_for_unsaved, " The error was \n", e$message)) - } else { - abort(e$message) - } - } - ) -} - -#' @describeIn styler_addins Styles the highlighted selection in a `.R` or -#' `.Rmd` file. +#' Styles the highlighted selection in a `.R` or `.Rmd` file. #' @importFrom rlang abort #' @keywords internal style_selection <- function() { @@ -100,43 +85,77 @@ get_rstudio_context <- function() { rstudioapi::getActiveDocumentContext() } -#' Ask the user to supply a style -#' +#' @describeIn styler_addins asks the user to supply a style #' @importFrom rlang abort #' @keywords internal #' @importFrom rlang with_handlers abort -prompt_style <- function() { - current_style <- get_addins_style_name() +set_style_transformers <- function() { + current_style <- get_addins_style_transformer_name() new_style <- rstudioapi::showPrompt( "Select a style", - "Enter the name of a style function, e.g. `styler::tidyverse_style`", + "Enter the name of a style transformer, e.g. `styler::tidyverse_style()`", current_style ) - parsed_new_style <- with_handlers( - eval(parse(text = new_style)), + parsed_new_style <- with_handlers({ + transformers <- eval(parse(text = new_style)) + style_text(c("a = 2", "function() {", "NULL", "}")) + }, error = function(e) { - abort(paste0("The selected style \"", new_style, "\" is not valid: ", e$message)) + abort(paste0( + "The selected style transformers \"", + new_style, "\" is not valid: ", e$message + )) } ) - options(styler.addins.style = new_style) + options(styler.addins_style_transformer = new_style) invisible(current_style) } #' Return the style function or name #' #' @keywords internal -get_addins_style_name <- function() { - getOption("styler.addins.style", default = "styler::tidyverse_style") +get_addins_style_transformer_name <- function() { + getOption("styler.addins_style_transformer", default = "styler::tidyverse_style()") } -#' @rdname get_addins_style_name +#' @rdname get_addins_style_transformer_name #' @keywords internal get_addins_style_fun <- function() { - eval(parse(text = get_addins_style_name())) + eval(parse(text = get_addins_style_transformer_name())) } communicate_addins_style <- function() { - style_name <- get_addins_style_name() - cat("Using style `", style_name, "`\n", sep = "") + style_name <- get_addins_style_transformer_name() + cat("Using style transformers`", style_name, "`\n", sep = "") +} + +#' Style a file as if it was an .R file +#' +#' If not successful, the file is most +#' likely not a .R file, so saving the file and try styling again will work if +#' the file is an .Rmd file. Otherwise, we can throw an error that the file must +#' be a .R or .Rmd file. +#' @param context The context from `styler:::get_rstudio_context()`. +#' @param transformer A transformer function most conveniently constructed with +#' [make_transformer()]. +#' @importFrom rlang with_handlers abort +#' @keywords internal +try_transform_as_r_file <- function(context, transformer) { + with_handlers( + transformer(context$contents), + error = function(e) { + preamble_for_unsaved <- paste( + "Styling of unsaved files is only supported for R files with valid code.", + "Please save the file (as .R or .Rmd) and make sure that the R code in it", + "can be parsed. Then, try to style again." + ) + + if (context$path == "") { + abort(paste0(preamble_for_unsaved, " The error was \n", e$message)) + } else { + abort(e$message) + } + } + ) } diff --git a/inst/rstudio/addins.dcf b/inst/rstudio/addins.dcf index 027ff00cb..0b524b316 100644 --- a/inst/rstudio/addins.dcf +++ b/inst/rstudio/addins.dcf @@ -1,6 +1,6 @@ Name: Set style -Description: Prompt for and set the style used by all STYLER addins -Binding: prompt_style +Description: Prompt for and set the style transformers used by all styler addins +Binding: set_style_transformers Interactive: true Name: Style active file diff --git a/man/get_addins_style_name.Rd b/man/get_addins_style_transformer_name.Rd similarity index 68% rename from man/get_addins_style_name.Rd rename to man/get_addins_style_transformer_name.Rd index 0873f3761..a5c005390 100644 --- a/man/get_addins_style_name.Rd +++ b/man/get_addins_style_transformer_name.Rd @@ -1,11 +1,11 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/addins.R -\name{get_addins_style_name} -\alias{get_addins_style_name} +\name{get_addins_style_transformer_name} +\alias{get_addins_style_transformer_name} \alias{get_addins_style_fun} \title{Return the style function or name} \usage{ -get_addins_style_name() +get_addins_style_transformer_name() get_addins_style_fun() } diff --git a/man/prompt_style.Rd b/man/prompt_style.Rd deleted file mode 100644 index 539d25103..000000000 --- a/man/prompt_style.Rd +++ /dev/null @@ -1,12 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/addins.R -\name{prompt_style} -\alias{prompt_style} -\title{Ask the user to supply a style} -\usage{ -prompt_style() -} -\description{ -Ask the user to supply a style -} -\keyword{internal} diff --git a/man/style_selection.Rd b/man/style_selection.Rd new file mode 100644 index 000000000..668695a44 --- /dev/null +++ b/man/style_selection.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/addins.R +\name{style_selection} +\alias{style_selection} +\title{Styles the highlighted selection in a \code{.R} or \code{.Rmd} file.} +\usage{ +style_selection() +} +\description{ +Styles the highlighted selection in a \code{.R} or \code{.Rmd} file. +} +\keyword{internal} diff --git a/man/styler_addins.Rd b/man/styler_addins.Rd index acddef317..1be6c4a5d 100644 --- a/man/styler_addins.Rd +++ b/man/styler_addins.Rd @@ -2,26 +2,45 @@ % Please edit documentation in R/addins.R \name{styler_addins} \alias{styler_addins} -\alias{style_active_file} -\alias{style_selection} +\alias{set_style_transformers} \title{Stylers for RStudio Addins} \usage{ -style_active_file() - -style_selection() +set_style_transformers() } \description{ Helper functions for styling via RStudio Addins. } \section{Functions}{ \itemize{ -\item \code{style_active_file}: Styles the active file with \code{\link[=tidyverse_style]{tidyverse_style()}} and -\code{strict = TRUE}. - -\item \code{style_selection}: Styles the highlighted selection in a \code{.R} or -\code{.Rmd} file. +\item \code{set_style_transformers}: asks the user to supply a style }} +\section{Addins}{ + +\itemize{ +\item Set style: Select the style transformers to use. For flexibility, the user +input is passed to the \code{transformers} argument, not the \code{style} argument, so +enter \code{styler::tidyverse_style(scope = "spaces")} in the Addin is +equivalent to \code{styler::style_text("1+1", scope = "spaces")} and +\code{styler::style_text("1+1", transformers = styler::tidyverse_style(scope = "spaces"))} +if the text to style is \code{1 + 1}. The style is memorized within an R +session. If you want it to persist over a session, set the option +\code{styler.addins_style_transformer} in your \code{.Rprofile}. +\item Style active file: Styles the active file, by default with +\code{\link[= tidyverse_style]{ tidyverse_style()}} and \code{strict = TRUE}. You can set the style +with the Addin \code{Set style}. +} +} + +\section{Life-cycle}{ + +The way of specifying the style in the Addin is experimental. We currently +think about letting the user specify the defaults for other style APIs like +\code{\link[styler:style_text]{styler::style_text()}}, either via R options, config files or other ways as +well. See \href{https://github.com/r-lib/styler/issues/319}{r-lib/styler#319} for +the current status of this. +} + \section{Auto-Save Option}{ By default, both of the RStudio Addins will apply styling to the (selected) diff --git a/tests/testmanual/addins/non-r.py b/tests/testmanual/addins/non-r.py new file mode 100644 index 000000000..381f905d2 --- /dev/null +++ b/tests/testmanual/addins/non-r.py @@ -0,0 +1,2 @@ +2 + 2 +" " diff --git a/tests/testmanual/addins/r-invalid.R b/tests/testmanual/addins/r-invalid.R new file mode 100644 index 000000000..88d5c0639 --- /dev/null +++ b/tests/testmanual/addins/r-invalid.R @@ -0,0 +1 @@ +1+ /1 diff --git a/tests/testmanual/addins/r-valid.R b/tests/testmanual/addins/r-valid.R new file mode 100644 index 000000000..612dabf52 --- /dev/null +++ b/tests/testmanual/addins/r-valid.R @@ -0,0 +1 @@ +1+ 1 diff --git a/tests/testmanual/addins/rmd-invalid.Rmd b/tests/testmanual/addins/rmd-invalid.Rmd new file mode 100644 index 000000000..5b7c2f3ee --- /dev/null +++ b/tests/testmanual/addins/rmd-invalid.Rmd @@ -0,0 +1,30 @@ +--- +title: "Untitled" +author: "Lorenz Walthert" +date: "4/28/2019" +output: html_document +--- + +```{r setup, include=FALSE} +knitr::opts_chunk$setecho = -TRUE) +``` + +## R Markdown + +This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see . + +When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this: + +```{r cars} +summary(cars ) +``` + +## Including Plots + +You can also embed plots, for example: + +```{r pressure, echo=FALSE} +plot(pressure) +``` + +Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot. diff --git a/tests/testmanual/addins/rmd-valid.Rmd b/tests/testmanual/addins/rmd-valid.Rmd new file mode 100644 index 000000000..b92ffe928 --- /dev/null +++ b/tests/testmanual/addins/rmd-valid.Rmd @@ -0,0 +1,30 @@ +--- +title: "Untitled" +author: "Lorenz Walthert" +date: "4/28/2019" +output: html_document +--- + +```{r setup, include=FALSE} +knitr::opts_chunk$set(echo = -TRUE) +``` + +## R Markdown + +This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see . + +When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this: + +```{r cars} +summary(cars) +``` + +## Including Plots + +You can also embed plots, for example: + +```{r pressure, echo=FALSE} +plot(pressure) +``` + +Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot. diff --git a/tests/testmanual/addins/rnw-invalid.Rnw b/tests/testmanual/addins/rnw-invalid.Rnw new file mode 100644 index 000000000..046415c2c --- /dev/null +++ b/tests/testmanual/addins/rnw-invalid.Rnw @@ -0,0 +1,28 @@ +\documentclass{article} + +\begin{document} + +Some text +<>>= +# Some R code +f <- function(x) { + x +} +@ + +More text + +<<>>= +# More R code +g <- function(y) { + y +} +@ + +Final text +<<>>= +1 + 2 +@ + + +\end{document} diff --git a/tests/testmanual/addins/rnw-valid.Rnw b/tests/testmanual/addins/rnw-valid.Rnw new file mode 100644 index 000000000..e964baaf9 --- /dev/null +++ b/tests/testmanual/addins/rnw-valid.Rnw @@ -0,0 +1,28 @@ +\documentclass{article} + +\begin{document} + +Some text +<<>>= +# Some R code +f <- function(x) { + x +} +@ + +More text + +<<>>= +# More R code +g <- function(y) { + y +} +@ + +Final text +<<>>= +1 + 2 +@ + + +\end{document} diff --git a/tests/testmanual/tests b/tests/testmanual/tests new file mode 100644 index 000000000..09081c041 --- /dev/null +++ b/tests/testmanual/tests @@ -0,0 +1,16 @@ +# Manual tests + +## Addins + +* set style: + - test setting a valid stlye + - test setting an invalid style +* style active file: + - saved .R file (valid and invalid code) + - unsaved .R file + - saved .Rmd file (valid and invalid code) + - unsaved .Rmd file + - saved .Rnw file (valid and invalid code) + - unsaved .Rnw file + - saved non-R file + - unsaved R file From ad492f53c94a5326f07a80fbf351b70bb999fd12 Mon Sep 17 00:00:00 2001 From: lorenzwalthert Date: Sun, 28 Apr 2019 16:32:35 +0200 Subject: [PATCH 02/15] fix function reference --- R/addins.R | 2 +- R/style-guides.R | 1 - man/set_style_transformers.Rd | 12 ++++++++++++ man/styler_addins.Rd | 12 +----------- 4 files changed, 14 insertions(+), 13 deletions(-) create mode 100644 man/set_style_transformers.Rd diff --git a/R/addins.R b/R/addins.R index b7407f524..6fb8a6490 100644 --- a/R/addins.R +++ b/R/addins.R @@ -85,7 +85,7 @@ get_rstudio_context <- function() { rstudioapi::getActiveDocumentContext() } -#' @describeIn styler_addins asks the user to supply a style +#' Asks the user to supply a style #' @importFrom rlang abort #' @keywords internal #' @importFrom rlang with_handlers abort diff --git a/R/style-guides.R b/R/style-guides.R index 89980ff83..385d1c38b 100644 --- a/R/style-guides.R +++ b/R/style-guides.R @@ -29,7 +29,6 @@ NULL #' @inheritParams create_style_guide #' @param math_token_spacing A list of parameters that define spacing around #' math token, conveniently constructed using [specify_math_token_spacing()]. - #' @details The following options for `scope` are available. #' #' * "none": Performs no transformation at all. diff --git a/man/set_style_transformers.Rd b/man/set_style_transformers.Rd new file mode 100644 index 000000000..410693bb3 --- /dev/null +++ b/man/set_style_transformers.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/addins.R +\name{set_style_transformers} +\alias{set_style_transformers} +\title{Asks the user to supply a style} +\usage{ +set_style_transformers() +} +\description{ +Asks the user to supply a style +} +\keyword{internal} diff --git a/man/styler_addins.Rd b/man/styler_addins.Rd index 1be6c4a5d..0ed2dce3b 100644 --- a/man/styler_addins.Rd +++ b/man/styler_addins.Rd @@ -2,19 +2,10 @@ % Please edit documentation in R/addins.R \name{styler_addins} \alias{styler_addins} -\alias{set_style_transformers} \title{Stylers for RStudio Addins} -\usage{ -set_style_transformers() -} \description{ Helper functions for styling via RStudio Addins. } -\section{Functions}{ -\itemize{ -\item \code{set_style_transformers}: asks the user to supply a style -}} - \section{Addins}{ \itemize{ @@ -32,7 +23,7 @@ with the Addin \code{Set style}. } } -\section{Life-cycle}{ +\section{Life cycle}{ The way of specifying the style in the Addin is experimental. We currently think about letting the user specify the defaults for other style APIs like @@ -60,4 +51,3 @@ Other stylers: \code{\link{style_dir}}, \code{\link{style_text}} } \concept{stylers} -\keyword{internal} From da092bbad3c5ff83a05f9eee43d85a3dcd78efc0 Mon Sep 17 00:00:00 2001 From: lorenzwalthert Date: Sun, 28 Apr 2019 16:33:46 +0200 Subject: [PATCH 03/15] fix ci --- tic.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tic.R b/tic.R index b23d448ac..25e33dea2 100644 --- a/tic.R +++ b/tic.R @@ -1,4 +1,4 @@ -add_package_checks(error_on = ifelse(getRversion() >= "3.2", "note", "error")) +do_package_checks(error_on = ifelse(getRversion() >= "3.2", "note", "error")) if (Sys.getenv("id_rsa") != "" && ci()$get_branch() == "master" && Sys.getenv("BUILD_PKGDOWN") != "") { # pkgdown documentation can be built optionally. Other example criteria: From 5b8f64c3626469292e6b0a77e6ec26a909dfb4c7 Mon Sep 17 00:00:00 2001 From: lorenzwalthert Date: Tue, 30 Apr 2019 21:02:36 +0200 Subject: [PATCH 04/15] hotfix addin for selection styling. --- R/addins.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/addins.R b/R/addins.R index 6fb8a6490..375518748 100644 --- a/R/addins.R +++ b/R/addins.R @@ -71,7 +71,7 @@ style_selection <- function() { context <- get_rstudio_context() text <- context$selection[[1]]$text if (all(nchar(text) == 0)) abort("No code selected") - out <- style_text(text, style = get_addins_style_fun()) + out <- style_text(text, transformers = get_addins_style_fun()) rstudioapi::modifyRange( context$selection[[1]]$range, paste0(out, collapse = "\n"), id = context$id From 1ded3d54867691d8d212c08ba944b133fef80c85 Mon Sep 17 00:00:00 2001 From: lorenzwalthert Date: Tue, 30 Apr 2019 21:24:38 +0200 Subject: [PATCH 05/15] add manual test instructions for selection styling. --- tests/testmanual/tests | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/testmanual/tests b/tests/testmanual/tests index 09081c041..e54f0373b 100644 --- a/tests/testmanual/tests +++ b/tests/testmanual/tests @@ -14,3 +14,13 @@ - unsaved .Rnw file - saved non-R file - unsaved R file +* style selection: + - saved .R file (valid and invalid code) + - unsaved .R file + - saved .Rmd file (valid and invalid code) + - unsaved .Rmd file + - saved .Rnw file (valid and invalid code) + - unsaved .Rnw file + - saved non-R file + - unsaved R file + From c3bc20201c31cb28aeda9e33f862bd1e9ed7984a Mon Sep 17 00:00:00 2001 From: lorenzwalthert Date: Tue, 30 Apr 2019 21:29:36 +0200 Subject: [PATCH 06/15] typo --- R/addins.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/addins.R b/R/addins.R index 375518748..b7a904ad0 100644 --- a/R/addins.R +++ b/R/addins.R @@ -127,7 +127,7 @@ get_addins_style_fun <- function() { communicate_addins_style <- function() { style_name <- get_addins_style_transformer_name() - cat("Using style transformers`", style_name, "`\n", sep = "") + cat("Using style transformers `", style_name, "`\n", sep = "") } #' Style a file as if it was an .R file From efabfd4c64ded614ae172b74b0fe208540ed41db Mon Sep 17 00:00:00 2001 From: lorenzwalthert Date: Fri, 3 May 2019 08:47:38 +0200 Subject: [PATCH 07/15] fix ref to tidyverse style --- R/addins.R | 2 +- man/styler_addins.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/addins.R b/R/addins.R index b7a904ad0..c45e937d3 100644 --- a/R/addins.R +++ b/R/addins.R @@ -11,7 +11,7 @@ #' session. If you want it to persist over a session, set the option #' `styler.addins_style_transformer` in your `.Rprofile`. #' * Style active file: Styles the active file, by default with -#' [ tidyverse_style()] and `strict = TRUE`. You can set the style +#' [tidyverse_style()] and `strict = TRUE`. You can set the style #' with the Addin `Set style`. #' @section Life cycle: #' The way of specifying the style in the Addin is experimental. We currently diff --git a/man/styler_addins.Rd b/man/styler_addins.Rd index 0ed2dce3b..ad8a08571 100644 --- a/man/styler_addins.Rd +++ b/man/styler_addins.Rd @@ -18,7 +18,7 @@ if the text to style is \code{1 + 1}. The style is memorized within an R session. If you want it to persist over a session, set the option \code{styler.addins_style_transformer} in your \code{.Rprofile}. \item Style active file: Styles the active file, by default with -\code{\link[= tidyverse_style]{ tidyverse_style()}} and \code{strict = TRUE}. You can set the style +\code{\link[=tidyverse_style]{tidyverse_style()}} and \code{strict = TRUE}. You can set the style with the Addin \code{Set style}. } } From a246b1edc3f5022d87ec31b7d8182e3b6ccec17e Mon Sep 17 00:00:00 2001 From: lorenzwalthert Date: Fri, 3 May 2019 09:08:28 +0200 Subject: [PATCH 08/15] don't visibly return rstudioapi::documentSave() --- R/addins.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/addins.R b/R/addins.R index c45e937d3..62a85eabd 100644 --- a/R/addins.R +++ b/R/addins.R @@ -77,7 +77,7 @@ style_selection <- function() { id = context$id ) if (Sys.getenv("save_after_styling") == TRUE && context$path != "") { - rstudioapi::documentSave(context$id) + invisible(rstudioapi::documentSave(context$id)) } } From abcef958a5ac6dff72ecc0440069d55526cd1992 Mon Sep 17 00:00:00 2001 From: lorenzwalthert Date: Fri, 3 May 2019 10:19:08 +0200 Subject: [PATCH 09/15] better Addin explanation --- R/addins.R | 15 +++++++++------ man/styler_addins.Rd | 11 +++++++---- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/R/addins.R b/R/addins.R index 62a85eabd..a6ad39b77 100644 --- a/R/addins.R +++ b/R/addins.R @@ -2,17 +2,20 @@ #' #' Helper functions for styling via RStudio Addins. #' @section Addins: -#' * Set style: Select the style transformers to use. For flexibility, the user +#' - Set style: Select the style transformers to use. For flexibility, the user #' input is passed to the `transformers` argument, not the `style` argument, so #' enter `styler::tidyverse_style(scope = "spaces")` in the Addin is #' equivalent to `styler::style_text("1+1", scope = "spaces")` and #' `styler::style_text("1+1", transformers = styler::tidyverse_style(scope = "spaces"))` -#' if the text to style is `1 + 1`. The style is memorized within an R -#' session. If you want it to persist over a session, set the option +#' if the text to style is `1 + 1`. The style transformers are memorized +#' within an R session via the R option `styler.addins_style_transformer` so +#' if you want it to persist over sessions, set the option #' `styler.addins_style_transformer` in your `.Rprofile`. -#' * Style active file: Styles the active file, by default with -#' [tidyverse_style()] and `strict = TRUE`. You can set the style -#' with the Addin `Set style`. +#' - Style active file: Styles the active file, by default with +#' [tidyverse_style()] and `strict = TRUE` or the value of the option +#' `styler.addins_style_transformer` if specified. +#' - Style selection: Same as *style active file*, but styles the highlighted +#' code instead of the whole file. #' @section Life cycle: #' The way of specifying the style in the Addin is experimental. We currently #' think about letting the user specify the defaults for other style APIs like diff --git a/man/styler_addins.Rd b/man/styler_addins.Rd index ad8a08571..e313d3e62 100644 --- a/man/styler_addins.Rd +++ b/man/styler_addins.Rd @@ -14,12 +14,15 @@ input is passed to the \code{transformers} argument, not the \code{style} argume enter \code{styler::tidyverse_style(scope = "spaces")} in the Addin is equivalent to \code{styler::style_text("1+1", scope = "spaces")} and \code{styler::style_text("1+1", transformers = styler::tidyverse_style(scope = "spaces"))} -if the text to style is \code{1 + 1}. The style is memorized within an R -session. If you want it to persist over a session, set the option +if the text to style is \code{1 + 1}. The style transformers are memorized +within an R session via the R option \code{styler.addins_style_transformer} so +if you want it to persist over sessions, set the option \code{styler.addins_style_transformer} in your \code{.Rprofile}. \item Style active file: Styles the active file, by default with -\code{\link[=tidyverse_style]{tidyverse_style()}} and \code{strict = TRUE}. You can set the style -with the Addin \code{Set style}. +\code{\link[=tidyverse_style]{tidyverse_style()}} and \code{strict = TRUE} or the value of the option +\code{styler.addins_style_transformer} if specified. +\item Style selection: Same as \emph{style active file}, but styles the highlighted +code instead of the whole file. } } From 4f18f0b6dc5eaf2196518bda8fb0a4751a899c96 Mon Sep 17 00:00:00 2001 From: lorenzwalthert Date: Fri, 3 May 2019 10:27:10 +0200 Subject: [PATCH 10/15] another round of doc improvement --- R/addins.R | 12 ++++++++++-- man/styler_addins.Rd | 14 +++++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/R/addins.R b/R/addins.R index a6ad39b77..93090b8bb 100644 --- a/R/addins.R +++ b/R/addins.R @@ -4,7 +4,7 @@ #' @section Addins: #' - Set style: Select the style transformers to use. For flexibility, the user #' input is passed to the `transformers` argument, not the `style` argument, so -#' enter `styler::tidyverse_style(scope = "spaces")` in the Addin is +#' entering `styler::tidyverse_style(scope = "spaces")` in the Addin is #' equivalent to `styler::style_text("1+1", scope = "spaces")` and #' `styler::style_text("1+1", transformers = styler::tidyverse_style(scope = "spaces"))` #' if the text to style is `1 + 1`. The style transformers are memorized @@ -33,7 +33,15 @@ #' #' @name styler_addins #' @family stylers -#' @seealso [Sys.setenv()] +#' @examples +#' \dontrun{ +#' # save after styling when using the Addin +#' Sys.setenv(save_after_styling = TRUE) +#' # only style with scope = "spaces" when using the Addin +#' options( +#' styler.addins_style_transformer = "styler::tidyverse_style(scope = 'spaces')" +#' ) +#' } NULL diff --git a/man/styler_addins.Rd b/man/styler_addins.Rd index e313d3e62..48254cf48 100644 --- a/man/styler_addins.Rd +++ b/man/styler_addins.Rd @@ -11,7 +11,7 @@ Helper functions for styling via RStudio Addins. \itemize{ \item Set style: Select the style transformers to use. For flexibility, the user input is passed to the \code{transformers} argument, not the \code{style} argument, so -enter \code{styler::tidyverse_style(scope = "spaces")} in the Addin is +entering \code{styler::tidyverse_style(scope = "spaces")} in the Addin is equivalent to \code{styler::style_text("1+1", scope = "spaces")} and \code{styler::style_text("1+1", transformers = styler::tidyverse_style(scope = "spaces"))} if the text to style is \code{1 + 1}. The style transformers are memorized @@ -46,9 +46,17 @@ this setting across multiple sessions. Untitled files will always need to be saved manually after styling. } +\examples{ +\dontrun{ +# save after styling when using the Addin +Sys.setenv(save_after_styling = TRUE) +# only style with scope = "spaces" when using the Addin +options( + styler.addins_style_transformer = "styler::tidyverse_style(scope = 'spaces')" +) +} +} \seealso{ -\code{\link[=Sys.setenv]{Sys.setenv()}} - Other stylers: \code{\link{style_dir}}, \code{\link{style_file}}, \code{\link{style_pkg}}, \code{\link{style_text}} From a75ab55a689875a5368524ce4197f8647d0bd530 Mon Sep 17 00:00:00 2001 From: lorenzwalthert Date: Fri, 3 May 2019 10:41:28 +0200 Subject: [PATCH 11/15] re-arrange life cylce and auto save --- R/addins.R | 21 ++++++++++----------- man/styler_addins.Rd | 26 +++++++++++++------------- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/R/addins.R b/R/addins.R index 93090b8bb..1808afaa3 100644 --- a/R/addins.R +++ b/R/addins.R @@ -7,30 +7,29 @@ #' entering `styler::tidyverse_style(scope = "spaces")` in the Addin is #' equivalent to `styler::style_text("1+1", scope = "spaces")` and #' `styler::style_text("1+1", transformers = styler::tidyverse_style(scope = "spaces"))` -#' if the text to style is `1 + 1`. The style transformers are memorized +#' if the text to style is `1+1`. The style transformers are memorized #' within an R session via the R option `styler.addins_style_transformer` so #' if you want it to persist over sessions, set the option #' `styler.addins_style_transformer` in your `.Rprofile`. #' - Style active file: Styles the active file, by default with -#' [tidyverse_style()] and `strict = TRUE` or the value of the option +#' [tidyverse_style()] or the value of the option #' `styler.addins_style_transformer` if specified. -#' - Style selection: Same as *style active file*, but styles the highlighted +#' - Style selection: Same as *Style active file*, but styles the highlighted #' code instead of the whole file. -#' @section Life cycle: -#' The way of specifying the style in the Addin is experimental. We currently -#' think about letting the user specify the defaults for other style APIs like -#' [styler::style_text()], either via R options, config files or other ways as -#' well. See [r-lib/styler#319](https://github.com/r-lib/styler/issues/319) for -#' the current status of this. #' @section Auto-Save Option: #' By default, both of the RStudio Addins will apply styling to the (selected) #' file contents without saving changes. Automatic saving can be enabled by #' setting the environment variable `save_after_styling` to `TRUE`. -#' #' Consider setting this in your `.Rprofile` file if you want to persist #' this setting across multiple sessions. Untitled files will always need to be #' saved manually after styling. -#' +#' @section Life cycle: +#' The way of specifying the style as well as the auto-save option (see below) +#' in the Addin are experimental. We currently +#' think about letting the user specify the defaults for other style APIs like +#' [styler::style_text()], either via R options, config files or other ways as +#' well. See [r-lib/styler#319](https://github.com/r-lib/styler/issues/319) for +#' the current status of this. #' @name styler_addins #' @family stylers #' @examples diff --git a/man/styler_addins.Rd b/man/styler_addins.Rd index 48254cf48..659dd0db4 100644 --- a/man/styler_addins.Rd +++ b/man/styler_addins.Rd @@ -14,38 +14,38 @@ input is passed to the \code{transformers} argument, not the \code{style} argume entering \code{styler::tidyverse_style(scope = "spaces")} in the Addin is equivalent to \code{styler::style_text("1+1", scope = "spaces")} and \code{styler::style_text("1+1", transformers = styler::tidyverse_style(scope = "spaces"))} -if the text to style is \code{1 + 1}. The style transformers are memorized +if the text to style is \code{1+1}. The style transformers are memorized within an R session via the R option \code{styler.addins_style_transformer} so if you want it to persist over sessions, set the option \code{styler.addins_style_transformer} in your \code{.Rprofile}. \item Style active file: Styles the active file, by default with -\code{\link[=tidyverse_style]{tidyverse_style()}} and \code{strict = TRUE} or the value of the option +\code{\link[=tidyverse_style]{tidyverse_style()}} or the value of the option \code{styler.addins_style_transformer} if specified. -\item Style selection: Same as \emph{style active file}, but styles the highlighted +\item Style selection: Same as \emph{Style active file}, but styles the highlighted code instead of the whole file. } } -\section{Life cycle}{ - -The way of specifying the style in the Addin is experimental. We currently -think about letting the user specify the defaults for other style APIs like -\code{\link[styler:style_text]{styler::style_text()}}, either via R options, config files or other ways as -well. See \href{https://github.com/r-lib/styler/issues/319}{r-lib/styler#319} for -the current status of this. -} - \section{Auto-Save Option}{ By default, both of the RStudio Addins will apply styling to the (selected) file contents without saving changes. Automatic saving can be enabled by setting the environment variable \code{save_after_styling} to \code{TRUE}. - Consider setting this in your \code{.Rprofile} file if you want to persist this setting across multiple sessions. Untitled files will always need to be saved manually after styling. } +\section{Life cycle}{ + +The way of specifying the style as well as the auto-save option (see below) +in the Addin are experimental. We currently +think about letting the user specify the defaults for other style APIs like +\code{\link[styler:style_text]{styler::style_text()}}, either via R options, config files or other ways as +well. See \href{https://github.com/r-lib/styler/issues/319}{r-lib/styler#319} for +the current status of this. +} + \examples{ \dontrun{ # save after styling when using the Addin From 37750c770e9ee54c9fb02350e15b30587549f379 Mon Sep 17 00:00:00 2001 From: lorenzwalthert Date: Fri, 3 May 2019 10:43:42 +0200 Subject: [PATCH 12/15] re-arrange doc --- R/addins.R | 10 +++++----- man/styler_addins.Rd | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/R/addins.R b/R/addins.R index 1808afaa3..6d3b16691 100644 --- a/R/addins.R +++ b/R/addins.R @@ -24,11 +24,11 @@ #' this setting across multiple sessions. Untitled files will always need to be #' saved manually after styling. #' @section Life cycle: -#' The way of specifying the style as well as the auto-save option (see below) -#' in the Addin are experimental. We currently -#' think about letting the user specify the defaults for other style APIs like -#' [styler::style_text()], either via R options, config files or other ways as -#' well. See [r-lib/styler#319](https://github.com/r-lib/styler/issues/319) for +#' The way of specifying the style in the Addin as well as the auto-save option +#' (see below) are experimental. We currently think about letting the user +#' specify the defaults for other style APIs like [styler::style_text()], +#' either via R options, config files or other ways as well. +#' See [r-lib/styler#319](https://github.com/r-lib/styler/issues/319) for #' the current status of this. #' @name styler_addins #' @family stylers diff --git a/man/styler_addins.Rd b/man/styler_addins.Rd index 659dd0db4..ed585a1de 100644 --- a/man/styler_addins.Rd +++ b/man/styler_addins.Rd @@ -38,11 +38,11 @@ saved manually after styling. \section{Life cycle}{ -The way of specifying the style as well as the auto-save option (see below) -in the Addin are experimental. We currently -think about letting the user specify the defaults for other style APIs like -\code{\link[styler:style_text]{styler::style_text()}}, either via R options, config files or other ways as -well. See \href{https://github.com/r-lib/styler/issues/319}{r-lib/styler#319} for +The way of specifying the style in the Addin as well as the auto-save option +(see below) are experimental. We currently think about letting the user +specify the defaults for other style APIs like \code{\link[styler:style_text]{styler::style_text()}}, +either via R options, config files or other ways as well. +See \href{https://github.com/r-lib/styler/issues/319}{r-lib/styler#319} for the current status of this. } From 605e71ee5546dc666d2315aefe507e732408281f Mon Sep 17 00:00:00 2001 From: Jon Calder Date: Sun, 5 May 2019 15:50:00 +0200 Subject: [PATCH 13/15] Update R/addins.R Co-Authored-By: lorenzwalthert --- R/addins.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/addins.R b/R/addins.R index 6d3b16691..c91c0e4ad 100644 --- a/R/addins.R +++ b/R/addins.R @@ -25,7 +25,7 @@ #' saved manually after styling. #' @section Life cycle: #' The way of specifying the style in the Addin as well as the auto-save option -#' (see below) are experimental. We currently think about letting the user +#' (see below) are experimental. We are currently considering letting the user #' specify the defaults for other style APIs like [styler::style_text()], #' either via R options, config files or other ways as well. #' See [r-lib/styler#319](https://github.com/r-lib/styler/issues/319) for From 259192399b5270b6961e773a2c9590c0caba178e Mon Sep 17 00:00:00 2001 From: lorenzwalthert Date: Mon, 6 May 2019 19:13:41 +0200 Subject: [PATCH 14/15] renaming for consistency, re-add style pkg addin. --- R/addins.R | 19 +++++++++++++------ inst/rstudio/addins.dcf | 11 ++++++++--- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/R/addins.R b/R/addins.R index 6d3b16691..695132b98 100644 --- a/R/addins.R +++ b/R/addins.R @@ -47,9 +47,9 @@ NULL #' @importFrom rlang abort #' @keywords internal style_active_file <- function() { - communicate_addins_style() + communicate_addins_style_transformers() context <- get_rstudio_context() - transformer <- make_transformer(get_addins_style_fun(), + transformer <- make_transformer(get_addins_style_transformer(), include_roxygen_examples = TRUE, warn_empty = is_plain_r_file(context$path) ) @@ -73,15 +73,22 @@ style_active_file <- function() { rstudioapi::setCursorPosition(context$selection[[1]]$range) } +#' Wrapper around [style_pkg()] for access via Addin. +#' @keywords internal +style_active_pkg <- function() { + communicate_addins_style_transformers() + style_pkg(transformers = get_addins_style_transformer()) +} + #' Styles the highlighted selection in a `.R` or `.Rmd` file. #' @importFrom rlang abort #' @keywords internal style_selection <- function() { - communicate_addins_style() + communicate_addins_style_transformers() context <- get_rstudio_context() text <- context$selection[[1]]$text if (all(nchar(text) == 0)) abort("No code selected") - out <- style_text(text, transformers = get_addins_style_fun()) + out <- style_text(text, transformers = get_addins_style_transformer()) rstudioapi::modifyRange( context$selection[[1]]$range, paste0(out, collapse = "\n"), id = context$id @@ -131,11 +138,11 @@ get_addins_style_transformer_name <- function() { #' @rdname get_addins_style_transformer_name #' @keywords internal -get_addins_style_fun <- function() { +get_addins_style_transformer <- function() { eval(parse(text = get_addins_style_transformer_name())) } -communicate_addins_style <- function() { +communicate_addins_style_transformers <- function() { style_name <- get_addins_style_transformer_name() cat("Using style transformers `", style_name, "`\n", sep = "") } diff --git a/inst/rstudio/addins.dcf b/inst/rstudio/addins.dcf index 0b524b316..fcef8f90d 100644 --- a/inst/rstudio/addins.dcf +++ b/inst/rstudio/addins.dcf @@ -3,12 +3,17 @@ Description: Prompt for and set the style transformers used by all styler addins Binding: set_style_transformers Interactive: true +Name: Style selection +Description: Pretty-print selection +Binding: style_selection +Interactive: true + Name: Style active file Description: Pretty-print active file Binding: style_active_file Interactive: true -Name: Style selection -Description: Pretty-print selection -Binding: style_selection +Name: Style active package +Description: Pretty-print active package +Binding: style_active_pkg Interactive: true From 941506b900959d7e24257dbb77f9c6ed1b8f2650 Mon Sep 17 00:00:00 2001 From: lorenzwalthert Date: Mon, 6 May 2019 19:30:22 +0200 Subject: [PATCH 15/15] document --- man/get_addins_style_transformer_name.Rd | 4 ++-- man/style_active_pkg.Rd | 12 ++++++++++++ man/styler_addins.Rd | 2 +- 3 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 man/style_active_pkg.Rd diff --git a/man/get_addins_style_transformer_name.Rd b/man/get_addins_style_transformer_name.Rd index a5c005390..deccf5ff3 100644 --- a/man/get_addins_style_transformer_name.Rd +++ b/man/get_addins_style_transformer_name.Rd @@ -2,12 +2,12 @@ % Please edit documentation in R/addins.R \name{get_addins_style_transformer_name} \alias{get_addins_style_transformer_name} -\alias{get_addins_style_fun} +\alias{get_addins_style_transformer} \title{Return the style function or name} \usage{ get_addins_style_transformer_name() -get_addins_style_fun() +get_addins_style_transformer() } \description{ Return the style function or name diff --git a/man/style_active_pkg.Rd b/man/style_active_pkg.Rd new file mode 100644 index 000000000..d16e22270 --- /dev/null +++ b/man/style_active_pkg.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/addins.R +\name{style_active_pkg} +\alias{style_active_pkg} +\title{Wrapper around \code{\link[=style_pkg]{style_pkg()}} for access via Addin.} +\usage{ +style_active_pkg() +} +\description{ +Wrapper around \code{\link[=style_pkg]{style_pkg()}} for access via Addin. +} +\keyword{internal} diff --git a/man/styler_addins.Rd b/man/styler_addins.Rd index ed585a1de..78a8d6b7b 100644 --- a/man/styler_addins.Rd +++ b/man/styler_addins.Rd @@ -39,7 +39,7 @@ saved manually after styling. \section{Life cycle}{ The way of specifying the style in the Addin as well as the auto-save option -(see below) are experimental. We currently think about letting the user +(see below) are experimental. We are currently considering letting the user specify the defaults for other style APIs like \code{\link[styler:style_text]{styler::style_text()}}, either via R options, config files or other ways as well. See \href{https://github.com/r-lib/styler/issues/319}{r-lib/styler#319} for