瀏覽代碼

Updates to selection processing in addin

- Use global option regexplain.addin.max_lines (default 100)
- Give hint on warning about option
- Always use unique lines in selection
tags/v0.2.0
Garrick Aden-Buie 8 年之前
父節點
當前提交
3083a5d3f2
共有 4 個檔案被更改,包括 44 行新增15 行删除
  1. +1
    -1
      R/regex_gadget.R
  2. +32
    -10
      R/rstudio_addins.R
  3. +8
    -2
      man/regexplain_addin.Rd
  4. +3
    -2
      man/regexplain_file.Rd

+ 1
- 1
R/regex_gadget.R 查看文件

@@ -721,7 +721,7 @@ regex_gadget <- function(
})
}

viewer <- shiny::paneViewer(minHeight = 1000)
viewer <- shiny::paneViewer(minHeight = 800)
runGadget(ui, server, viewer = viewer)
}


+ 32
- 10
R/rstudio_addins.R 查看文件

@@ -1,4 +1,12 @@
#' regexplain_addin
#' RegExplain Addin
#'
#' Addin for regexplain. If the selection does not contain multiple lines, the
#' addin tries to evaluate the selection and coerce the result to a character
#' string. Only unique lines are passed to the gadget to maximize screen space.
#' The max number of lines returned is set by the option
#' `regexplain.addin.max_lines`, with a default value of 100 lines.
#' If evaluating the selection does not produce a character vector without
#' errors, the original selection is returned.
#'
#' @keywords internal
regexplain_addin <- function() {
@@ -8,38 +16,52 @@ regexplain_addin <- function() {
# Get context text
ctx_text <- context$selection[[1]]$text

max_lines <- getOption("rexeplain.addin.max_lines", 100)

# If it is one line and evaluates to something, use that
# Otherwise treat as text
obj <- tryCatch({
if (grepl("\n", ctx_text)) {
ctx_text[1:min(length(ctx_text), 100)]
ctx_text[1:min(length(ctx_text), max_lines)]
} else {
x <- eval(parse(text = ctx_text))
if (inherits(x, "list")) x <- unlist(x)
x <- as.character(x)
if (length(x) == 1 && grepl("\n", x))
x <- strsplit(x, "\n")[[1]]
if (length(x) > 10) {
message(ctx_text, " gave ", length(x), " lines, limiting to first 10 unique lines.")
x <- unique(x)
if (length(x) > max_lines) {
message(ctx_text, " gave ", length(x), " lines, limiting to first ",
max_lines, " unique lines. Set ",
"options('regexplain.addin.max_lines') to a higher value to ",
"increase the number of lines.")
x <- unique(x)
x[1:min(length(x), 10)]
x[1:min(length(x), max_lines)]
} else x
}
},
error = function(e) {as.character(ctx_text[1:min(length(ctx_text), 100)])})
error = function(e) {as.character(ctx_text[1:min(length(ctx_text), max_lines)])})

regex_gadget(if (length(obj) && obj != "") obj)

}

#' regexplain file loader
#' Regexplain File Addin
#'
#' See [regexplain_addin]. Opens file chooser to pick file, reads lines, returns
#' first `regexplain.addin.max_lines` (default 100).
#'
#' @keywords internal
regexplain_file <- function() {
fname <- file.choose()
x <- readLines(fname)
if (length(x) > 100) {
message("There were ", format(length(x), big.mark = ","), " lines in ", fname, "\nUsing only first 100.")
x <- x[1:100]
max_lines <- getOption("rexeplain.addin.max_lines", 100)
if (length(x) > max_lines) {
message("There were ", format(length(x), big.mark = ","), " lines in ",
fname, "\nUsing only first ", max_lines, ".\n",
"Set options('regexplain.addin.max_lines') to a higher value to ",
"increase the number of lines.")
x <- x[1:max_lines]
}
regex_gadget(x, "RegEx")
}

+ 8
- 2
man/regexplain_addin.Rd 查看文件

@@ -2,11 +2,17 @@
% Please edit documentation in R/rstudio_addins.R
\name{regexplain_addin}
\alias{regexplain_addin}
\title{regexplain_addin}
\title{RegExplain Addin}
\usage{
regexplain_addin()
}
\description{
regexplain_addin
Addin for regexplain. If the selection does not contain multiple lines, the
addin tries to evaluate the selection and coerce the result to a character
string. Only unique lines are passed to the gadget to maximize screen space.
The max number of lines returned is set by the option
\code{regexplain.addin.max_lines}, with a default value of 100 lines.
If evaluating the selection does not produce a character vector without
errors, the original selection is returned.
}
\keyword{internal}

+ 3
- 2
man/regexplain_file.Rd 查看文件

@@ -2,11 +2,12 @@
% Please edit documentation in R/rstudio_addins.R
\name{regexplain_file}
\alias{regexplain_file}
\title{regexplain file loader}
\title{Regexplain File Addin}
\usage{
regexplain_file()
}
\description{
regexplain file loader
See \link{regexplain_addin}. Opens file chooser to pick file, reads lines, returns
first \code{regexplain.addin.max_lines} (default 100).
}
\keyword{internal}

Loading…
取消
儲存