Skip to content

Commit

Permalink
multiple extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
stla committed Apr 24, 2024
1 parent b86fd69 commit 83562f8
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 25 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# findInFiles 0.5.0

* The argument `ext` of the `findInFiles` function has been renamed to
`extensions` and it can be a vector of file extensions now.

* The arguments `excludePattern` and `excludeFoldersPattern` of the
`findInFiles` function can be some vectors now, to give multiple patterns.

Expand Down
19 changes: 10 additions & 9 deletions R/findInFiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
#' @name findInFiles
#' @rdname findInFiles
#'
#' @param ext file extension, e.g. \code{"R"} or \code{"js"}
#' @param extensions extension(s) of the files to include in the search,
#' e.g. \code{"R"} or \code{"js"} or \code{c("R", "Rmd")}
#' @param pattern pattern to search for, a regular expression, e.g.
#' \code{"function"} or \code{"^function"}
#' @param depth depth of the search, \code{NULL} or a negative number for an
Expand Down Expand Up @@ -66,7 +67,7 @@
#' excludePattern = c("*.min.css", "selectize*", "shiny*")
#' )
findInFiles <- function(
ext, pattern, depth = NULL, maxCountPerFile = NULL, maxCount = NULL,
extensions, pattern, depth = NULL, maxCountPerFile = NULL, maxCount = NULL,
wholeWord = FALSE, ignoreCase = FALSE, perl = FALSE,
includePattern = NULL,
excludePattern = NULL, excludeFoldersPattern = NULL,
Expand All @@ -78,12 +79,12 @@ findInFiles <- function(
return(invisible(NULL))
}

stopifnot(isString(ext))

if(isBinaryExtension(ext)){
stop(
sprintf("This file extension ('%s') is not allowed.", ext)
)
for(ext in extensions) {
if(isBinaryExtension(ext)){
stop(
sprintf("This file extension ('%s') is not allowed.", ext)
)
}
}

stopifnot(isString(output))
Expand All @@ -108,7 +109,7 @@ findInFiles <- function(
)

results <- grepInFiles(
ext = ext, pattern = pattern, depth = depth,
ext = extensions, pattern = pattern, depth = depth,
maxCountPerFile = maxCountPerFile, maxCount = maxCount,
wholeWord = wholeWord, ignoreCase = ignoreCase, perl = perl,
includePattern = includePattern,
Expand Down
25 changes: 15 additions & 10 deletions R/grepInFiles.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
getFiles <- function(ext, depth){
getFiles <- function(extensions, depth){
stopifnot(isPositiveInteger(depth))
wildcards <- Reduce(
file.path, x = rep("*", depth), init = paste0("*.", ext),
right = TRUE, accumulate = TRUE
)
wildcards <- do.call(c, lapply(extensions, function(ext) {
Reduce(
file.path, x = rep("*", depth), init = paste0("*.", ext),
right = TRUE, accumulate = TRUE
)
}))
Sys.glob(wildcards)
}

#' @importFrom utils head
#' @noRd
grepInFiles <- function(
ext, pattern, depth, maxCountPerFile, maxCount,
wholeWord, ignoreCase, perl,
includePattern, excludePattern, excludeFoldersPattern,
directory, output
ext, pattern, depth, maxCountPerFile, maxCount,
wholeWord, ignoreCase, perl,
includePattern, excludePattern, excludeFoldersPattern,
directory, output
){
if(inSolaris()){
if(Sys.which("ggrep") == ""){
Expand All @@ -24,7 +26,10 @@ grepInFiles <- function(
stop("This package requires the 'grep' command-line utility.")
}
}
stopifnot(isString(ext))
check <- all(vapply(ext, isString, logical(1L)))
if(!check) {
stop("Invalid file extension.")
}
stopifnot(isString(pattern))
stopifnot(isBoolean(wholeWord))
stopifnot(isBoolean(ignoreCase))
Expand Down
6 changes: 4 additions & 2 deletions inst/shinyApp/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ shinyServer(function(input, output, session){
iv <- InputValidator$new()
iv$add_rule(
"ext",
sv_regex("^[a-zA-Z0-9\\+]+$", "Only alphanumeric or 'c++'")
sv_regex("^[a-zA-Z0-9\\+,]+$", "Only alphanumeric or 'c++'")
)
iv$add_rule("pattern", sv_required())
iv$add_rule("depth", isPositiveIntegerOrNA)
Expand Down Expand Up @@ -222,8 +222,10 @@ shinyServer(function(input, output, session){

output[["results"]] <- renderFIF({
req(Run())
extensions <- strsplit(isolate(input[["ext"]]), ",", fixed = TRUE)[[1L]]
extensions <- Filter(function(x) nchar(x) > 0L, extensions)
fifWidget <- findInFiles(
ext = isolate(input[["ext"]]),
extensions = extensions,
pattern = isolate(input[["pattern"]]),
depth = isolate(Depth()),
maxCount = isolate(maxCount()),
Expand Down
2 changes: 1 addition & 1 deletion inst/shinyApp/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ shinyUI(fluidPage(
),
br(),
smallInput(textInput(
"ext", "File extension:",
"ext", "Extensions (comma-separated):",
value = "R"
)),
textInput(
Expand Down
7 changes: 4 additions & 3 deletions man/findInFiles.Rd

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

0 comments on commit 83562f8

Please sign in to comment.