Skip to content

Commit a629201

Browse files
committed
fix issue #30
1 parent 1d106e9 commit a629201

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

NEWS.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# Rdpack 2.6.1.9000
2+
3+
- `RStudio_reprompt' now issues a more informative error message when not called on a
4+
suitable file or function. Fixes issue #30 by @daattali.
5+
6+
7+
18
# Rdpack 2.6.1
29

310
- R-devel c86938 turned to error the warning for a non-existing key in the bib

R/RStudio.R

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
11
# In RStudio, call reprompt on function that cursor is pointing to.
22

33
RStudio_reprompt <- function(verbose = TRUE) {
4-
5-
if (!requireNamespace("rstudioapi") || !requireNamespace("rprojroot"))
6-
stop("RStudio support requires you to install the 'rprojroot' and 'rstudioapi' packages")
7-
8-
sourceContext <- rstudioapi::getSourceEditorContext()
9-
infile <- sourceContext$path
10-
4+
5+
if (!requireNamespace("rstudioapi") || !requireNamespace("rprojroot"))
6+
stop("RStudio support requires you to install the 'rprojroot' and 'rstudioapi' packages")
7+
8+
sourceContext <- rstudioapi::getSourceEditorContext()
9+
infile <- sourceContext$path
10+
11+
if(length(infile) == 0)
12+
stop("Nothing to do. See ?Rdpack::RStudio_reprompt")
13+
1114
if (grepl("[.][rR]d$", infile)) # editing a help file
1215
reprompt(infile = infile, filename = infile, verbose = verbose)
13-
16+
1417
else if (grepl("[.][rRsSq]$", infile)) { # editing R source
1518
pkgdir <- rprojroot::find_package_root_file(path = dirname(infile))
1619
pkg <- basename(pkgdir)
17-
20+
1821
if (length(sourceContext$selection) == 1) {
1922
fnname <- sourceContext$selection[[1]]$text
2023
} else
2124
fnname <- ""
2225
if (!nchar(fnname))
2326
stop("Select a function name")
24-
27+
2528
if (!exists(fnname))
2629
stop("Object ", sQuote(fnname), " not found. Run 'Install and Restart'?")
27-
30+
2831
existing <- help(fnname)
2932
# Subset to the ones in the current package
3033
existing <- existing[basename(dirname(dirname(existing))) == pkg]
@@ -39,6 +42,6 @@ RStudio_reprompt <- function(verbose = TRUE) {
3942
stop("Multiple matches to ", sQuote(fnname), ". Open one help file manually.")
4043
} else
4144
stop("This tool only works on .Rd or .R files.")
42-
45+
4346
rstudioapi::navigateToFile(infile)
44-
}
47+
}

0 commit comments

Comments
 (0)