Skip to content

Commit

Permalink
Rename regex_outline to pattern + forgotten recurse arg
Browse files Browse the repository at this point in the history
  • Loading branch information
olivroy committed May 9, 2024
1 parent 9f6bcef commit 1406d7a
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 58 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion R/outline-criteria.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
61 changes: 31 additions & 30 deletions R/outline.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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."
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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 = "|")
Expand Down Expand Up @@ -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 -------------------
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions R/proj-list.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions man/outline.Rd

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

4 changes: 2 additions & 2 deletions man/proj_file.Rd

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

6 changes: 3 additions & 3 deletions tests/testthat/_snaps/outline.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

---
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/proj-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

6 changes: 3 additions & 3 deletions tests/testthat/test-outline.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
Expand Down Expand Up @@ -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")))
})

0 comments on commit 1406d7a

Please sign in to comment.