Skip to content

Commit e2388b7

Browse files
basic code template for styling roxygen code examples
1 parent 53ae5e1 commit e2388b7

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Collate:
5555
'serialize.R'
5656
'set-assert-args.R'
5757
'style-guides.R'
58+
'style-roxgen-examples.R'
5859
'styler.R'
5960
'testing.R'
6061
'token-create.R'

R/style-roxgen-examples.R

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#' Figure out where code examples start and stop
2+
#'
3+
#' Finds the start and stop indices of the lines in `text` that are
4+
#' code examples in roxygen comments.
5+
#' @param text
6+
identify_start_and_stop_of_royxgen_examples_from_text <- function(text) {
7+
starts <- identify_start_points(text)
8+
stop_candidates <- identify_stop_candidates(text)
9+
stops <- map_int(starts, match_stop_to_start,
10+
stop_candidates = stop_candidates
11+
)
12+
NULL
13+
}
14+
15+
identify_start_stop_of_royxgen_examples_from_paths <- function(path) {
16+
content <- enc::read_lines_enc(path) # ensure file can be read
17+
identify_start_stop_of_royxgen_examples_from_text(content)
18+
list(
19+
c(5, 9),
20+
c(18, 39)
21+
)
22+
}
23+
24+
match_stop_to_start <- function(start, stop_candidates) {
25+
NULL
26+
}
27+
28+
#' TODO:
29+
#' * move to R/ui.R
30+
#' * same arguments as other stylers
31+
#' * add include_roxygen_code_examples argument to style_pkg() et al.
32+
#' * export
33+
style_roxygen_code_examples <- function(path) {
34+
map(path, style_roxygen_code_examples_one)
35+
}
36+
37+
style_roxygen_code_examples_one <- function(path) {
38+
full_file_content <- enc::read_lines_enc(path)
39+
start_stop_sequences <- identify_start_and_stop_of_royxgen_examples_from_paths(
40+
full_file_content
41+
) %>%
42+
start_stop_pairs_to_sequences()
43+
44+
masked_examples <- extract_selected_lines_from_text(
45+
full_file_content, start_stop_sequences
46+
)
47+
48+
plain_examples <- map(start_stop_paris, remove_roxygen_mask,
49+
text = masked_examples
50+
)
51+
styled_examples <- map(plain_examples, style_text, ...)
52+
masked_examples <- map(styled_examples, add_roxygen_mask)
53+
full_file_content <- update_selected_lines_of_text(
54+
full_file_content, masked_examples, start_stop_sequences
55+
)
56+
enc::write_lines_enc(path, full_file_content)
57+
}
58+
59+
60+
remove_roxygen_mask <- function() {
61+
NULL
62+
}
63+
64+
add_roxygen_mask <- function() {
65+
NULL
66+
}

R/utils.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,16 @@ extend_if_comment <- function(pd, pos) {
9292
map_filetype_to_pattern <- function(filetype) {
9393
paste0("(", paste(set_and_assert_arg_filetype(filetype), collapse = "|"), ")$")
9494
}
95+
96+
97+
extract_selected_lines_from_text <- function() {
98+
NULL
99+
}
100+
101+
update_selected_lines_of_text <- function() {
102+
NULL
103+
}
104+
105+
start_stop_pairs_to_sequences <- function() {
106+
NULL
107+
}

0 commit comments

Comments
 (0)