1
1
# ' Stylers for RStudio Addins
2
2
# '
3
3
# ' 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.
5
19
# ' @section Auto-Save Option:
6
20
# ' By default, both of the RStudio Addins will apply styling to the (selected)
7
21
# ' file contents without saving changes. Automatic saving can be enabled by
8
22
# ' setting the environment variable `save_after_styling` to `TRUE`.
9
- # '
10
23
# ' Consider setting this in your `.Rprofile` file if you want to persist
11
24
# ' this setting across multiple sessions. Untitled files will always need to be
12
25
# ' 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.
14
33
# ' @name styler_addins
15
34
# ' @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
+ # ' }
17
44
NULL
18
45
19
- # ' @describeIn styler_addins Styles the active file with [tidyverse_style()] and
20
- # ' `strict = TRUE`.
46
+
21
47
# ' @importFrom rlang abort
22
48
# ' @keywords internal
23
49
style_active_file <- function () {
24
- communicate_addins_style ()
50
+ communicate_addins_style_transformers ()
25
51
context <- get_rstudio_context()
26
- transformer <- make_transformer(get_addins_style_fun() (),
52
+ transformer <- make_transformer(get_addins_style_transformer (),
27
53
include_roxygen_examples = TRUE , warn_empty = is_plain_r_file(context $ path )
28
54
)
29
55
@@ -47,96 +73,106 @@ style_active_file <- function() {
47
73
rstudioapi :: setCursorPosition(context $ selection [[1 ]]$ range )
48
74
}
49
75
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.
60
77
# ' @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())
78
81
}
79
82
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.
82
84
# ' @importFrom rlang abort
83
85
# ' @keywords internal
84
86
style_selection <- function () {
85
- communicate_addins_style ()
87
+ communicate_addins_style_transformers ()
86
88
context <- get_rstudio_context()
87
89
text <- context $ selection [[1 ]]$ text
88
90
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 ())
90
92
rstudioapi :: modifyRange(
91
93
context $ selection [[1 ]]$ range , paste0(out , collapse = " \n " ),
92
94
id = context $ id
93
95
)
94
96
if (Sys.getenv(" save_after_styling" ) == TRUE && context $ path != " " ) {
95
- rstudioapi :: documentSave(context $ id )
97
+ invisible ( rstudioapi :: documentSave(context $ id ) )
96
98
}
97
99
}
98
100
99
101
get_rstudio_context <- function () {
100
102
rstudioapi :: getActiveDocumentContext()
101
103
}
102
104
103
- # ' Ask the user to supply a style
104
- # '
105
+ # ' Asks the user to supply a style
105
106
# ' @importFrom rlang abort
106
107
# ' @keywords internal
107
108
# ' @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 ()
110
111
new_style <-
111
112
rstudioapi :: showPrompt(
112
113
" 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() `" ,
114
115
current_style
115
116
)
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
+ },
118
121
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
+ ))
120
126
}
121
127
)
122
- options(styler.addins.style = new_style )
128
+ options(styler.addins_style_transformer = new_style )
123
129
invisible (current_style )
124
130
}
125
131
126
132
# ' Return the style function or name
127
133
# '
128
134
# ' @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() " )
131
137
}
132
138
133
- # ' @rdname get_addins_style_name
139
+ # ' @rdname get_addins_style_transformer_name
134
140
# ' @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 = " " )
137
148
}
138
149
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
+ )
142
178
}
0 commit comments