Skip to content

Commit 8583bdc

Browse files
Merge pull request #54 from lorenzwalthert/parse_comments
* correctly deal with comments (spacing before comments, start comment with space) * T more flexibly (`test_collection()` and friends now support `...`)
2 parents 7b8e52b + f8b37a9 commit 8583bdc

25 files changed

+218
-20
lines changed

API

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
## Exported functions
44

55
get_transformers(flat, ...)
6-
get_transformers_flat(strict = TRUE)
7-
get_transformers_nested(strict = TRUE, indent_by = 2)
6+
get_transformers_flat(strict = TRUE, start_comments_with_one_space = FALSE)
7+
get_transformers_nested(strict = TRUE, indent_by = 2, start_comments_with_one_space = FALSE)
88
style_pkg(pkg = ".", flat = FALSE, transformers = get_transformers(flat = flat))
99
style_src(path = ".", flat = FALSE, recursive = TRUE, transformers = get_transformers(flat = flat))
1010
style_text(text, flat = FALSE, transformers = get_transformers(flat = flat))

R/get_transformers.R

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@ get_transformers <- function(flat, ...) {
1818
#'
1919
#' @param strict A logical value indicating whether a set of strict
2020
#' or not so strict transformer functions should be returned.
21+
#' @param start_comments_with_one_space Whether or not comments should start
22+
#' with only one space (see [start_comments_with_space()]).
2123
#' @return A list of transformer functions that operate on flat parse
2224
#' tables.
2325
#' @export
2426
#' @family obtain transformers
25-
get_transformers_flat <- function(strict = TRUE) {
27+
#' @importFrom purrr partial
28+
get_transformers_flat <- function(strict = TRUE,
29+
start_comments_with_one_space = FALSE) {
2630
c(
2731
fix_quotes,
2832
remove_space_before_closing_paren,
@@ -33,6 +37,8 @@ get_transformers_flat <- function(strict = TRUE) {
3337
if (strict) set_space_after_comma else add_space_after_comma,
3438
remove_space_after_unary_pm,
3539
remove_space_after_opening_paren,
40+
partial(start_comments_with_space,
41+
force_one = start_comments_with_one_space),
3642
NULL)
3743
}
3844

@@ -46,12 +52,15 @@ get_transformers_flat <- function(strict = TRUE) {
4652
#' @family obtain transformers
4753
#' @importFrom purrr partial
4854
#' @export
49-
get_transformers_nested <- function(strict = TRUE, indent_by = 2) {
55+
get_transformers_nested <- function(strict = TRUE,
56+
indent_by = 2,
57+
start_comments_with_one_space = FALSE) {
5058
c(create_filler,
5159
partial(indent_round, indent_by = indent_by),
5260
partial(indent_curly, indent_by = indent_by),
5361
strip_eol_spaces,
54-
get_transformers_flat(strict),
62+
get_transformers_flat(strict, start_comments_with_one_space),
63+
set_space_before_comments,
5564
set_space_between_levels
5665
)
5766
}

R/nested.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@ tokenize <- function(text) {
4747
parse_data
4848
}
4949

50+
#' Helper for setting spaces
51+
#'
52+
#' @param spaces_after_prefix An integer vector with the number of spaces
53+
#' after the prefix.
54+
#' @param force_one Whether spaces_after_prefix should be set to one in all
55+
#' cases.
56+
#' @return An integer vector of length spaces_after_prefix, which is either
57+
#' one (if `force_one = TRUE`) or `space_after_prefix` with all values
58+
#' below one set to one.
59+
set_spaces <- function(spaces_after_prefix, force_one) {
60+
if (force_one) {
61+
n_of_spaces <- rep(1, length(spaces_after_prefix))
62+
} else {
63+
n_of_spaces <- if_else(spaces_after_prefix < 1L, 1L, spaces_after_prefix)
64+
}
65+
n_of_spaces
66+
}
67+
5068

5169
#' Nest a flat parse table
5270
#'

R/rules.R

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,41 @@ set_space_between_levels <- function(pd_flat) {
106106
pd_flat
107107
}
108108

109+
#' Start comments with a space
110+
#'
111+
#' Forces comments to start with a space, that is, after the regular expression
112+
#' "^#+'*", at least one space must follow. Multiple spaces may be legit for
113+
#' indention in some situations.
114+
#'
115+
#' @param pd A parse table.
116+
#' @param force_one Wheter or not to force one space or allow multiple spaces
117+
#' after the regex "^#+'*".
118+
start_comments_with_space <- function(pd, force_one = FALSE) {
119+
comments <- pd %>%
120+
filter(token == "COMMENT")
121+
122+
if (nrow(comments) == 0) return(pd)
123+
124+
non_comments <- pd %>%
125+
filter(token != "COMMENT")
126+
127+
comments <- comments %>%
128+
extract(text, c("prefix", "space_after_prefix", "text"),
129+
regex = "^(#+'*)( *)(.*)$") %>%
130+
mutate(space_after_prefix = nchar(space_after_prefix),
131+
space_after_prefix = set_spaces(space_after_prefix, force_one),
132+
text = paste0(prefix, rep_char(" ", space_after_prefix), text),
133+
short = substr(text, 1, 5)) %>%
134+
select(-space_after_prefix, -prefix)
135+
bind_rows(comments, non_comments) %>%
136+
arrange(line1, col1)
137+
}
138+
139+
140+
set_space_before_comments <- function(pd_flat) {
141+
comment_after <- pd_flat$token == "COMMENT"
142+
comment_before <- lead(comment_after, default = FALSE)
143+
pd_flat$spaces[comment_before & (pd_flat$newlines == 0L)] <- 1L
144+
pd_flat
145+
146+
}

R/serialized_tests.R

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
test_collection <- function(test, sub_test = NULL,
2525
write_back = FALSE,
2626
write_tree = TRUE,
27-
transformer) {
27+
transformer,
28+
...) {
2829
path <- rprojroot::find_testthat_root_file(test)
2930

3031
pattern <- if_else(!is.null(sub_test),
@@ -48,7 +49,8 @@ test_collection <- function(test, sub_test = NULL,
4849
transform_and_check,
4950
transformer = transformer,
5051
write_back = write_back,
51-
write_tree = write_tree)
52+
write_tree = write_tree,
53+
...)
5254
}
5355

5456
#' Construct *-out.R from a *-in.R
@@ -83,21 +85,22 @@ construct_tree <- function(in_paths, suffix = "_tree") {
8385
#' to the output file.
8486
#' @param write_tree Whether or not the tree structure of the test should be
8587
#' computed and written to a file.
88+
#' @param ... Parameters passed to transformer function.
8689
#' @param out_tree Name of tree file if written out.
8790
#' @importFrom utils write.table
8891
transform_and_check <- function(in_item, out_item,
8992
in_name = in_item, out_name = out_item,
9093
transformer, write_back,
9194
write_tree = FALSE,
92-
out_tree = "_tree") {
95+
out_tree = "_tree", ...) {
9396

9497
read_in <- utf8::read_lines_enc(in_item)
9598
if (write_tree) {
9699
create_tree(read_in) %>%
97100
write.table(out_tree, col.names = FALSE, row.names = FALSE, quote = FALSE)
98101
}
99102
transformed <- read_in %>%
100-
transformer()
103+
transformer(...)
101104
transformed <- suppressMessages(utf8::transform_lines_enc(out_item,
102105
function(x) transformed,
103106
write_back = write_back))

man/get_transformers_flat.Rd

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

man/get_transformers_nested.Rd

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

man/set_spaces.Rd

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/start_comments_with_space.Rd

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/test_collection.Rd

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

0 commit comments

Comments
 (0)