Skip to content

Commit 04b62c0

Browse files
Allow selecting the style used by RStudio addins.
* "styler.addins.style" option stores the selected style. * New addin to set the style. * Existing addins adapted to use the selected style.
1 parent 4b7ea1b commit 04b62c0

File tree

2 files changed

+85
-3
lines changed

2 files changed

+85
-3
lines changed

R/addins.R

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ NULL
2020
#' `strict = TRUE`.
2121
#' @keywords internal
2222
style_active_file <- function() {
23+
communicate_addins_style()
2324
context <- get_rstudio_context()
24-
transformer <- make_transformer(tidyverse_style(),
25+
transformer <- make_transformer(get_addins_style_fun()(),
2526
include_roxygen_examples = TRUE, warn_empty = is_plain_r_file(context$path)
2627
)
2728

@@ -78,10 +79,11 @@ try_transform_as_r_file <- function(context, transformer) {
7879
#' `.Rmd` file.
7980
#' @keywords internal
8081
style_selection <- function() {
82+
communicate_addins_style()
8183
context <- get_rstudio_context()
8284
text <- context$selection[[1]]$text
8385
if (all(nchar(text) == 0)) stop("No code selected")
84-
out <- style_text(text)
86+
out <- style_text(text, style = get_addins_style_fun())
8587
rstudioapi::modifyRange(
8688
context$selection[[1]]$range, paste0(out, collapse = "\n"),
8789
id = context$id
@@ -94,3 +96,78 @@ style_selection <- function() {
9496
get_rstudio_context <- function() {
9597
rstudioapi::getActiveDocumentContext()
9698
}
99+
100+
101+
# Dedicated binding for package styling addin. Simple wrapper calling style_pkg
102+
# with the selected addins style.
103+
style_package <- function() {
104+
communicate_addins_style()
105+
style_pkg(style = get_addins_style_fun())
106+
}
107+
108+
109+
# `match.fun`-like utility covering "ns::name".
110+
exported_value_rx <- "^([^:]+)::([^:]+)$"
111+
is_exported_value <- function(x) {
112+
rlang::is_scalar_character(x) && grepl(exported_value_rx, x)
113+
}
114+
extract_exported_ns <- function(x) {
115+
sub(exported_value_rx, "\\1", x)
116+
}
117+
extract_exported_name <- function(x) {
118+
sub(exported_value_rx, "\\2", x)
119+
}
120+
match_fun <- function(x) {
121+
if (is_exported_value(x)) {
122+
x <-
123+
getExportedValue(
124+
extract_exported_ns(x),
125+
extract_exported_name(x)
126+
)
127+
}
128+
match.fun(x)
129+
}
130+
131+
# Binding for style-setting addin.
132+
prompt_style <- function() {
133+
current_style <- get_addins_style()
134+
new_style <-
135+
rstudioapi::showPrompt(
136+
"Select a style",
137+
"Enter the name of a style function, e.g. `styler::tidyverse_style`",
138+
current_style
139+
)
140+
if (!is.null(new_style)) {
141+
set_addins_style(new_style)
142+
}
143+
invisible(current_style)
144+
}
145+
146+
# Set/get style used by the addins.
147+
set_addins_style <- function(style_name) {
148+
# match_fun ensures the provided name is a valid function
149+
invisible(match_fun(style_name))
150+
options(
151+
styler.addins.style = style_name
152+
)
153+
}
154+
155+
get_addins_style <- function() {
156+
# `default` could be an environment variable
157+
getOption(
158+
"styler.addins.style",
159+
default = "styler::tidyverse_style"
160+
)
161+
}
162+
163+
get_addins_style_fun <- function() {
164+
match_fun(
165+
get_addins_style()
166+
)
167+
}
168+
169+
# How the addins communicate the style being used.
170+
communicate_addins_style <- function() {
171+
style_name <- get_addins_style()
172+
cat("Using style `", style_name, "`\n", sep = "")
173+
}

inst/rstudio/addins.dcf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
Name: Set style
2+
Description: Prompt for and set the style used by all STYLER addins
3+
Binding: prompt_style
4+
Interactive: true
5+
16
Name: Style package
27
Description: Pretty-print package source code
3-
Binding: style_pkg
8+
Binding: style_package
49
Interactive: true
510

611
Name: Style active file

0 commit comments

Comments
 (0)