Skip to content
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

[WIP] Use raw strings (from R 4.0) #1220

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions R/detect-alignment.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ token_is_on_aligned_line <- function(pd_flat) {
is_aligned <- length(unique(current_col)) == 1L
if (!is_aligned || length(current_col) < 2L) {
# check 2: left aligned after , (comma to next token)
current_col <- "^(,[\\s\\t]*)[^ ]*.*$" %>%
gsub("\\1", by_line, perl = TRUE) %>%
current_col <-
gsub(R"(^(,[\s\t]*)[^ ]*.*$)", R"(\1)", by_line, perl = TRUE) %>%
nchar() %>%
magrittr::subtract(1L)

Expand Down
8 changes: 2 additions & 6 deletions R/roxygen-examples-add-remove.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@ remove_dont_mask <- function(roxygen) {
)
}

remove_blank_lines <- function(code) {
code[code != "\n"]
}

remove_roxygen_mask <- function(text) {
code_with_header <- gsub(pattern = "^#'\\s?", "", text)
code_with_header <- gsub(pattern = R"(^#'\s?)", "", text)
remove_roxygen_header(code_with_header)
}

Expand All @@ -29,7 +25,7 @@ remove_roxygen_mask <- function(text) {
#' #' @examples c(1, 2)
#' @keywords internal
remove_roxygen_header <- function(text) {
gsub("^[\\s\t]*@examples(If)?(\\s|\t)*", "", text, perl = TRUE)
gsub(R"(^[\s\t]*@examples(If)?(\s|\t)*)", "", text, perl = TRUE)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this a bug? That is, should this actually have been \\s|\\t?

Copy link
Collaborator

@lorenzwalthert lorenzwalthert Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\s stands for space, no? Or should be \\s to escape? It should capture spaces and tabs.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@IndrajeetPatil are you confident in these topics?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, but, if that's a question we need to ask, I think our testing suite needs an upgrade 😉

At any rate, I am going to convert this PR to a draft since I'd much rather work on updates needed to make the next CRAN release happen ASAP.

}

#' Add the roxygen mask to code
Expand Down
4 changes: 2 additions & 2 deletions R/roxygen-examples-find.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
#' @param text A text consisting of code and/or roxygen comments.
#' @keywords internal
identify_start_to_stop_of_roxygen_examples_from_text <- function(text) {
starts <- grep("^#'(\\s|\t)*@examples(If\\s|\\s|\t|$)", text, perl = TRUE)
starts <- grep(R"{^#'(\s|\t)*@examples(If\s|\s|\t|$)}", text, perl = TRUE)
if (length(starts) < 1L) {
return(integer())
}
stop_candidates <- which(magrittr::or(
# starts with code or a tag
grepl("(^[^#]|^#'[\\s\t]*@)", text, perl = TRUE),
grepl(R"{(^[^#]|^#'[\s\t]*@)}", text, perl = TRUE),
# starts with a roxygen comment with a blank line after
grepl("^ *\t*$", text) & grepl("^#' *", lead(text))
)) %>%
Expand Down
12 changes: 6 additions & 6 deletions R/roxygen-examples-parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ parse_roxygen <- function(roxygen) {
roxygen_remove_extra_brace <- function(parsed) {
parsed <- rlang::try_fetch(
{
parse(text = paste(gsub("^\\\\[[:alpha:]]*", "", parsed), collapse = ""))
parse(text = paste(gsub(R"(^\\[[:alpha:]]*)", "", parsed), collapse = ""))
parsed
},
error = function(e) {
# might have extra braces that are not needed: try to remove them

# if fails, you need initial input for best error message
parsed_ <- gsub("^\\\\[[:alpha:]]+", "", parsed)
parsed_ <- gsub(R"(^\\[[:alpha:]]+)", "", parsed)
worth_trying_to_remove_brace <- any(parsed == "}")
if (worth_trying_to_remove_brace) {
# try to remove one and see if you can parse. If not, another one, until
Expand All @@ -95,7 +95,7 @@ roxygen_remove_extra_brace <- function(parsed) {
worth_trying_to_remove_brace <- rlang::try_fetch(
{
# this will error informatively
parse(text = gsub("^\\\\[[:alpha:]]+", "", parsed))
parse(text = gsub(R"(^\\[[:alpha:]]+)", "", parsed))
# if parsing succeeds, we can stop tryint to remove brace and move
# on with parsed
FALSE
Expand All @@ -107,15 +107,15 @@ roxygen_remove_extra_brace <- function(parsed) {
} else {
# this will error informatively. If not, outer loop will fail
# informatively
parse(text = gsub("^\\\\[[:alpha:]]+", "", parsed_))
parse(text = gsub(R"(^\\[[:alpha:]]+)", "", parsed_))
FALSE
}
}
)
}
} else {
# this will error informatively
parse(text = gsub("^\\\\[[:alpha:]]*", "", parsed_))
parse(text = gsub(R"(^\\[[:alpha:]]*)", "", parsed_))
}
parsed
}
Expand Down Expand Up @@ -167,7 +167,7 @@ emulate_rd <- function(roxygen) {
#' @keywords internal
needs_rd_emulation <- function(roxygen) {
# escape characters \ and % count, but not macros like \dontrun
any(grepl("\\\\|%", gsub("^#'\\s*\\\\[[:alpha:]]*", "", roxygen)))
any(grepl(R"(\\|%)", gsub(R"(^#'\s*\\[[:alpha:]]*)", "", roxygen)))
}

#' Changing the line definition
Expand Down
5 changes: 1 addition & 4 deletions R/rules-tokens.R
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,6 @@ if_for_while_part_requires_braces <- function(pd, key_token) {

#' Replace single quotes with double quotes
#'
#' We do not use `deparse()` as in previous implementations but `paste0()` since
#' the former approach escapes the reverse backslash in the line break character
#' `\\n` whereas the solution with `paste0()` does not.
#' @examples
#' style_text("'here
#' is a string
Expand Down Expand Up @@ -215,6 +212,6 @@ fix_quotes_one <- function(x) {
xi <- gsub(rx, '"\\1"', x[i])

# Replace inner escaped quotes (\') by ' and keep all other instances of \., including \\
x[i] <- gsub("\\\\(')|(\\\\[^'])", "\\1\\2", xi)
x[i] <- gsub(R"(\\(')|(\\[^']))", R"(\1\2)", xi)
x
}
4 changes: 1 addition & 3 deletions man/fix_quotes.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading