diff --git a/NEWS.md b/NEWS.md index 18f0271..68a355f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,10 @@ +# Rdpack 2.6.1.9000 + +- `RStudio_reprompt' now issues a more informative error message when not called on a + suitable file or function. Fixes issue #30 by @daattali. + + + # Rdpack 2.6.1 - R-devel c86938 turned to error the warning for a non-existing key in the bib diff --git a/R/RStudio.R b/R/RStudio.R index a491717..51a3352 100644 --- a/R/RStudio.R +++ b/R/RStudio.R @@ -1,30 +1,33 @@ # In RStudio, call reprompt on function that cursor is pointing to. RStudio_reprompt <- function(verbose = TRUE) { - - if (!requireNamespace("rstudioapi") || !requireNamespace("rprojroot")) - stop("RStudio support requires you to install the 'rprojroot' and 'rstudioapi' packages") - - sourceContext <- rstudioapi::getSourceEditorContext() - infile <- sourceContext$path - + + if (!requireNamespace("rstudioapi") || !requireNamespace("rprojroot")) + stop("RStudio support requires you to install the 'rprojroot' and 'rstudioapi' packages") + + sourceContext <- rstudioapi::getSourceEditorContext() + infile <- sourceContext$path + + if(length(infile) == 0) + stop("Nothing to do. See ?Rdpack::RStudio_reprompt") + if (grepl("[.][rR]d$", infile)) # editing a help file reprompt(infile = infile, filename = infile, verbose = verbose) - + else if (grepl("[.][rRsSq]$", infile)) { # editing R source pkgdir <- rprojroot::find_package_root_file(path = dirname(infile)) pkg <- basename(pkgdir) - + if (length(sourceContext$selection) == 1) { fnname <- sourceContext$selection[[1]]$text } else fnname <- "" if (!nchar(fnname)) stop("Select a function name") - + if (!exists(fnname)) stop("Object ", sQuote(fnname), " not found. Run 'Install and Restart'?") - + existing <- help(fnname) # Subset to the ones in the current package existing <- existing[basename(dirname(dirname(existing))) == pkg] @@ -39,6 +42,6 @@ RStudio_reprompt <- function(verbose = TRUE) { stop("Multiple matches to ", sQuote(fnname), ". Open one help file manually.") } else stop("This tool only works on .Rd or .R files.") - + rstudioapi::navigateToFile(infile) -} \ No newline at end of file +}