From 1406d7a793d1133f4c56f90e0e5849cbfe47896b Mon Sep 17 00:00:00 2001 From: olivroy Date: Thu, 9 May 2024 09:50:36 -0400 Subject: [PATCH] Rename `regex_outline` to `pattern` + forgotten recurse arg --- NEWS.md | 2 + R/outline-criteria.R | 2 +- R/outline.R | 61 +++++++++++++++--------------- R/proj-list.R | 20 +++++----- README.md | 2 +- man/outline.Rd | 14 +++---- man/proj_file.Rd | 4 +- tests/testthat/_snaps/outline.md | 6 +-- tests/testthat/_snaps/proj-list.md | 2 +- tests/testthat/test-outline.R | 6 +-- 10 files changed, 61 insertions(+), 58 deletions(-) diff --git a/NEWS.md b/NEWS.md index 83f68d1..17e9cdf 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # reuseme (development version) +* in `file/proj/dir_outline()` `regex_outline` is now `pattern` + * `proj_outline()` and `dir_outline()` now excludes example files * `file_outline()` better support for TODO in md files diff --git a/R/outline-criteria.R b/R/outline-criteria.R index cdfaaf8..2da739f 100644 --- a/R/outline-criteria.R +++ b/R/outline-criteria.R @@ -35,7 +35,7 @@ o_is_todo_fixme <- function(x) { !stringr::str_starts(candidates, "\\s*\"\\s*") & !stringr::str_detect(candidates, "extract_tag_in_text") & !o_is_roxygen_comment(candidates) & # don't put these tags in documentation :) - !stringr::str_detect(candidates, "grepl?\\(|g?sub\\(|str_detect|str_remove|str_extract|regex_outline\\s|use_todo|,\\stodo\\)|TODO\\.R|TODO file|@param") & + !stringr::str_detect(candidates, "grepl?\\(|g?sub\\(|str_detect|str_remove|str_extract|use_todo|,\\stodo\\)|TODO\\.R|TODO file|@param") & !stringr::str_detect(candidates, "[:upper:]\"|[:upper:]{4,10} item") & # eliminate false positives !stringr::str_detect(candidates, "\".{0,100}(TODO|FIXME|WORK)") # remove some true negs for now. has_todo diff --git a/R/outline.R b/R/outline.R index cf536a0..ecc59db 100644 --- a/R/outline.R +++ b/R/outline.R @@ -6,7 +6,7 @@ #' This will fail if you are trying to map an unsaved file. #' #' If `work_only` is set to `TRUE`, the function will only return outline of the `# WORK` comment -#' in `path`. `work_only = TRUE` will have an effect on regex_outline. +#' in `path`. `work_only = TRUE` will have an effect on `pattern`. #' #' By default #' * `file_outline()` prints the outline the [active document][active_rs_doc()] if in RStudio @@ -23,12 +23,12 @@ #' complete][complete_todo()] #' @param path,proj A character vector of file paths, a [project][proj_list()]. #' Defaults to active file, project or directory. `rstudioapi::documentPath()` -#' @param regex_outline A string or regex to search for in the outline. If +#' @param pattern A string or regex to search for in the outline. If #' specified, will search only for elements matching this regular expression. -#' The print method will show the document title for context. +#' The print method will show the document title for context. Previously `regex_outline` #' @param work_only If `TRUE`, (the default), will only show you work items #' first. Set to `FALSE` if you want to see the full outline. `WORK` will -#' combine with `regex_outline` +#' combine with `pattern` #' @param print_todo Should include TODOs in the file outline? If `FALSE`, will #' print a less verbose output with sections. #' @param alpha Whether to show in alphabetical order @@ -63,7 +63,7 @@ NULL #' @export #' @rdname outline -file_outline <- function(regex_outline = NULL, +file_outline <- function(pattern = NULL, path = active_rs_doc(), work_only = TRUE, alpha = FALSE, @@ -131,39 +131,39 @@ file_outline <- function(regex_outline = NULL, } # Will append Work to the regexp outline, if it was provided. - # otherwise sets regex_outline to anything + # otherwise sets pattern to anything if (should_show_only_work_items) { cli::cli_inform("Use {.code work_only = FALSE} to display the full file/project outline.") - regex_outline <- paste(c("work\\s", regex_outline), collapse = "|") + pattern <- paste(c("work\\s", pattern), collapse = "|") } else { - regex_outline <- regex_outline %||% ".+" + pattern <- pattern %||% ".+" } - check_string(regex_outline, arg = "You may have specified path Internal error") + check_string(pattern, arg = "You may have specified path Internal error") file_sections00 <- define_outline_criteria(file_content, print_todo = print_todo) # filter for interesting items. file_sections0 <- keep_outline_element(file_sections00) - if (!grepl(".+", regex_outline, fixed = TRUE)) { - # keep files where regex_outline was detected (not the generic .+) + if (!grepl(".+", pattern, fixed = TRUE)) { + # keep files where pattern was detected (not the generic .+) file_sections0 <- dplyr::filter( file_sections0, - any(grepl(regex_outline, content, ignore.case = TRUE)), + any(grepl(pattern, content, ignore.case = TRUE)), .by = "file" ) } if (nrow(file_sections0) == 0) { - if (is_active_doc && !identical(regex_outline, ".+")) { - msg <- c("{.code regex_outline = {.val {regex_outline}}} did not return any results looking in the active document.", - "i" = "Did you mean to use {.run reuseme::file_outline(path = {.str {regex_outline}})}?" + if (is_active_doc && !identical(pattern, ".+")) { + msg <- c("{.code pattern = {.val {pattern}}} did not return any results looking in the active document.", + "i" = "Did you mean to use {.run reuseme::file_outline(path = {.str {pattern}})}?" ) - } else if (!identical(regex_outline, ".+")) { + } else if (!identical(pattern, ".+")) { msg <- c( - "{.code regex_outline = {.val {regex_outline}}} did not return any results looking in {length(path)} file{?s}.", - "i" = "Run {.run [{.fn proj_file}](reuseme::proj_file(\"{regex_outline}\"))} to search in file names too." + "{.code pattern = {.val {pattern}}} did not return any results looking in {length(path)} file{?s}.", + "i" = "Run {.run [{.fn proj_file}](reuseme::proj_file(\"{pattern}\"))} to search in file names too." ) } else { msg <- "Empty outline." @@ -179,7 +179,7 @@ file_outline <- function(regex_outline = NULL, file_sections1 <- display_outline_element(file_sections0) # Create hyperlink in console - file_sections <- construct_outline_link(file_sections1, is_saved_doc, dir_common, regex_outline) + file_sections <- construct_outline_link(file_sections1, is_saved_doc, dir_common, pattern) if (alpha) { # remove inline markup first before sorting alphabetically @@ -206,20 +206,20 @@ file_outline <- function(regex_outline = NULL, } #' @rdname outline #' @export -proj_outline <- function(regex_outline = NULL, proj = proj_get2(), work_only = TRUE, dir_tree = FALSE, alpha = FALSE, recent_only = FALSE) { +proj_outline <- function(pattern = NULL, proj = proj_get2(), work_only = TRUE, dir_tree = FALSE, alpha = FALSE, recent_only = FALSE) { is_active_proj <- identical(proj, proj_get2()) - if (is_active_proj && !is.null(regex_outline) && regex_outline %in% names(reuseme::proj_list())) { + if (is_active_proj && !is.null(pattern) && pattern %in% names(reuseme::proj_list())) { # only throw warning if proj is supplied cli::cli_warn(c( - "You specified {.arg regex_outline} = {.val {regex_outline}}", - i = "Did you mean to use `proj = {.val {regex_outline}}?" + "You specified {.arg pattern} = {.val {pattern}}", + i = "Did you mean to use `proj = {.val {pattern}}?" )) } if (is_active_proj) { return(dir_outline( - regex_outline = regex_outline, + pattern = pattern, work_only = work_only, dir_tree = dir_tree, alpha = alpha, @@ -259,16 +259,17 @@ proj_outline <- function(regex_outline = NULL, proj = proj_get2(), work_only = T } dir_outline( - regex_outline = regex_outline, + pattern = pattern, path = proj_dir, work_only = work_only, dir_tree = dir_tree, - alpha = alpha + alpha = alpha, + recurse = TRUE ) } #' @rdname outline #' @export -dir_outline <- function(regex_outline = NULL, path = ".", work_only = TRUE, dir_tree = FALSE, alpha = FALSE, recent_only = FALSE, recurse = FALSE) { +dir_outline <- function(pattern = NULL, path = ".", work_only = TRUE, dir_tree = FALSE, alpha = FALSE, recent_only = FALSE, recurse = FALSE) { dir <- fs::path_real(path) file_exts <- c("R", "qmd", "Rmd", "md", "Rmarkdown") file_exts_regex <- paste0("*.", file_exts, "$", collapse = "|") @@ -308,7 +309,7 @@ dir_outline <- function(regex_outline = NULL, path = ".", work_only = TRUE, dir_ invert = TRUE ) } - file_outline(path = file_list_to_outline, regex_outline = regex_outline, work_only = work_only, dir_common = dir, alpha = alpha, recent_only = recent_only) + file_outline(path = file_list_to_outline, pattern = pattern, work_only = work_only, dir_common = dir, alpha = alpha, recent_only = recent_only) } # Print method ------------------- @@ -521,7 +522,7 @@ define_important_element <- function(.data) { ) } -construct_outline_link <- function(.data, is_saved_doc, dir_common, regex_outline) { +construct_outline_link <- function(.data, is_saved_doc, dir_common, pattern) { rs_avail_file_link <- rstudioapi::isAvailable("2023.09.0.375") # better handling after .data <- define_important_element(.data) @@ -601,7 +602,7 @@ construct_outline_link <- function(.data, is_saved_doc, dir_common, regex_outlin rs_version = NULL, outline_el2 = NULL ) |> - dplyr::filter(is.na(outline_el) | tolower(outline_el) |> stringr::str_detect(tolower(regex_outline))) + dplyr::filter(is.na(outline_el) | tolower(outline_el) |> stringr::str_detect(tolower(pattern))) } trim_outline <- function(x, width) { # problematic in case_when diff --git a/R/proj-list.R b/R/proj-list.R index 2da18ca..d4e662e 100644 --- a/R/proj-list.R +++ b/R/proj-list.R @@ -35,19 +35,19 @@ proj_switch <- function(proj = NULL, new_session = TRUE) { #' #' @param file A filename or regexp to a file inside `proj` #' @param proj a project path or file [proj_list()] -#' @param regex_outline A regular expression to look for +#' @param pattern A regular expression to look for #' @return The file outline if multiple matches are found #' @export #' #' @examples #' try(proj_file("A non-existent file")) #' @family project management helpers -proj_file <- function(file = NULL, proj = NULL, regex_outline = NULL) { +proj_file <- function(file = NULL, proj = NULL, pattern = NULL) { rlang::check_required(file) - # search will only be conducted with regex_outline - if (is.null(regex_outline) && is.null(file)) { + # search will only be conducted with pattern + if (is.null(pattern) && is.null(file)) { cli::cli_abort( - "One of {.arg regex_outline} or {.arg file} must exist." + "One of {.arg pattern} or {.arg file} must exist." ) } file <- file %||% "A non-existent rubbish file placeholder" @@ -69,27 +69,27 @@ proj_file <- function(file = NULL, proj = NULL, regex_outline = NULL) { possible_files <- fs::path_filter(possible_files, regexp = file) if (length(possible_files) == 0) { - if (is.null(regex_outline)) { + if (is.null(pattern)) { cli::cli_abort("No match found for {.val {file}} in {.file {proj_path}}") } else { return(proj_outline( - regex_outline = regex_outline, + pattern = pattern, proj = proj )) } } if (length(possible_files) == 1) { - if (is.null(regex_outline)) { + if (is.null(pattern)) { open_rs_doc(possible_files) } else { - file_outline(regex_outline = regex_outline, path = possible_files) + file_outline(pattern = pattern, path = possible_files) } } cli::cli_inform(c( # TODO improve on this message "A couple files found. Access the desired place." )) - file_outline(regex_outline = regex_outline, path = possible_files) + file_outline(pattern = pattern, path = possible_files) } #' Returns a named project list options diff --git a/README.md b/README.md index adbbf05..dca0eed 100644 --- a/README.md +++ b/README.md @@ -333,7 +333,7 @@ outline #> #> ── `tests/testthat/_snaps/outline.md` #> `i` alpha and work_only arguments work -#> `i` regex_outline works as expected +#> `i` pattern works as expected #> #> ── `tests/testthat/_snaps/rename-files.md` 🕒 #> `i` Helper files returns the expected input diff --git a/man/outline.Rd b/man/outline.Rd index 11bcc25..2083c83 100644 --- a/man/outline.Rd +++ b/man/outline.Rd @@ -8,7 +8,7 @@ \title{Print interactive outline of file sections} \usage{ file_outline( - regex_outline = NULL, + pattern = NULL, path = active_rs_doc(), work_only = TRUE, alpha = FALSE, @@ -18,7 +18,7 @@ file_outline( ) proj_outline( - regex_outline = NULL, + pattern = NULL, proj = proj_get2(), work_only = TRUE, dir_tree = FALSE, @@ -27,7 +27,7 @@ proj_outline( ) dir_outline( - regex_outline = NULL, + pattern = NULL, path = ".", work_only = TRUE, dir_tree = FALSE, @@ -37,16 +37,16 @@ dir_outline( ) } \arguments{ -\item{regex_outline}{A string or regex to search for in the outline. If +\item{pattern}{A string or regex to search for in the outline. If specified, will search only for elements matching this regular expression. -The print method will show the document title for context.} +The print method will show the document title for context. Previously \code{regex_outline}} \item{path, proj}{A character vector of file paths, a \link[=proj_list]{project}. Defaults to active file, project or directory. \code{rstudioapi::documentPath()}} \item{work_only}{If \code{TRUE}, (the default), will only show you work items first. Set to \code{FALSE} if you want to see the full outline. \code{WORK} will -combine with \code{regex_outline}} +combine with \code{pattern}} \item{alpha}{Whether to show in alphabetical order} @@ -74,7 +74,7 @@ RStudio project, or directories This will fail if you are trying to map an unsaved file. If \code{work_only} is set to \code{TRUE}, the function will only return outline of the \verb{# WORK} comment -in \code{path}. \code{work_only = TRUE} will have an effect on regex_outline. +in \code{path}. \code{work_only = TRUE} will have an effect on \code{pattern}. By default \itemize{ diff --git a/man/proj_file.Rd b/man/proj_file.Rd index 05b2ef4..f2331f3 100644 --- a/man/proj_file.Rd +++ b/man/proj_file.Rd @@ -4,14 +4,14 @@ \alias{proj_file} \title{Active a file at location} \usage{ -proj_file(file = NULL, proj = NULL, regex_outline = NULL) +proj_file(file = NULL, proj = NULL, pattern = NULL) } \arguments{ \item{file}{A filename or regexp to a file inside \code{proj}} \item{proj}{a project path or file \code{\link[=proj_list]{proj_list()}}} -\item{regex_outline}{A regular expression to look for} +\item{pattern}{A regular expression to look for} } \value{ The file outline if multiple matches are found diff --git a/tests/testthat/_snaps/outline.md b/tests/testthat/_snaps/outline.md index ae53faf..dec43d3 100644 --- a/tests/testthat/_snaps/outline.md +++ b/tests/testthat/_snaps/outline.md @@ -62,12 +62,12 @@ `i` A great title `i` TODO improve this Viz!- `Donev?` -# regex_outline works as expected +# pattern works as expected Code - file_outline(regex_outline = "not found", path = file) + file_outline(pattern = "not found", path = file) Message - `regex_outline = "not found"` did not return any results looking in 1 file. + `pattern = "not found"` did not return any results looking in 1 file. i Run `` `proj_file()` `` to search in file names too. --- diff --git a/tests/testthat/_snaps/proj-list.md b/tests/testthat/_snaps/proj-list.md index 0977eab..fca7ff0 100644 --- a/tests/testthat/_snaps/proj-list.md +++ b/tests/testthat/_snaps/proj-list.md @@ -4,5 +4,5 @@ proj_file() Condition Error in `proj_file()`: - ! One of `regex_outline` or `file` must exist. + ! One of `pattern` or `file` must exist. diff --git a/tests/testthat/test-outline.R b/tests/testthat/test-outline.R index c2e2f37..5abdbdd 100644 --- a/tests/testthat/test-outline.R +++ b/tests/testthat/test-outline.R @@ -30,11 +30,11 @@ test_that("file_outline() is a data frame", { ) }) -test_that("regex_outline works as expected", { +test_that("pattern works as expected", { # TODO change tests for data frame size when stable (efficiency). As still debugging, better to keep all snapshots. # The idea is to show doc title + regex outline match when relevant file <- fs::path_package("reuseme", "example-file", "outline-script.R") - expect_snapshot(file_outline(regex_outline = "not found", path = file)) + expect_snapshot(file_outline(pattern = "not found", path = file)) expect_snapshot({ file_outline("Viz", path = file) }, @@ -69,5 +69,5 @@ test_that("file_outline() contains function calls", { }) test_that("dir_outline() works with no error", { - expect_no_error(dir_outline(regex_outline = ".+", path = test_path("_ref"))) + expect_no_error(dir_outline(pattern = ".+", path = test_path("_ref"))) })