Skip to content

Commit 446622c

Browse files
implement global indention
1 parent 21d95ef commit 446622c

33 files changed

+291
-70
lines changed

API

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ create_style_guide(initialize = default_style_guide_attributes, line_break = NUL
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)
13-
style_dir(path = ".", ..., style = tidyverse_style, transformers = style(...), filetype = c("R", "Rprofile"), recursive = TRUE, exclude_files = NULL, exclude_dirs = c("packrat", "renv"), include_roxygen_examples = TRUE, dry = "off")
14-
style_file(path, ..., style = tidyverse_style, transformers = style(...), include_roxygen_examples = TRUE, dry = "off")
15-
style_pkg(pkg = ".", ..., style = tidyverse_style, transformers = style(...), filetype = c("R", "Rprofile"), exclude_files = "R/RcppExports.R", exclude_dirs = c("packrat", "renv"), include_roxygen_examples = TRUE, dry = "off")
16-
style_text(text, ..., style = tidyverse_style, transformers = style(...), include_roxygen_examples = TRUE)
13+
style_dir(path = ".", ..., style = tidyverse_style, transformers = style(...), filetype = c("R", "Rprofile"), recursive = TRUE, exclude_files = NULL, exclude_dirs = c("packrat", "renv"), include_roxygen_examples = TRUE, base_indention = 0, dry = "off")
14+
style_file(path, ..., style = tidyverse_style, transformers = style(...), include_roxygen_examples = TRUE, base_indention = 0, dry = "off")
15+
style_pkg(pkg = ".", ..., style = tidyverse_style, transformers = style(...), filetype = c("R", "Rprofile"), exclude_files = "R/RcppExports.R", exclude_dirs = c("packrat", "renv"), include_roxygen_examples = TRUE, base_indention = 0, dry = "off")
16+
style_text(text, ..., style = tidyverse_style, transformers = style(...), include_roxygen_examples = TRUE, base_indention = 0)
1717
tidyverse_math_token_spacing()
1818
tidyverse_reindention()
1919
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())

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Encoding: UTF-8
4545
LazyData: true
4646
Roxygen: list(markdown = TRUE, roclets = c("rd", "namespace",
4747
"collate", "pkgapi::api_roclet"))
48-
RoxygenNote: 7.1.0
48+
RoxygenNote: 7.1.1
4949
Collate:
5050
'addins.R'
5151
'communicate.R'

