Skip to content

Allow selecting the style used by RStudio addins. #463

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Feb 16, 2019
Merged
49 changes: 47 additions & 2 deletions R/addins.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)

Expand Down Expand Up @@ -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
Expand All @@ -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 = "")
}
6 changes: 3 additions & 3 deletions inst/rstudio/addins.dcf
Original file line number Diff line number Diff line change
@@ -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
Expand Down
15 changes: 15 additions & 0 deletions man/get_addins_style_name.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions man/prompt_style.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.