Skip to content

Tweaks for the PR to r-lib/styler#463 #1

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 23 additions & 55 deletions R/addins.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,77 +97,45 @@ get_rstudio_context <- function() {
rstudioapi::getActiveDocumentContext()
}


# Dedicated binding for package styling addin. Simple wrapper calling style_pkg
# with the selected addins style.
style_package <- function() {
communicate_addins_style()
style_pkg(style = get_addins_style_fun())
}


# `match.fun`-like utility covering "ns::name".
exported_value_rx <- "^([^:]+)::([^:]+)$"
is_exported_value <- function(x) {
rlang::is_scalar_character(x) && grepl(exported_value_rx, x)
}
extract_exported_ns <- function(x) {
sub(exported_value_rx, "\\1", x)
}
extract_exported_name <- function(x) {
sub(exported_value_rx, "\\2", x)
}
match_fun <- function(x) {
if (is_exported_value(x)) {
x <-
getExportedValue(
extract_exported_ns(x),
extract_exported_name(x)
)
}
match.fun(x)
}

# Binding for style-setting addin.
#' Ask the user to supply a style
#'
#' @keywords internal
prompt_style <- function() {
current_style <- get_addins_style()
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
)
if (!is.null(new_style)) {
set_addins_style(new_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)
}

# Set/get style used by the addins.
set_addins_style <- function(style_name) {
# match_fun ensures the provided name is a valid function
invisible(match_fun(style_name))
options(
styler.addins.style = style_name
)
}

get_addins_style <- function() {
# `default` could be an environment variable
getOption(
"styler.addins.style",
default = "styler::tidyverse_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() {
match_fun(
get_addins_style()
)
eval(parse(text = get_addins_style_name()))
}

# How the addins communicate the style being used.
communicate_addins_style <- function() {
style_name <- get_addins_style()
style_name <- get_addins_style_name()
cat("Using style `", style_name, "`\n", sep = "")
}
5 changes: 0 additions & 5 deletions inst/rstudio/addins.dcf
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ Description: Prompt for and set the style used by all STYLER addins
Binding: prompt_style
Interactive: true

Name: Style package
Description: Pretty-print package source code
Binding: style_package
Interactive: true

Name: Style active file
Description: Pretty-print active file
Binding: 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.