Skip to content

Commit

Permalink
first implementation draft for touchstone
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzwalthert committed Jan 10, 2021
1 parent ac57e29 commit a87c7f9
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 6 deletions.
2 changes: 1 addition & 1 deletion API
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ cache_activate(cache_name = NULL, verbose = TRUE)
cache_clear(cache_name = NULL, ask = TRUE)
cache_deactivate(verbose = TRUE)
cache_info(cache_name = NULL, format = "both")
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)
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)
default_style_guide_attributes(pd_flat)
specify_math_token_spacing(zero = "'^'", one = c("'+'", "'-'", "'*'", "'/'"))
specify_reindention(regex_pattern = NULL, indention = 0, comments_only = TRUE)
Expand Down
41 changes: 38 additions & 3 deletions R/style-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,38 @@ tidyverse_style <- function(scope = "tokens",
)
}

subset_transformers <- list(
force_assignment_op = "EQ_ASSIGN",
add_line_break_after_pipe = "SPECIAL-PIPE",
wrap_if_else_while_for_fun_multi_line_in_curly = c("IF", "WHILE", "FOR", "FUNCTION"),
remove_line_breaks_in_fun_dec = "FUNCTION",
set_space_in_curly_curly = c("'{'", "'}'"),
set_space_between_eq_sub_and_comma = "EQ_SUB",
remove_space_around_colons = c("':'", "NS_GET_INT", "NS_GET"),
remove_space_after_fun_dec = "FUNCTION",
remove_space_before_dollar = "'$'",
set_space_after_bang_bang = "'!'",
remove_space_after_excl = "'!'",
style_space_around_tilde = "'~'",
add_space_after_for_if_while = c("IF", "WHILE", "FOR"),
set_line_break_around_curly_curly = "'{'",
indent_braces = c("'('", "'['", "'{'", "')'", "']'", "'}'"),
unindent_fun_dec = "FUNCTION",
indent_eq_sub = c("EQ_SUB", "EQ_FORMALS"), # TODO rename
update_indention_ref_fun_dec = "FUNCTION",
remove_space_before_closing_paren = c("')'", "']'"),
remove_space_before_opening_paren = c("'('", "'['", "LBB"),
remove_space_before_comma = "','",
style_space_around_math_token = c(
math_token_spacing$zero,
math_token_spacing$one
),
remove_space_after_opening_paren = c("'('", "'['", "LBB"),
start_comments_with_space = "COMMENT",
remove_line_break_before_round_closing_after_curly = "'}'",
style_line_break_around_curly = "'{'"
)

style_guide_name <- "styler::tidyverse_style@https://github.com/r-lib"
create_style_guide(
# transformer functions
Expand All @@ -187,7 +219,8 @@ tidyverse_style <- function(scope = "tokens",
reindention = reindention,
style_guide_name = style_guide_name,
style_guide_version = styler_version,
more_specs_style_guide = args
more_specs_style_guide = args,
subset_transformers = subset_transformers
)
}

Expand Down Expand Up @@ -256,7 +289,8 @@ create_style_guide <- function(initialize = default_style_guide_attributes,
reindention = tidyverse_reindention(),
style_guide_name = NULL,
style_guide_version = NULL,
more_specs_style_guide = NULL) {
more_specs_style_guide = NULL,
subset_transformers = NULL) {
lst(
# transformer functions
initialize = lst(initialize),
Expand All @@ -269,7 +303,8 @@ create_style_guide <- function(initialize = default_style_guide_attributes,
reindention,
style_guide_name,
style_guide_version,
more_specs_style_guide
more_specs_style_guide,
subset_transformers
) %>%
map(compact)
}
Expand Down
39 changes: 38 additions & 1 deletion R/transform-files.R
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,18 @@ parse_transform_serialize_r <- function(text,

text <- assert_text(text)
pd_nested <- compute_parse_data_nested(text, transformers, more_specs)
blank_lines_to_next_expr <- find_blank_lines_to_next_block(pd_nested)
if (nrow(pd_nested) == 0) {
if (warn_empty) {
warn("Text to style did not contain any tokens. Returning empty string.")
}
return("")
}
transformers <- transformers_subset(
pd_nested$text[!pd_nested$is_cached],
transformers
)
blank_lines_to_next_expr <- find_blank_lines_to_next_block(pd_nested)


text_out <- pd_nested %>%
split(pd_nested$block) %>%
Expand All @@ -245,6 +250,38 @@ parse_transform_serialize_r <- function(text,
text_out
}

transformers_subset_impl <- function(x, token) {
if (!any(x %in% token)) {
x
}
}

#' Remove transformers that are not needed
#' For every transformer, at least one token must be given to make subsetting.
#' active.
transformers_subset <- function(text, transformers) {
is_colon <- text == ";"
if (any(is_colon)) {
# ; can only be parsed when on the same line as other token, not the case
# here since text is output of compute_parse_data_nested.
text <- c(text[!is_colon], "1;")
}
token <- unique(tokenize(text)$token)
to_remove <- purrr::map(
transformers$subset_transformers,
transformers_subset_impl, token
) %>%
compact() %>% # ise imap, return names directly, save compact()
names()
if (length(to_remove) > 0) {
for (scope in c("initialize", "line_break", "space", "token", "indention")) {
transformers[[scope]][to_remove] <- NULL
transformers[[scope]] <- purrr::compact(transformers[[scope]])
}
}
transformers
}

#' Apply transformers to a parse table
#'
#' The column `multi_line` is updated (after the line break information is
Expand Down
3 changes: 2 additions & 1 deletion man/create_style_guide.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions man/transformers_subset.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a87c7f9

Please sign in to comment.