Skip to content

Commit 1a58cac

Browse files
Merge pull request #500 from lorenzwalthert/addin-select-style
- More flexibility with Addin (#500).
2 parents 2e69bd1 + 941506b commit 1a58cac

19 files changed

+330
-99
lines changed

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ CONTRIBUTING.md
1616
^gitsum$
1717
revdep
1818
^cran-comments\.md$
19+
^tests/testmanual$

R/addins.R

Lines changed: 93 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,55 @@
11
#' Stylers for RStudio Addins
22
#'
33
#' Helper functions for styling via RStudio Addins.
4-
#'
4+
#' @section Addins:
5+
#' - Set style: Select the style transformers to use. For flexibility, the user
6+
#' input is passed to the `transformers` argument, not the `style` argument, so
7+
#' entering `styler::tidyverse_style(scope = "spaces")` in the Addin is
8+
#' equivalent to `styler::style_text("1+1", scope = "spaces")` and
9+
#' `styler::style_text("1+1", transformers = styler::tidyverse_style(scope = "spaces"))`
10+
#' if the text to style is `1+1`. The style transformers are memorized
11+
#' within an R session via the R option `styler.addins_style_transformer` so
12+
#' if you want it to persist over sessions, set the option
13+
#' `styler.addins_style_transformer` in your `.Rprofile`.
14+
#' - Style active file: Styles the active file, by default with
15+
#' [tidyverse_style()] or the value of the option
16+
#' `styler.addins_style_transformer` if specified.
17+
#' - Style selection: Same as *Style active file*, but styles the highlighted
18+
#' code instead of the whole file.
519
#' @section Auto-Save Option:
620
#' By default, both of the RStudio Addins will apply styling to the (selected)
721
#' file contents without saving changes. Automatic saving can be enabled by
822
#' setting the environment variable `save_after_styling` to `TRUE`.
9-
#'
1023
#' Consider setting this in your `.Rprofile` file if you want to persist
1124
#' this setting across multiple sessions. Untitled files will always need to be
1225
#' saved manually after styling.
13-
#'
26+
#' @section Life cycle:
27+
#' The way of specifying the style in the Addin as well as the auto-save option
28+
#' (see below) are experimental. We are currently considering letting the user
29+
#' specify the defaults for other style APIs like [styler::style_text()],
30+
#' either via R options, config files or other ways as well.
31+
#' See [r-lib/styler#319](https://github.com/r-lib/styler/issues/319) for
32+
#' the current status of this.
1433
#' @name styler_addins
1534
#' @family stylers
16-
#' @seealso [Sys.setenv()]
35+
#' @examples
36+
#' \dontrun{
37+
#' # save after styling when using the Addin
38+
#' Sys.setenv(save_after_styling = TRUE)
39+
#' # only style with scope = "spaces" when using the Addin
40+
#' options(
41+
#' styler.addins_style_transformer = "styler::tidyverse_style(scope = 'spaces')"
42+
#' )
43+
#' }
1744
NULL
1845

19-
#' @describeIn styler_addins Styles the active file with [tidyverse_style()] and
20-
#' `strict = TRUE`.
46+
2147
#' @importFrom rlang abort
2248
#' @keywords internal
2349
style_active_file <- function() {
24-
communicate_addins_style()
50+
communicate_addins_style_transformers()
2551
context <- get_rstudio_context()
26-
transformer <- make_transformer(get_addins_style_fun()(),
52+
transformer <- make_transformer(get_addins_style_transformer(),
2753
include_roxygen_examples = TRUE, warn_empty = is_plain_r_file(context$path)
2854
)
2955

@@ -47,96 +73,106 @@ style_active_file <- function() {
4773
rstudioapi::setCursorPosition(context$selection[[1]]$range)
4874
}
4975

50-
#' Style a file as if it was an .R file
51-
#'
52-
#' If not successful, the file is most
53-
#' likely not a .R file, so saving the file and try styling again will work if
54-
#' the file is an .Rmd file. Otherwise, we can throw an error that the file must
55-
#' be a .R or .Rmd file.
56-
#' @param context The context from `styler:::get_rstudio_context()`.
57-
#' @param transformer A transformer function most conveniently constructed with
58-
#' [make_transformer()].
59-
#' @importFrom rlang with_handlers abort
76+
#' Wrapper around [style_pkg()] for access via Addin.
6077
#' @keywords internal
61-
try_transform_as_r_file <- function(context, transformer) {
62-
with_handlers(
63-
transformer(context$contents),
64-
error = function(e) {
65-
preamble_for_unsaved <- paste(
66-
"Styling of unsaved files is only supported for R files with valid code.",
67-
"Please save the file (as .R or .Rmd) and make sure that the R code in it",
68-
"can be parsed. Then, try to style again."
69-
)
70-
71-
if (context$path == "") {
72-
abort(paste0(preamble_for_unsaved, " The error was \n", e$message))
73-
} else {
74-
abort(e$message)
75-
}
76-
}
77-
)
78+
style_active_pkg <- function() {
79+
communicate_addins_style_transformers()
80+
style_pkg(transformers = get_addins_style_transformer())
7881
}
7982

80-
#' @describeIn styler_addins Styles the highlighted selection in a `.R` or
81-
#' `.Rmd` file.
83+
#' Styles the highlighted selection in a `.R` or `.Rmd` file.
8284
#' @importFrom rlang abort
8385
#' @keywords internal
8486
style_selection <- function() {
85-
communicate_addins_style()
87+
communicate_addins_style_transformers()
8688
context <- get_rstudio_context()
8789
text <- context$selection[[1]]$text
8890
if (all(nchar(text) == 0)) abort("No code selected")
89-
out <- style_text(text, style = get_addins_style_fun())
91+
out <- style_text(text, transformers = get_addins_style_transformer())
9092
rstudioapi::modifyRange(
9193
context$selection[[1]]$range, paste0(out, collapse = "\n"),
9294
id = context$id
9395
)
9496
if (Sys.getenv("save_after_styling") == TRUE && context$path != "") {
95-
rstudioapi::documentSave(context$id)
97+
invisible(rstudioapi::documentSave(context$id))
9698
}
9799
}
98100

99101
get_rstudio_context <- function() {
100102
rstudioapi::getActiveDocumentContext()
101103
}
102104

103-
#' Ask the user to supply a style
104-
#'
105+
#' Asks the user to supply a style
105106
#' @importFrom rlang abort
106107
#' @keywords internal
107108
#' @importFrom rlang with_handlers abort
108-
prompt_style <- function() {
109-
current_style <- get_addins_style_name()
109+
set_style_transformers <- function() {
110+
current_style <- get_addins_style_transformer_name()
110111
new_style <-
111112
rstudioapi::showPrompt(
112113
"Select a style",
113-
"Enter the name of a style function, e.g. `styler::tidyverse_style`",
114+
"Enter the name of a style transformer, e.g. `styler::tidyverse_style()`",
114115
current_style
115116
)
116-
parsed_new_style <- with_handlers(
117-
eval(parse(text = new_style)),
117+
parsed_new_style <- with_handlers({
118+
transformers <- eval(parse(text = new_style))
119+
style_text(c("a = 2", "function() {", "NULL", "}"))
120+
},
118121
error = function(e) {
119-
abort(paste0("The selected style \"", new_style, "\" is not valid: ", e$message))
122+
abort(paste0(
123+
"The selected style transformers \"",
124+
new_style, "\" is not valid: ", e$message
125+
))
120126
}
121127
)
122-
options(styler.addins.style = new_style)
128+
options(styler.addins_style_transformer = new_style)
123129
invisible(current_style)
124130
}
125131

126132
#' Return the style function or name
127133
#'
128134
#' @keywords internal
129-
get_addins_style_name <- function() {
130-
getOption("styler.addins.style", default = "styler::tidyverse_style")
135+
get_addins_style_transformer_name <- function() {
136+
getOption("styler.addins_style_transformer", default = "styler::tidyverse_style()")
131137
}
132138

133-
#' @rdname get_addins_style_name
139+
#' @rdname get_addins_style_transformer_name
134140
#' @keywords internal
135-
get_addins_style_fun <- function() {
136-
eval(parse(text = get_addins_style_name()))
141+
get_addins_style_transformer <- function() {
142+
eval(parse(text = get_addins_style_transformer_name()))
143+
}
144+
145+
communicate_addins_style_transformers <- function() {
146+
style_name <- get_addins_style_transformer_name()
147+
cat("Using style transformers `", style_name, "`\n", sep = "")
137148
}
138149

139-
communicate_addins_style <- function() {
140-
style_name <- get_addins_style_name()
141-
cat("Using style `", style_name, "`\n", sep = "")
150+
#' Style a file as if it was an .R file
151+
#'
152+
#' If not successful, the file is most
153+
#' likely not a .R file, so saving the file and try styling again will work if
154+
#' the file is an .Rmd file. Otherwise, we can throw an error that the file must
155+
#' be a .R or .Rmd file.
156+
#' @param context The context from `styler:::get_rstudio_context()`.
157+
#' @param transformer A transformer function most conveniently constructed with
158+
#' [make_transformer()].
159+
#' @importFrom rlang with_handlers abort
160+
#' @keywords internal
161+
try_transform_as_r_file <- function(context, transformer) {
162+
with_handlers(
163+
transformer(context$contents),
164+
error = function(e) {
165+
preamble_for_unsaved <- paste(
166+
"Styling of unsaved files is only supported for R files with valid code.",
167+
"Please save the file (as .R or .Rmd) and make sure that the R code in it",
168+
"can be parsed. Then, try to style again."
169+
)
170+
171+
if (context$path == "") {
172+
abort(paste0(preamble_for_unsaved, " The error was \n", e$message))
173+
} else {
174+
abort(e$message)
175+
}
176+
}
177+
)
142178
}

R/style-guides.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ NULL
2929
#' @inheritParams create_style_guide
3030
#' @param math_token_spacing A list of parameters that define spacing around
3131
#' math token, conveniently constructed using [specify_math_token_spacing()].
32-
3332
#' @details The following options for `scope` are available.
3433
#'
3534
#' * "none": Performs no transformation at all.

inst/rstudio/addins.dcf

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
Name: Set style
2-
Description: Prompt for and set the style used by all STYLER addins
3-
Binding: prompt_style
2+
Description: Prompt for and set the style transformers used by all styler addins
3+
Binding: set_style_transformers
4+
Interactive: true
5+
6+
Name: Style selection
7+
Description: Pretty-print selection
8+
Binding: style_selection
49
Interactive: true
510

611
Name: Style active file
712
Description: Pretty-print active file
813
Binding: style_active_file
914
Interactive: true
1015

11-
Name: Style selection
12-
Description: Pretty-print selection
13-
Binding: style_selection
16+
Name: Style active package
17+
Description: Pretty-print active package
18+
Binding: style_active_pkg
1419
Interactive: true

man/get_addins_style_name.Rd renamed to man/get_addins_style_transformer_name.Rd

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/prompt_style.Rd

Lines changed: 0 additions & 12 deletions
This file was deleted.

man/set_style_transformers.Rd

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/style_active_pkg.Rd

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/style_selection.Rd

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)