R/addins.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ style_active_file <- function() {
5050
communicate_addins_style_transformers()
5151
context <- get_rstudio_context()
5252
transformer <- make_transformer(get_addins_style_transformer(),
53-
include_roxygen_examples = TRUE, warn_empty = is_plain_r_file(context$path)
53+
include_roxygen_examples = TRUE,
54+
base_indention = 0,
55+
warn_empty = is_plain_r_file(context$path)
5456
)
5557
is_r_file <- any(
5658
is_plain_r_file(context$path),

R/roxygen-examples.R

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
#' @inheritSection parse_transform_serialize_roxygen Hierarchy
99
#' @importFrom purrr map flatten_chr
1010
#' @keywords internal
11-
style_roxygen_code_example <- function(example, transformers) {
11+
style_roxygen_code_example <- function(example, transformers, base_indention) {
1212
bare <- parse_roxygen(example)
1313
one_dont <- split(bare, factor(cumsum(bare %in% dont_keywords())))
14-
map(one_dont, style_roxygen_code_example_segment, transformers) %>%
14+
map(one_dont, style_roxygen_code_example_segment,
15+
transformers = transformers,
16+
base_indention = base_indention
17+
) %>%
1518
flatten_chr() %>%
1619
add_roxygen_mask()
1720
}
@@ -35,7 +38,9 @@ style_roxygen_code_example <- function(example, transformers) {
3538
#' @importFrom rlang seq2
3639
#' @importFrom purrr map2 flatten_chr
3740
#' @keywords internal
38-
style_roxygen_code_example_segment <- function(one_dont, transformers) {
41+
style_roxygen_code_example_segment <- function(one_dont,
42+
transformers,
43+
base_indention) {
3944
if (length(one_dont) < 1L) {
4045
return(character())
4146
}
@@ -46,7 +51,8 @@ style_roxygen_code_example_segment <- function(one_dont, transformers) {
4651

4752
map2(split_segments$separated, is_dont,
4853
style_roxygen_example_snippet,
49-
transformers = transformers
54+
transformers = transformers,
55+
base_indention = base_indention
5056
) %>%
5157
flatten_chr()
5258
}
@@ -61,7 +67,8 @@ style_roxygen_code_example_segment <- function(one_dont, transformers) {
6167
#' @keywords internal
6268
style_roxygen_example_snippet <- function(code_snippet,
6369
transformers,
64-
is_dont) {
70+
is_dont,
71+
base_indention) {
6572
if (is_dont) {
6673
decomposed <- remove_dont_mask(code_snippet)
6774
code_snippet <- decomposed$code
@@ -73,7 +80,9 @@ style_roxygen_example_snippet <- function(code_snippet,
7380
is_cached <- is_cached(code_snippet, transformers)
7481
if (!is_cached || !cache_is_active) {
7582
code_snippet <- code_snippet %>%
76-
parse_transform_serialize_r(transformers, warn_empty = FALSE)
83+
parse_transform_serialize_r(transformers,
84+
base_indention = base_indention, warn_empty = FALSE
85+
)
7786
} else {
7887
code_snippet <- ensure_last_n_empty(code_snippet, n = 0)
7988
}

R/testing.R

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ transform_and_check <- function(in_item, out_item,
113113
read_in <- xfun::read_utf8(in_item)
114114
if (write_tree) {
115115
create_tree(read_in) %>%
116-
write.table(out_tree, col.names = FALSE, row.names = FALSE, quote = FALSE,
116+
write.table(out_tree,
117+
col.names = FALSE, row.names = FALSE, quote = FALSE,
117118
fileEncoding = "UTF-8"
118119
)
119120
}
@@ -160,7 +161,7 @@ NULL
160161
#' transformations but remove EOL spaces and indention due to the way the
161162
#' serialization is set up.
162163
#' @keywords internal
163-
style_empty <- function(text) {
164+
style_empty <- function(text, base_indention = 0) {
164165
transformers <- list(
165166
# transformer functions
166167
initialize = default_style_guide_attributes,
@@ -172,13 +173,16 @@ style_empty <- function(text) {
172173
reindention = specify_reindention(),
173174
NULL
174175
)
175-
transformed_text <- parse_transform_serialize_r(text, transformers)
176+
transformed_text <- parse_transform_serialize_r(text,
177+
transformers = transformers,
178+
base_indention = base_indention
179+
)
176180
transformed_text
177181
}
178182

179183
#' @describeIn test_transformer Transformations for indention based on operators
180184
#' @keywords internal
181-
style_op <- function(text) {
185+
style_op <- function(text, base_indention = 0) {
182186
transformers <- list(
183187
# transformer functions
184188
initialize = default_style_guide_attributes,
@@ -191,7 +195,10 @@ style_op <- function(text) {
191195
NULL
192196
)
193197

194-
transformed_text <- parse_transform_serialize_r(text, transformers)
198+
transformed_text <- parse_transform_serialize_r(text,
199+
transformers = transformers,
200+
base_indention = base_indention
201+
)
195202
transformed_text
196203
}
197204

@@ -246,16 +253,16 @@ n_times_faster_with_cache <- function(x1, x2 = x1, ...,
246253
n = 3,
247254
clear = "always") {
248255
rlang::arg_match(clear, c("always", "final", "never", "all but last"))
249-
capture.output(
250-
out <- purrr::map(1:n, n_times_faster_bench,
251-
x1 = x1, x2 = x2, fun = fun,
252-
..., n = n, clear = clear
256+
257+
out <- purrr::map(1:n, n_times_faster_bench,
258+
x1 = x1, x2 = x2, fun = fun,
259+
..., n = n, clear = clear
260+
) %>%
261+
purrr::map_dbl(
262+
~ unname(.x$first["elapsed"] / .x$second["elapsed"])
253263
) %>%
254-
purrr::map_dbl(
255-
~ unname(.x$first["elapsed"] / .x$second["elapsed"])
256-
) %>%
257-
mean()
258-
)
264+
mean()
265+
259266
if (clear %in% c("always", "final")) {
260267
clear_testthat_cache()
261268
}

R/transform-block.R

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,26 @@
44
#' for details on how a top level nest is split into blocks.
55
#' @param pd_nested A block of the nested parse table.
66
#' @param start_line The line number on which the code starts.
7+
#' @param base_indention Integer scalar indicating by how many spaces the whole
8+
#' output text should be indented. Note that this is not the same as splitting
9+
#' by line and add a `base_indention` spaces before the code in the case
10+
#' multi-line strings are present. See 'Examples'.
711
#' @inheritParams apply_transformers
12+
#' @examples
13+
#' text_in <- 'x<- function()
14+
#' "here
15+
#' is"
16+
#' NULL
17+
#' 1+ 1
18+
#' '
19+
#' style_text(text_in, base_indention = 3)
20+
#' # not equal to the naive approach
21+
#' styler:::construct_vertical(paste0(styler:::add_spaces(3), style_text(text_in), sep = ""))
822
#' @keywords internal
923
parse_transform_serialize_r_block <- function(pd_nested,
1024
start_line,
11-
transformers) {
25+
transformers,
26+
base_indention) {
1227
if (!all(pd_nested$is_cached, na.rm = TRUE) || !cache_is_activated()) {
1328
transformed_pd <- apply_transformers(pd_nested, transformers)
1429
flattened_pd <- post_visit(transformed_pd, list(extract_terminals)) %>%
@@ -20,8 +35,8 @@ parse_transform_serialize_r_block <- function(pd_nested,
2035
comments_only = transformers$reindention$comments_only
2136
)
2237
is_on_newline <- flattened_pd$lag_newlines > 0
23-
is_on_newline[1] <- 1
24-
flattened_pd$lag_spaces[is_on_newline] <- flattened_pd$lag_spaces[is_on_newline] + 2
38+
is_on_newline[1] <- TRUE
39+
flattened_pd$lag_spaces[is_on_newline] <- flattened_pd$lag_spaces[is_on_newline] + base_indention
2540
serialized_transformed_text <- serialize_parse_data_flattened(flattened_pd)
2641
} else {
2742
serialized_transformed_text <- map2(

R/transform-files.R

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
#' styling whether or not it was actually changed (or would be changed when
1212
#' `dry` is not "off").
1313
#' @keywords internal
14-
transform_files <- function(files, transformers, include_roxygen_examples, dry) {
15-
transformer <- make_transformer(transformers, include_roxygen_examples)
14+
transform_files <- function(files, transformers, include_roxygen_examples, base_indention, dry) {
15+
transformer <- make_transformer(transformers, include_roxygen_examples, base_indention)
1616
max_char <- min(max(nchar(files), 0), getOption("width"))
1717
len_files <- length(files)
1818
if (len_files > 0L && !getOption("styler.quiet", FALSE)) {
@@ -52,12 +52,12 @@ transform_file <- function(path,
5252
n_spaces_before_message_after <-
5353
max_char_after_message_path - char_after_path
5454
if (!getOption("styler.quiet", FALSE)) {
55-
cat(
56-
message_before, path,
57-
rep_char(" ", max(0L, n_spaces_before_message_after)),
58-
append = FALSE
59-
)
60-
}
55+
cat(
56+
message_before, path,
57+
rep_char(" ", max(0L, n_spaces_before_message_after)),
58+
append = FALSE
59+
)
60+
}
6161
changed <- transform_code(path, fun = fun, ..., dry = dry)
6262

6363
bullet <- ifelse(is.na(changed), "warning", ifelse(changed, "info", "tick"))
@@ -82,6 +82,7 @@ transform_file <- function(path,
8282
#' @importFrom purrr when
8383
make_transformer <- function(transformers,
8484
include_roxygen_examples,
85+
base_indention,
8586
warn_empty = TRUE) {
8687
force(transformers)
8788
assert_transformers(transformers)
@@ -98,10 +99,15 @@ make_transformer <- function(transformers,
9899

99100
if (!use_cache) {
100101
transformed_code <- text %>%
101-
parse_transform_serialize_r(transformers, warn_empty = warn_empty) %>%
102+
parse_transform_serialize_r(transformers,
103+
base_indention = base_indention,
104+
warn_empty = warn_empty
105+
) %>%
102106
when(
103107
include_roxygen_examples ~
104-
parse_transform_serialize_roxygen(., transformers),
108+
parse_transform_serialize_roxygen(.,
109+
transformers = transformers, base_indention = base_indention
110+
),
105111
~.
106112
)
107113
if (should_use_cache) {
@@ -140,15 +146,16 @@ make_transformer <- function(transformers,
140146
#' [parse_transform_serialize_r()].
141147
#' @importFrom purrr map_at flatten_chr
142148
#' @keywords internal
143-
parse_transform_serialize_roxygen <- function(text, transformers) {
149+
parse_transform_serialize_roxygen <- function(text, transformers, base_indention) {
144150
roxygen_seqs <- identify_start_to_stop_of_roxygen_examples_from_text(text)
145151
if (length(roxygen_seqs) < 1L) {
146152
return(text)
147153
}
148154
split_segments <- split_roxygen_segments(text, unlist(roxygen_seqs))
149155
map_at(split_segments$separated, split_segments$selectors,
150156
style_roxygen_code_example,
151-
transformers = transformers
157+
transformers = transformers,
158+
base_indention = base_indention
152159
) %>%
153160
flatten_chr()
154161
}
@@ -187,12 +194,13 @@ split_roxygen_segments <- function(text, roxygen_examples) {
187194
#' @param warn_empty Whether or not a warning should be displayed when `text`
188195
#' does not contain any tokens.
189196
#' @inheritParams compute_parse_data_nested
190-
#' @inheritParams apply_transformers
197+
#' @inheritParams parse_transform_serialize_r_block
191198
#' @seealso [parse_transform_serialize_roxygen()]
192199
#' @importFrom rlang abort
193200
#' @keywords internal
194201
parse_transform_serialize_r <- function(text,
195202
transformers,
203+
base_indention,
196204
warn_empty = TRUE) {
197205
text <- assert_text(text)
198206
pd_nested <- compute_parse_data_nested(text, transformers)
@@ -210,7 +218,8 @@ parse_transform_serialize_r <- function(text,
210218
unname() %>%
211219
map2(blank_lines_to_next_expr,
212220
parse_transform_serialize_r_block,
213-
transformers = transformers
221+
transformers = transformers,
222+
base_indention = base_indention
214223
) %>%
215224
unlist()
216225

0 commit comments

Comments
 (0)