Skip to content
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

Allow selecting the style used by RStudio addins. #463

Merged
merged 11 commits into from Feb 16, 2019
@@ -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,78 @@ style_selection <- function() {
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() {

This comment has been minimized.

Copy link
@lorenzwalthert

lorenzwalthert Jan 24, 2019

Collaborator

I think we don't want another Addin, see also #250.

This comment has been minimized.

Copy link
@lorenzwalthert

lorenzwalthert May 6, 2019

Collaborator

Actually thanks go @jonmcalder we have figured out that you have not registered a new Addin, just moved existing code. I overlooked that. Sorry. Will add in #500 again.

This comment has been minimized.

Copy link
@riccardoporreca

riccardoporreca May 6, 2019

Author Contributor

I thought this was intentional, as discussed when integrating your tweaks in miraisolutions#1 (comment)

This comment has been minimized.

Copy link
@lorenzwalthert

lorenzwalthert May 6, 2019

Collaborator

Well, the confusion arose because people wanted to add other Addins like #476 and #250 and in the diff, it looked as if you added an Addin for package styling. This was not the case, you just moved some code. So that was my mistake.

This comment has been minimized.

Copy link
@riccardoporreca

riccardoporreca May 6, 2019

Author Contributor

I see, no problem!

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.
prompt_style <- function() {
current_style <- get_addins_style()
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)
}
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"
)
}

get_addins_style_fun <- function() {
match_fun(
get_addins_style()
)
}

# How the addins communicate the style being used.
communicate_addins_style <- function() {
style_name <- get_addins_style()
cat("Using style `", style_name, "`\n", sep = "")
}
@@ -1,6 +1,11 @@
Name: Set style
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_pkg
Binding: style_package
Interactive: true

Name: Style active file
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.