diff --git a/R/addins.R b/R/addins.R index 84c2587af..fb853d875 100644 --- a/R/addins.R +++ b/R/addins.R @@ -20,8 +20,9 @@ NULL #' `strict = TRUE`. #' @keywords internal style_active_file <- function() { + communicate_addins_style() context <- get_rstudio_context() - transformer <- make_transformer(tidyverse_style(), + transformer <- make_transformer(get_addins_style_fun()(), include_roxygen_examples = TRUE, warn_empty = is_plain_r_file(context$path) ) @@ -78,10 +79,11 @@ try_transform_as_r_file <- function(context, transformer) { #' `.Rmd` file. #' @keywords internal style_selection <- function() { + communicate_addins_style() context <- get_rstudio_context() text <- context$selection[[1]]$text if (all(nchar(text) == 0)) stop("No code selected") - out <- style_text(text) + out <- style_text(text, style = get_addins_style_fun()) rstudioapi::modifyRange( context$selection[[1]]$range, paste0(out, collapse = "\n"), id = context$id @@ -94,3 +96,46 @@ style_selection <- function() { get_rstudio_context <- function() { rstudioapi::getActiveDocumentContext() } + +#' Ask the user to supply a style +#' +#' @keywords internal +prompt_style <- function() { + current_style <- get_addins_style_name() + new_style <- + rstudioapi::showPrompt( + "Select a style", + "Enter the name of a style function, e.g. `styler::tidyverse_style`", + current_style + ) + parsed_new_style <- tryCatch( + eval(parse(text = new_style)), + error = function(e) { + stop("The selected style \"", new_style, "\" is not valid: ", e$message) + } + ) + if (inherits(parsed_new_style, "function")) { + options(styler.addins.style = new_style) + } else { + stop("The selected style \"", new_style, "\" is not a function.") + } + invisible(current_style) +} + +#' Return the style function or name +#' +#' @keywords internal +get_addins_style_name <- function() { + getOption("styler.addins.style", default = "styler::tidyverse_style") +} + +#' @rdname get_addins_style_name +#' @keywords internal +get_addins_style_fun <- function() { + eval(parse(text = get_addins_style_name())) +} + +communicate_addins_style <- function() { + style_name <- get_addins_style_name() + cat("Using style `", style_name, "`\n", sep = "") +} diff --git a/inst/rstudio/addins.dcf b/inst/rstudio/addins.dcf index 1735c5b11..027ff00cb 100644 --- a/inst/rstudio/addins.dcf +++ b/inst/rstudio/addins.dcf @@ -1,6 +1,6 @@ -Name: Style package -Description: Pretty-print package source code -Binding: style_pkg +Name: Set style +Description: Prompt for and set the style used by all STYLER addins +Binding: prompt_style Interactive: true Name: Style active file diff --git a/man/get_addins_style_name.Rd b/man/get_addins_style_name.Rd new file mode 100644 index 000000000..0873f3761 --- /dev/null +++ b/man/get_addins_style_name.Rd @@ -0,0 +1,15 @@ +% 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} +\alias{get_addins_style_fun} +\title{Return the style function or name} +\usage{ +get_addins_style_name() + +get_addins_style_fun() +} +\description{ +Return the style function or name +} +\keyword{internal} diff --git a/man/prompt_style.Rd b/man/prompt_style.Rd new file mode 100644 index 000000000..539d25103 --- /dev/null +++ b/man/prompt_style.Rd @@ -0,0 +1,12 @@ +% 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}