HughParsonage / hutils
Showing 2 of 18 files from the diff.

@@ -1,4 +1,6 @@
Loading
1 1
#' Find string pattern in (text) file
2 +
#' @description \code{goto_pattern_in} present from 1.6.0
3 +
#' 
2 4
#' @param file_contents A perl-regular expression as a search query.
3 5
#' @param basedir The root of the directory tree in which files will be searched recursively.
4 6
#' @param dir_recursive (logical, default: \code{TRUE}) Search within subdirectories of \code{basedir}?
@@ -16,9 +18,22 @@
Loading
16 18
#' @param file_contents_ignore_case (logical, default: \code{FALSE}) As in \code{\link[base]{grep}}.
17 19
#' @param file.ext A file extension passed to the operating system if \code{use.OS} is used.
18 20
#' @param which_lines One of \code{"first"} and \code{"all"}. If \code{"first"} only the first match in any file is returned in the result; if \code{"all"}, all matches are.
21 +
#' 
22 +
#' @param ... Arguments passed to \code{find_pattern_in}.
23 +
#' 
19 24
#' @return A \code{data.table}, showing the matches per file.
25 +
#' 
26 +
#' \code{goto_pattern_in} additionally prompts for a row of the returned results.
27 +
#' Using the \code{rstudioapi}, if available, RStudio will jump to the file 
28 +
#' and line number.
29 +
#' 
20 30
#' @details For convenience, if \code{file_contents} appears to be a directory
21 31
#' and \code{basedir} does not, the arguments are swapped, but with a warning.
32 +
#' 
33 +
#' 
34 +
#' 
35 +
#' 
36 +
#' 
22 37
#' @export
23 38
24 39
find_pattern_in <- function(file_contents,
@@ -47,11 +62,16 @@
Loading
47 62
            "Do not rely on this behaviour as it may change without notice.")
48 63
    file_contents %<->% basedir
49 64
  }
50 -
51 65
  
66 +
  reader_was_missing <- missing(reader)
52 67
  ..reader <- match.fun(reader)
53 68
  .reader <- function(x) {
54 -
    out <- ..reader(x)
69 +
    if (reader_was_missing) {
70 +
      # don't warn on bad newline by default
71 +
      out <- readLines(x, warn = FALSE)
72 +
    } else {
73 +
      out <- ..reader(x)
74 +
    }
55 75
    if (!include.comments) {
56 76
      if (is.null(comment.char)) {
57 77
        comment.char <-
@@ -69,6 +89,7 @@
Loading
69 89
  }
70 90
  
71 91
  
92 +
  
72 93
  if (length(file.ext)) {
73 94
    stopifnot(length(file.ext) == 1L,
74 95
              is.character(file.ext))
@@ -189,6 +210,24 @@
Loading
189 210
  if (identical(.Platform$OS.type,
190 211
                "windows")) .Deprecated("hutilsInteractive::find_pattern_in_windows")
191 212
}
213 +
214 +
215 +
#' @rdname find_pattern_in
216 +
#' @export
217 +
goto_pattern_in <- function(file_contents, ...) {
218 +
  if (!is.data.table(file_contents)) {
219 +
    found_patterns <- find_pattern_in(file_contents, ...)
220 +
  }
221 +
  print(found_patterns)
222 +
  RQ("rstudioapi", no = {
223 +
    i <- as.integer(readline(prompt = "Select row number "))
224 +
    file <- found_patterns[i][["file"]]
225 +
    line_no <- found_patterns[i][["line_no"]]
226 +
    rstudioapi::navigateToFile(file = file, line = line_no)
227 +
  })
228 +
  invisible(found_patterns)
229 +
}
230 +
192 231
#nocov end
193 232
194 233

@@ -1,7 +1,7 @@
Loading
1 1
#' Replace string pattern in text file
2 2
#' @param file_contents Character string containing a regular expression to be matched in the 
3 -
#' given character vector. Passed to \code{pattern} in \code{\link[base]{gsub}}.
4 -
#' @param replace The replacement, passed to \code{replacement} in \code{\link[base]{gsub}}.
3 +
#' given character vector. Passed to \code{pattern} in \code{\link[base:grep]{gsub}}.
4 +
#' @param replace The replacement, passed to \code{replacement} in \code{\link[base:grep]{gsub}}.
5 5
#' @param basedir The root of the directory tree in which files will be searched recursively.
6 6
#' @param dir_recursive (logical, default: \code{TRUE}) Search within subdirectories of \code{basedir}?
7 7
#' @param reader A function, akin to \code{base::readLines}, the default, that accepts a filename and returns a character vector.
Files Coverage
R 98.89%
Project Totals (51 files) 98.89%
ddihjga8672xdtb4
1lja34o298ygj9s6

No yaml found.

Create your codecov.yml to customize your Codecov experience

Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading