Skip to content

Commit

Permalink
fix issue #30
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoBosh committed Nov 8, 2024
1 parent 1d106e9 commit a629201
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
29 changes: 16 additions & 13 deletions R/RStudio.R
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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)
}
}

0 comments on commit a629201

Please sign in to comment.