Skip to content

Commit 3c779b2

Browse files
Merge pull request #1043 from r-lib/export_third_party_helpers
Expose internals used with other style guides
2 parents 68436ea + 24684f6 commit 3c779b2

25 files changed

+284
-111
lines changed

API

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,22 @@ cache_activate(cache_name = NULL, verbose = !getOption("styler.quiet", FALSE))
66
cache_clear(cache_name = NULL, ask = TRUE)
77
cache_deactivate(verbose = !getOption("styler.quiet", FALSE))
88
cache_info(cache_name = NULL, format = "both")
9+
compute_parse_data_nested(text, transformers = tidyverse_style(), more_specs = NULL)
910
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, transformers_drop = specify_transformers_drop(), indent_character = " ")
1011
default_style_guide_attributes(pd_flat)
12+
is_asymmetric_tilde_expr(pd)
13+
is_comment(pd)
14+
is_conditional_expr(pd)
15+
is_curly_expr(pd)
16+
is_for_expr(pd)
17+
is_function_call(pd)
18+
is_function_declaration(pd)
19+
is_symmetric_tilde_expr(pd)
20+
is_tilde_expr(pd, tilde_pos = c(1L, 2L))
21+
is_while_expr(pd)
22+
next_non_comment(pd, pos)
23+
previous_non_comment(pd, pos)
24+
scope_normalize(scope, name = substitute(scope))
1125
specify_math_token_spacing(zero = "'^'", one = c("'+'", "'-'", "'*'", "'/'"))
1226
specify_reindention(regex_pattern = NULL, indention = 0L, comments_only = TRUE)
1327
specify_transformers_drop(spaces = NULL, indention = NULL, line_breaks = NULL, tokens = NULL)
@@ -17,7 +31,7 @@ style_pkg(pkg = ".", ..., style = tidyverse_style, transformers = style(...), fi
1731
style_text(text, ..., style = tidyverse_style, transformers = style(...), include_roxygen_examples = TRUE, base_indention = 0L)
1832
tidyverse_math_token_spacing()
1933
tidyverse_reindention()
20-
tidyverse_style(scope = "tokens", strict = TRUE, indent_by = 2, start_comments_with_one_space = FALSE, reindention = tidyverse_reindention(), math_token_spacing = tidyverse_math_token_spacing())
34+
tidyverse_style(scope = "tokens", strict = TRUE, indent_by = 2L, start_comments_with_one_space = FALSE, reindention = tidyverse_reindention(), math_token_spacing = tidyverse_math_token_spacing())
2135

2236
## Foreign S3 methods
2337

NAMESPACE

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,22 @@ export(cache_activate)
55
export(cache_clear)
66
export(cache_deactivate)
77
export(cache_info)
8+
export(compute_parse_data_nested)
89
export(create_style_guide)
910
export(default_style_guide_attributes)
11+
export(is_asymmetric_tilde_expr)
12+
export(is_comment)
13+
export(is_conditional_expr)
14+
export(is_curly_expr)
15+
export(is_for_expr)
16+
export(is_function_call)
17+
export(is_function_declaration)
18+
export(is_symmetric_tilde_expr)
19+
export(is_tilde_expr)
20+
export(is_while_expr)
21+
export(next_non_comment)
22+
export(previous_non_comment)
23+
export(scope_normalize)
1024
export(specify_math_token_spacing)
1125
export(specify_reindention)
1226
export(specify_transformers_drop)

R/detect-alignment.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#' list(styler.cache_name = NULL), # temporarily deactivate cache
3232
#' {
3333
#' transformers <- tidyverse_style()
34-
#' pd_nested <- styler:::compute_parse_data_nested(c(
34+
#' pd_nested <- compute_parse_data_nested(c(
3535
#' "call(",
3636
#' " ab = 1L,",
3737
#' " a = 2",

R/expr-is.R

Lines changed: 74 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,72 @@
1-
#' Check whether a parse table corresponds to a certain expression
1+
#' What is a parse table representing?
22
#'
3-
#' @param pd A parse table.
3+
#' Check whether a parse table corresponds to a certain expression.
44
#' @name pd_is
5+
#'
6+
#' @param pd A parse table.
7+
#' @param tilde_pos Integer vector indicating row-indices that should be
8+
#' checked for tilde. See 'Details'.
9+
#'
10+
#' @family third-party style guide helpers
511
#' @keywords internal
612
NULL
713

8-
#' @describeIn pd_is Checks whether `pd` contains an expression wrapped in
9-
#' curly brackets.
10-
#' @keywords internal
14+
#' @describeIn pd_is Checks whether `pd` contains an expression wrapped in curly brackets.
15+
#' @examples
16+
#' code <- "if (TRUE) { 1 }"
17+
#' pd <- compute_parse_data_nested(code)
18+
#' is_curly_expr(pd)
19+
#' child_of_child <- pd$child[[1]]$child[[5]]
20+
#' is_curly_expr(child_of_child)
21+
#'
22+
#' @export
1123
is_curly_expr <- function(pd) {
1224
if (is.null(pd)) {
1325
return(FALSE)
1426
}
1527
pd$token[1L] == "'{'"
1628
}
1729

30+
#' @describeIn pd_is Checks whether `pd` contains a `for` loop.
31+
#' @examples
32+
#' code <- "for (i in 1:5) print(1:i)"
33+
#' pd <- compute_parse_data_nested(code)
34+
#' is_for_expr(pd)
35+
#' is_for_expr(pd$child[[1]])
36+
#'
37+
#' @export
1838
is_for_expr <- function(pd) {
1939
pd$token[1L] == "FOR"
2040
}
2141

2242
#' @describeIn pd_is Checks whether `pd` contains is a conditional expression.
23-
#' @keywords internal
24-
is_cond_expr <- function(pd) {
43+
#' @examples
44+
#' code <- "if (TRUE) x <- 1 else x <- 0"
45+
#' pd <- compute_parse_data_nested(code)
46+
#' is_conditional_expr(pd)
47+
#' is_conditional_expr(pd$child[[1]])
48+
#'
49+
#' @export
50+
is_conditional_expr <- function(pd) {
2551
pd$token[1L] == "IF"
2652
}
2753

28-
#' @describeIn pd_is Checks whether `pd` contains is a while loop.
29-
#' @keywords internal
54+
#' @describeIn pd_is Checks whether `pd` contains a `while` loop.
55+
#' @export
3056
is_while_expr <- function(pd) {
3157
pd$token[1L] == "WHILE"
3258
}
3359

60+
3461
#' @describeIn pd_is Checks whether `pd` is a function call.
35-
#' @keywords internal
62+
#' @examples
63+
#' code <- "x <- list(1:3)"
64+
#' pd <- compute_parse_data_nested(code)
65+
#' is_function_call(pd)
66+
#' child_of_child <- pd$child[[1]]$child[[3]]
67+
#' is_function_call(child_of_child)
68+
#'
69+
#' @export
3670
is_function_call <- function(pd) {
3771
if (is.null(pd)) {
3872
return(FALSE)
@@ -44,56 +78,69 @@ is_function_call <- function(pd) {
4478
}
4579

4680
#' @describeIn pd_is Checks whether `pd` is a function declaration.
47-
#' @keywords internal
48-
is_function_dec <- function(pd) {
81+
#' @examples
82+
#' code <- "foo <- function() NULL"
83+
#' pd <- compute_parse_data_nested(code)
84+
#' is_function_declaration(pd)
85+
#' child_of_child <- pd$child[[1]]$child[[3]]
86+
#' is_function_declaration(child_of_child)
87+
#'
88+
#' @export
89+
is_function_declaration <- function(pd) {
4990
if (is.null(pd)) {
5091
return(FALSE)
5192
}
5293
pd$token[1L] == "FUNCTION"
5394
}
5495

5596
#' @describeIn pd_is Checks for every token whether or not it is a comment.
56-
#' @keywords internal
97+
#' @examples
98+
#' code <- "x <- 1 # TODO: check value"
99+
#' pd <- compute_parse_data_nested(code)
100+
#' is_comment(pd)
101+
#'
102+
#' @export
57103
is_comment <- function(pd) {
58104
if (is.null(pd)) {
59105
return(FALSE)
60106
}
61107
pd$token == "COMMENT"
62108
}
63109

64-
65-
66-
#' Check whether a parse table contains a tilde
67-
#'
68-
#'
69-
#' @param pd A parse table.
70-
#' @param tilde_pos Integer vector indicating row-indices that should be
71-
#' checked for tilde. See 'Details'.
72-
#'
110+
#' @describeIn pd_is Checks whether `pd` contains a tilde.
73111
#' @details
74112
#' A tilde is on the top row in the parse table if it is an asymmetric tilde
75113
#' expression (like `~column`), in the second row if it is a symmetric tilde
76114
#' expression (like `a~b`).
77-
#' @keywords internal
115+
#' @examples
116+
#' code <- "lm(wt ~ mpg, mtcars)"
117+
#' pd <- compute_parse_data_nested(code)
118+
#' is_tilde_expr(pd$child[[1]]$child[[3]])
119+
#' is_symmetric_tilde_expr(pd$child[[1]]$child[[3]])
120+
#' is_asymmetric_tilde_expr(pd$child[[1]]$child[[3]])
121+
#'
122+
#' @export
78123
is_tilde_expr <- function(pd, tilde_pos = c(1L, 2L)) {
79124
if (is.null(pd) || nrow(pd) == 1L) {
80125
return(FALSE)
81126
}
82127
any(pd$token[tilde_pos] == "'~'")
83128
}
84129

85-
#' @rdname is_tilde_expr
130+
#' @describeIn pd_is If `pd` contains a tilde, checks whether it is asymmetrical.
131+
#' @export
86132
is_asymmetric_tilde_expr <- function(pd) {
87133
is_tilde_expr(pd, tilde_pos = 1L)
88134
}
89135

90-
#' @rdname is_tilde_expr
136+
#' @describeIn pd_is If `pd` contains a tilde, checks whether it is symmetrical.
137+
#' @export
91138
is_symmetric_tilde_expr <- function(pd) {
92139
is_tilde_expr(pd, tilde_pos = 2L)
93140
}
94141

95142
is_subset_expr <- function(pd) {
96-
if (is.null(pd) || nrow(pd) == 1) {
143+
if (is.null(pd) || nrow(pd) == 1L) {
97144
return(FALSE)
98145
}
99146
pd$token[2L] %in% subset_token_opening
@@ -152,7 +199,7 @@ contains_else_expr_that_needs_braces <- function(pd) {
152199
non_comment_after_else <- next_non_comment(pd, else_idx)
153200
sub_expr <- pd$child[[non_comment_after_else]]
154201
# needs braces if NOT if_condition, NOT curly expr
155-
!is_cond_expr(sub_expr) && !is_curly_expr(sub_expr)
202+
!is_conditional_expr(sub_expr) && !is_curly_expr(sub_expr)
156203
} else {
157204
FALSE
158205
}

R/initialize.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#' list(styler.cache_name = NULL), # temporarily deactivate cache
1010
#' {
1111
#' string_to_format <- "call( 3)"
12-
#' pd <- styler:::compute_parse_data_nested(string_to_format)
12+
#' pd <- compute_parse_data_nested(string_to_format)
1313
#' styler:::pre_visit_one(pd, default_style_guide_attributes)
1414
#' }
1515
#' )

R/nest.R

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@
66
#' @return A nested parse table. See [tokenize()] for details on the columns
77
#' of the parse table.
88
#' @importFrom purrr when
9-
#' @keywords internal
9+
#' @examples
10+
#' code <- "
11+
#' ab <- 1L # some comment
12+
#' abcdef <- 2L
13+
#' "
14+
#' writeLines(code)
15+
#' compute_parse_data_nested(code)
16+
#' @export
1017
compute_parse_data_nested <- function(text,
11-
transformers,
12-
more_specs) {
18+
transformers = tidyverse_style(),
19+
more_specs = NULL) {
1320
parse_data <- text_to_flat_pd(text, transformers, more_specs = more_specs)
1421
env_add_stylerignore(parse_data)
1522
parse_data$child <- rep(list(NULL), length(parse_data$text))

R/nested-to-tree.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ create_tree_from_pd_with_default_style_attributes <- function(pd,
3636
#' list(styler.cache_name = NULL), # temporarily deactivate cache
3737
#' {
3838
#' code <- "a <- function(x) { if(x > 1) { 1+1 } else {x} }"
39-
#' nested_pd <- styler:::compute_parse_data_nested(code)
39+
#' nested_pd <- compute_parse_data_nested(code)
4040
#' initialized <- styler:::pre_visit_one(
4141
#' nested_pd, default_style_guide_attributes
4242
#' )

R/rules-indention.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ indent_braces <- function(pd, indent_by) {
1818
#' @seealso set_unindention_child update_indention_ref_fun_dec
1919
#' @keywords internal
2020
unindent_fun_dec <- function(pd) {
21-
if (is_function_dec(pd)) {
21+
if (is_function_declaration(pd)) {
2222
idx_closing_brace <- which(pd$token %in% "')'")
2323
fun_dec_head <- seq2(2L, idx_closing_brace)
2424
pd$indent[fun_dec_head] <- 0L

R/rules-line-breaks.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ set_line_break_before_curly_opening <- function(pd) {
9898
should_not_be_on_same_line_idx <- line_break_to_set_idx[
9999
should_not_be_on_same_line
100100
]
101-
if (is_function_dec(pd)) {
101+
if (is_function_declaration(pd)) {
102102
should_not_be_on_same_line_idx <- setdiff(
103103
1L + should_not_be_on_same_line_idx, nrow(pd)
104104
)
@@ -228,7 +228,7 @@ remove_line_break_before_round_closing_after_curly <- function(pd) {
228228
}
229229

230230
remove_line_breaks_in_fun_dec <- function(pd) {
231-
if (is_function_dec(pd)) {
231+
if (is_function_declaration(pd)) {
232232
round_after <- (
233233
pd$token == "')'" | pd$token_before == "'('"
234234
) &

R/rules-spaces.R

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ set_space_around_op <- function(pd_flat, strict) {
2222
!getOption("styler.ignore_alignment", FALSE) &&
2323
(
2424
(is_function_call(pd_flat) && sum_lag_newlines > 2L) ||
25-
(is_function_dec(pd_flat) && sum_lag_newlines > 1L)
25+
(is_function_declaration(pd_flat) && sum_lag_newlines > 1L)
2626
) &&
2727
any(pd_flat$token %in% c("EQ_SUB", "','", "EQ_FORMALS"))
2828
) {
@@ -96,10 +96,8 @@ style_space_around_token <- function(pd_flat,
9696
pd_flat$spaces[idx_before] <- level_before
9797
pd_flat$spaces[idx_after] <- level_after
9898
} else {
99-
pd_flat$spaces[idx_before] <-
100-
pmax(pd_flat$spaces[idx_before], level_before)
101-
pd_flat$spaces[idx_after] <-
102-
pmax(pd_flat$spaces[idx_after], level_after)
99+
pd_flat$spaces[idx_before] <- pmax(pd_flat$spaces[idx_before], level_before)
100+
pd_flat$spaces[idx_after] <- pmax(pd_flat$spaces[idx_after], level_after)
103101
}
104102
pd_flat
105103
}
@@ -110,12 +108,15 @@ style_space_around_tilde <- function(pd_flat, strict) {
110108
strict, "'~'",
111109
level_before = 1L, level_after = 1L
112110
)
113-
} else if (is_asymmetric_tilde_expr(pd_flat)) {
111+
}
112+
113+
if (is_asymmetric_tilde_expr(pd_flat)) {
114114
pd_flat <- style_space_around_token(pd_flat,
115115
strict = TRUE, "'~'", level_before = 1L,
116116
level_after = as.integer(nrow(pd_flat$child[[2L]]) > 1L)
117117
)
118118
}
119+
119120
pd_flat
120121
}
121122

@@ -286,7 +287,7 @@ start_comments_with_space <- function(pd, force_one = FALSE) {
286287
comments$text
287288
) %>%
288289
trimws("right")
289-
pd$short[is_comment] <- substr(pd$text[is_comment], 1, 5)
290+
pd$short[is_comment] <- substr(pd$text[is_comment], 1L, 5L)
290291
pd
291292
}
292293

@@ -345,20 +346,16 @@ remove_space_after_fun_dec <- function(pd_flat) {
345346
}
346347

347348
remove_space_around_colons <- function(pd_flat) {
348-
one_two_or_three_col_after <-
349-
pd_flat$token %in% c("':'", "NS_GET_INT", "NS_GET")
350-
351-
one_two_or_three_col_before <-
352-
lead(one_two_or_three_col_after, default = FALSE)
349+
one_two_or_three_col_after <- pd_flat$token %in% c("':'", "NS_GET_INT", "NS_GET")
350+
one_two_or_three_col_before <- lead(one_two_or_three_col_after, default = FALSE)
353351

354-
col_around <-
355-
one_two_or_three_col_before | one_two_or_three_col_after
352+
col_around <- one_two_or_three_col_before | one_two_or_three_col_after
356353

357354
pd_flat$spaces[col_around & (pd_flat$newlines == 0L)] <- 0L
358355
pd_flat
359356
}
360357

361-
#' Set space between EQ_SUB and "','"
358+
#' Set space between `EQ_SUB` and `"','"`
362359
#' @param pd A parse table.
363360
#' @keywords internal
364361
set_space_between_eq_sub_and_comma <- function(pd) {

0 commit comments

Comments
 (0)