Skip to content

Commit a87c7f9

Browse files
first implementation draft for touchstone
1 parent ac57e29 commit a87c7f9

File tree

5 files changed

+94
-6
lines changed

5 files changed

+94
-6
lines changed

API

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ cache_activate(cache_name = NULL, verbose = TRUE)
66
cache_clear(cache_name = NULL, ask = TRUE)
77
cache_deactivate(verbose = TRUE)
88
cache_info(cache_name = NULL, format = "both")
9-
create_style_guide(initialize = default_style_guide_attributes, line_break = NULL, space = NULL, token = NULL, indention = NULL, use_raw_indention = FALSE, reindention = tidyverse_reindention(), style_guide_name = NULL, style_guide_version = NULL, more_specs_style_guide = NULL)
9+
create_style_guide(initialize = default_style_guide_attributes, line_break = NULL, space = NULL, token = NULL, indention = NULL, use_raw_indention = FALSE, reindention = tidyverse_reindention(), style_guide_name = NULL, style_guide_version = NULL, more_specs_style_guide = NULL, subset_transformers = NULL)
1010
default_style_guide_attributes(pd_flat)
1111
specify_math_token_spacing(zero = "'^'", one = c("'+'", "'-'", "'*'", "'/'"))
1212
specify_reindention(regex_pattern = NULL, indention = 0, comments_only = TRUE)

R/style-guides.R

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,38 @@ tidyverse_style <- function(scope = "tokens",
174174
)
175175
}
176176

177+
subset_transformers <- list(
178+
force_assignment_op = "EQ_ASSIGN",
179+
add_line_break_after_pipe = "SPECIAL-PIPE",
180+
wrap_if_else_while_for_fun_multi_line_in_curly = c("IF", "WHILE", "FOR", "FUNCTION"),
181+
remove_line_breaks_in_fun_dec = "FUNCTION",
182+
set_space_in_curly_curly = c("'{'", "'}'"),
183+
set_space_between_eq_sub_and_comma = "EQ_SUB",
184+
remove_space_around_colons = c("':'", "NS_GET_INT", "NS_GET"),
185+
remove_space_after_fun_dec = "FUNCTION",
186+
remove_space_before_dollar = "'$'",
187+
set_space_after_bang_bang = "'!'",
188+
remove_space_after_excl = "'!'",
189+
style_space_around_tilde = "'~'",
190+
add_space_after_for_if_while = c("IF", "WHILE", "FOR"),
191+
set_line_break_around_curly_curly = "'{'",
192+
indent_braces = c("'('", "'['", "'{'", "')'", "']'", "'}'"),
193+
unindent_fun_dec = "FUNCTION",
194+
indent_eq_sub = c("EQ_SUB", "EQ_FORMALS"), # TODO rename
195+
update_indention_ref_fun_dec = "FUNCTION",
196+
remove_space_before_closing_paren = c("')'", "']'"),
197+
remove_space_before_opening_paren = c("'('", "'['", "LBB"),
198+
remove_space_before_comma = "','",
199+
style_space_around_math_token = c(
200+
math_token_spacing$zero,
201+
math_token_spacing$one
202+
),
203+
remove_space_after_opening_paren = c("'('", "'['", "LBB"),
204+
start_comments_with_space = "COMMENT",
205+
remove_line_break_before_round_closing_after_curly = "'}'",
206+
style_line_break_around_curly = "'{'"
207+
)
208+
177209
style_guide_name <- "styler::tidyverse_style@https://github.com/r-lib"
178210
create_style_guide(
179211
# transformer functions
@@ -187,7 +219,8 @@ tidyverse_style <- function(scope = "tokens",
187219
reindention = reindention,
188220
style_guide_name = style_guide_name,
189221
style_guide_version = styler_version,
190-
more_specs_style_guide = args
222+
more_specs_style_guide = args,
223+
subset_transformers = subset_transformers
191224
)
192225
}
193226

@@ -256,7 +289,8 @@ create_style_guide <- function(initialize = default_style_guide_attributes,
256289
reindention = tidyverse_reindention(),
257290
style_guide_name = NULL,
258291
style_guide_version = NULL,
259-
more_specs_style_guide = NULL) {
292+
more_specs_style_guide = NULL,
293+
subset_transformers = NULL) {
260294
lst(
261295
# transformer functions
262296
initialize = lst(initialize),
@@ -269,7 +303,8 @@ create_style_guide <- function(initialize = default_style_guide_attributes,
269303
reindention,
270304
style_guide_name,
271305
style_guide_version,
272-
more_specs_style_guide
306+
more_specs_style_guide,
307+
subset_transformers
273308
) %>%
274309
map(compact)
275310
}

R/transform-files.R

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,18 @@ parse_transform_serialize_r <- function(text,
217217

218218
text <- assert_text(text)
219219
pd_nested <- compute_parse_data_nested(text, transformers, more_specs)
220-
blank_lines_to_next_expr <- find_blank_lines_to_next_block(pd_nested)
221220
if (nrow(pd_nested) == 0) {
222221
if (warn_empty) {
223222
warn("Text to style did not contain any tokens. Returning empty string.")
224223
}
225224
return("")
226225
}
226+
transformers <- transformers_subset(
227+
pd_nested$text[!pd_nested$is_cached],
228+
transformers
229+
)
230+
blank_lines_to_next_expr <- find_blank_lines_to_next_block(pd_nested)
231+
227232

228233
text_out <- pd_nested %>%
229234
split(pd_nested$block) %>%
@@ -245,6 +250,38 @@ parse_transform_serialize_r <- function(text,
245250
text_out
246251
}
247252

253+
transformers_subset_impl <- function(x, token) {
254+
if (!any(x %in% token)) {
255+
x
256+
}
257+
}
258+
259+
#' Remove transformers that are not needed
260+
#' For every transformer, at least one token must be given to make subsetting.
261+
#' active.
262+
transformers_subset <- function(text, transformers) {
263+
is_colon <- text == ";"
264+
if (any(is_colon)) {
265+
# ; can only be parsed when on the same line as other token, not the case
266+
# here since text is output of compute_parse_data_nested.
267+
text <- c(text[!is_colon], "1;")
268+
}
269+
token <- unique(tokenize(text)$token)
270+
to_remove <- purrr::map(
271+
transformers$subset_transformers,
272+
transformers_subset_impl, token
273+
) %>%
274+
compact() %>% # ise imap, return names directly, save compact()
275+
names()
276+
if (length(to_remove) > 0) {
277+
for (scope in c("initialize", "line_break", "space", "token", "indention")) {
278+
transformers[[scope]][to_remove] <- NULL
279+
transformers[[scope]] <- purrr::compact(transformers[[scope]])
280+
}
281+
}
282+
transformers
283+
}
284+
248285
#' Apply transformers to a parse table
249286
#'
250287
#' The column `multi_line` is updated (after the line break information is

man/create_style_guide.Rd

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

man/transformers_subset.Rd

Lines changed: 15 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)