From a539ba8ce77b4af0d15e555b844674abdbdb9f18 Mon Sep 17 00:00:00 2001 From: Indrajeet Patil Date: Sat, 29 Jun 2024 14:54:55 +0200 Subject: [PATCH 1/3] Use raw strings (introduced in R 4.0) --- R/roxygen-examples-parse.R | 12 ++++++------ R/rules-tokens.R | 5 +---- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/R/roxygen-examples-parse.R b/R/roxygen-examples-parse.R index a428acae4..c89418f89 100644 --- a/R/roxygen-examples-parse.R +++ b/R/roxygen-examples-parse.R @@ -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 @@ -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 @@ -107,7 +107,7 @@ 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 } } @@ -115,7 +115,7 @@ roxygen_remove_extra_brace <- function(parsed) { } } else { # this will error informatively - parse(text = gsub("^\\\\[[:alpha:]]*", "", parsed_)) + parse(text = gsub(R"(^\\[[:alpha:]]*)", "", parsed_)) } parsed } @@ -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 diff --git a/R/rules-tokens.R b/R/rules-tokens.R index 1a2e857a4..5518e47de 100644 --- a/R/rules-tokens.R +++ b/R/rules-tokens.R @@ -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 @@ -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 } From 2a4c6ccfd5672f0d3b2aac56a90518302f2f20c3 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Sat, 29 Jun 2024 12:56:28 +0000 Subject: [PATCH 2/3] pre-commit --- man/fix_quotes.Rd | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/man/fix_quotes.Rd b/man/fix_quotes.Rd index ab0586bc2..06629b7a0 100644 --- a/man/fix_quotes.Rd +++ b/man/fix_quotes.Rd @@ -10,9 +10,7 @@ fix_quotes(pd_flat) \item{pd_flat}{A flat parse table.} } \description{ -We do not use \code{deparse()} as in previous implementations but \code{paste0()} since -the former approach escapes the reverse backslash in the line break character -\verb{\\\\n} whereas the solution with \code{paste0()} does not. +Replace single quotes with double quotes } \examples{ style_text("'here From aa9bf83f73d0109335df2dbb6b291980b5c382b0 Mon Sep 17 00:00:00 2001 From: Indrajeet Patil Date: Sat, 29 Jun 2024 15:07:58 +0200 Subject: [PATCH 3/3] more raw strings --- R/detect-alignment.R | 4 ++-- R/roxygen-examples-add-remove.R | 8 ++------ R/roxygen-examples-find.R | 4 ++-- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/R/detect-alignment.R b/R/detect-alignment.R index bf8ffbe7c..cab7e00a4 100644 --- a/R/detect-alignment.R +++ b/R/detect-alignment.R @@ -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) diff --git a/R/roxygen-examples-add-remove.R b/R/roxygen-examples-add-remove.R index 3a7ecb40b..f311c83e4 100644 --- a/R/roxygen-examples-add-remove.R +++ b/R/roxygen-examples-add-remove.R @@ -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) } @@ -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) } #' Add the roxygen mask to code diff --git a/R/roxygen-examples-find.R b/R/roxygen-examples-find.R index bf61983c9..49955265e 100644 --- a/R/roxygen-examples-find.R +++ b/R/roxygen-examples-find.R @@ -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)) )) %>%