11# In RStudio, call reprompt on function that cursor is pointing to.
22
33RStudio_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