20
20
# ' `strict = TRUE`.
21
21
# ' @keywords internal
22
22
style_active_file <- function () {
23
+ communicate_addins_style()
23
24
context <- get_rstudio_context()
24
- transformer <- make_transformer(tidyverse_style (),
25
+ transformer <- make_transformer(get_addins_style_fun() (),
25
26
include_roxygen_examples = TRUE , warn_empty = is_plain_r_file(context $ path )
26
27
)
27
28
@@ -78,10 +79,11 @@ try_transform_as_r_file <- function(context, transformer) {
78
79
# ' `.Rmd` file.
79
80
# ' @keywords internal
80
81
style_selection <- function () {
82
+ communicate_addins_style()
81
83
context <- get_rstudio_context()
82
84
text <- context $ selection [[1 ]]$ text
83
85
if (all(nchar(text ) == 0 )) stop(" No code selected" )
84
- out <- style_text(text )
86
+ out <- style_text(text , style = get_addins_style_fun() )
85
87
rstudioapi :: modifyRange(
86
88
context $ selection [[1 ]]$ range , paste0(out , collapse = " \n " ),
87
89
id = context $ id
@@ -94,3 +96,78 @@ style_selection <- function() {
94
96
get_rstudio_context <- function () {
95
97
rstudioapi :: getActiveDocumentContext()
96
98
}
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
+ }
0 commit comments