-
Notifications
You must be signed in to change notification settings - Fork 73
Style roxygen code examples #381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Style roxygen code examples #381
Conversation
6e64394
to
23f2d8e
Compare
R/style-roxgen-examples.R
Outdated
|
||
plain_examples <- map(start_stop_sequences, remove_roxygen_mask, | ||
text = masked_examples | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oups this seems to need mapping over both sequences anybody masked examples (which is a list with each element corresponding to one example).
As noted in #332, we should aim for consistency with styling of Rmd files. In particular, styling has to happen inside Probably a better and more consistent approach would be to see code examples as part of the code body that is styled with transformer functions. That would imply to
Probably that's too complicated if not infeasible. In addition, we should use consistent naming for file names. R/transform-code.R should have rmd in the name since it mostly deals with rmd. The first function Since this will lead to an API change, we better think twice how we name the argument and what its values can be. In consideration of extending style functionality for roxygen comments, e.g. by maximal line width, indention etc. as suggested in #332 (comment), we may want to call the argument |
e1dc788
to
30df9c0
Compare
Further todo:
|
30df9c0
to
fa90de7
Compare
R/transform-files.R
Outdated
} | ||
} | ||
|
||
#' @importFrom purrr map_at flatten_chr | ||
parse_transform_serialize_roxygen <- function(text, transformers) { | ||
roxygen_seqs <- identify_start_to_stop_of_roxygen_examples_from_text(text) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably need to split that up into multiple functions. Also, some of it might be re-used for dontrun handling.
447ea81
to
5af7bef
Compare
Codecov Report
@@ Coverage Diff @@
## master #381 +/- ##
==========================================
- Coverage 90.63% 90.12% -0.52%
==========================================
Files 30 34 +4
Lines 1474 1570 +96
==========================================
+ Hits 1336 1415 +79
- Misses 138 155 +17
Continue to review full report at Codecov.
|
@jonmcalder you can review if you want. |
@krlmlr any idea how to handle |
Maybe with file <- tempfile()
writeLines(
c(
"\\examples{",
"\\dontrun{",
"TRUE",
"}",
"}"
),
file)
parsed <- tools::parse_Rd(
file
)
str(parsed)
#> List of 2
#> $ :List of 3
#> ..$ : atomic [1:1]
#>
#> .. ..- attr(*, "Rd_tag")= chr "RCODE"
#> .. ..- attr(*, "srcref")=Class 'srcref' atomic [1:6] 1 11 1 11 11 11
#> .. .. .. ..- attr(*, "srcfile")=Class 'srcfile' <environment: 0x55eafd898290>
#> ..$ :List of 2
#> .. ..$ : atomic [1:1]
#>
#> .. .. ..- attr(*, "Rd_tag")= chr "VERB"
#> .. .. ..- attr(*, "srcref")=Class 'srcref' atomic [1:6] 2 10 2 10 10 10
#> .. .. .. .. ..- attr(*, "srcfile")=Class 'srcfile' <environment: 0x55eafd898290>
#> .. ..$ : atomic [1:1] TRUE
#>
#> .. .. ..- attr(*, "Rd_tag")= chr "VERB"
#> .. .. ..- attr(*, "srcref")=Class 'srcref' atomic [1:6] 3 1 3 5 1 5
#> .. .. .. .. ..- attr(*, "srcfile")=Class 'srcfile' <environment: 0x55eafd898290>
#> .. ..- attr(*, "Rd_tag")= chr "\\dontrun"
#> .. ..- attr(*, "srcref")=Class 'srcref' atomic [1:6] 2 1 4 1 1 1
#> .. .. .. ..- attr(*, "srcfile")=Class 'srcfile' <environment: 0x55eafd898290>
#> ..$ : atomic [1:1]
#>
#> .. ..- attr(*, "Rd_tag")= chr "RCODE"
#> .. ..- attr(*, "srcref")=Class 'srcref' atomic [1:6] 4 2 4 2 2 2
#> .. .. .. ..- attr(*, "srcfile")=Class 'srcfile' <environment: 0x55eafd898290>
#> ..- attr(*, "Rd_tag")= chr "\\examples"
#> ..- attr(*, "srcref")=Class 'srcref' atomic [1:6] 1 1 5 1 1 1
#> .. .. ..- attr(*, "srcfile")=Class 'srcfile' <environment: 0x55eafd898290>
#> $ : atomic [1:1]
#>
#> ..- attr(*, "Rd_tag")= chr "TEXT"
#> ..- attr(*, "srcref")=Class 'srcref' atomic [1:6] 5 2 5 2 2 2
#> .. .. ..- attr(*, "srcfile")=Class 'srcfile' <environment: 0x55eafd898290>
#> - attr(*, "class")= chr "Rd"
#> - attr(*, "srcref")=Class 'srcref' atomic [1:6] 0 0 6 1 0 1
#> .. ..- attr(*, "srcfile")=Class 'srcfile' <environment: 0x55eafd898290>
#> - attr(*, "macros")=<environment: 0x55eafd71e0b0>
|
Well I am not sure if we want to replace source code with styled derivatives that are potentially out of date. |
|
Yes sure, not always, but it can be outdated. Let's say you update a code example in your roxygen comments and then run styler without doing devtools::document(). This will replace the source with its styled derivative and you will loose your changes. So I wondered if there is a way around taking code directly from roxygen comments, which we can do if it does not involve a dontrun. |
How about
? |
This is what we have but that does not allow |
2a) Run
3a) Serialize the parsed structure. |
So I am not sure if I understand you correctly, you want the following:
Here an illustration of step 3 to 5: library(magrittr)
file <- tempfile()
writeLines(
c(
"\\examples{",
"hi <- 3",
"\\dontrun{",
"TRUE", "function(x) {", " x", "}",
"}",
"x",
"}"
),
file
)
parsed <- tools::parse_Rd(
file,
fragment = TRUE
)
#> Warning in tools::parse_Rd(file, fragment = TRUE): /var/folders/8l/
#> fhmv3yj12kncddcjqwc19tkr0000gr/T//Rtmp6bX2B6/file20645f19d510:1: unexpected
#> section header '\examples'
char <- as.character(parsed, deparse = TRUE)
char
#> [1] "" "{" "\n"
#> [4] "hi <- 3\n" "\\dontrun" "{"
#> [7] "\n" "TRUE\n" "function(x) {\n"
#> [10] " x\n" "}\n" "}"
#> [13] "\n" "x\n" "}"
#> [16] "\n"
# styline the blocks in palce.
# Code here
paste0("\\examples", paste0(char, collapse = "")) %>% cat()
#> \examples{
#> hi <- 3
#> \dontrun{
#> TRUE
#> function(x) {
#> x
#> }
#> }
#> x
#> } Created on 2018-05-21 by the reprex package (v0.2.0). Problem: |
Oh we could just use textConnection to feed in a string into |
Yes, it's a bit of an effort but I think it's worth it to have nicely styled examples. |
Ok, I agree. I think me and @jonmcalder have already put in some time, so I think it's worth going forward this way. I think we need a lot of TDD to accommodate all edge cases. |
We can also set up a test infrastructure on Travis to download and style random CRAN packages for testing. |
d0af5a4
to
c31b5a7
Compare
See #381 (comment) |
I think it's good practice to leave a PR for a few days and go back afterwards. I think I got the big picture right, but there are some small things that can simplify the code (and add the feature preserving line-breaks). |
b347652
to
688feff
Compare
Will do a PR on top of this PR to solve various problems. -.- |
Attempt to use clear definitions for code line: * until immediately before transofmriong: \n characterizes line break, * map the previous definition to the styler definition. One line is one element. Can't simply paste0(...) %>% strplit("\n", fixed = TRUE) I think.
…mediately will be merged into one.
Finally, it seems we got it right 🎉 |
We just added ~3000 lines of code to styler. Most of it is tests though, but still a few hundred in the library(dplyr)
log <- gitsum::parse_log_detailed_full_run()
log %>%
arrange(desc(date)) %>%
slice(1:63) %>%
unnest(nested) %>%
filter(dirname(changed_file) == "R") %>%
summarize(sum(insertions), sum(deletions))
#> # A tibble: 1 x 2
#> `sum(insertions)` `sum(deletions)`
#> <int> <int>
#> 1 488 354 |
identify_start_and_stop_of_royxgen_examples_from_text()
), at least as long as we are in the experimental state.To do:
string-manipulation.R
,string-creation.R
)