Skip to content

Commit 511961e

Browse files
committed
help addin: also test help pages
1 parent 52856c9 commit 511961e

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

NAMESPACE

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ importFrom(miniUI,miniTabPanel)
1919
importFrom(miniUI,miniTabstripPanel)
2020
importFrom(miniUI,miniTitleBarButton)
2121
importFrom(miniUI,miniTitleBarCancelButton)
22-
importFrom(rstudioapi,getActiveDocumentContext)
22+
importFrom(rstudioapi,getSourceEditorContext)
2323
importFrom(rstudioapi,primary_selection)
2424
importFrom(rstudioapi,sendToConsole)
2525
importFrom(rstudioapi,showDialog)
@@ -44,4 +44,5 @@ importFrom(shiny,textInput)
4444
importFrom(shinyAce,aceEditor)
4545
importFrom(svMisc,assign_temp)
4646
importFrom(svMisc,get_temp)
47+
importFrom(utils,"?")
4748
importFrom(utils,methods)

R/addins.R

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ switch_repo_addin <- function()
3535
#' @importFrom methods findFunction
3636
#' @importFrom svMisc assign_temp get_temp
3737
#' @importFrom rstudioapi sendToConsole
38-
#' @importFrom utils methods
38+
#' @importFrom utils ? methods
3939
.sdd_help_addin <- function() {
4040

4141
get_help <- function(message) {
@@ -55,12 +55,12 @@ switch_repo_addin <- function()
5555
cur_sel <- ""
5656
# Default empty message for functions
5757
alt_fun_msg <- em(
58-
"Select the name of a known function in an editor",
58+
"Select the name of a function in an editor",
5959
"before calling this addin if you want help on it..."
6060
)
6161

6262
# Only look for a function if selection is short
63-
if (nchar(cur_sel) < 40) {
63+
if (nchar(cur_sel) < 120) {
6464
#If cur_sel is something like ns::fun, we already know ns
6565
if (grepl("::", cur_sel)) {
6666
fun <- sub("^([^:]+)(::)([^(]+)(.*)$", "\\3", cur_sel)
@@ -77,7 +77,11 @@ switch_repo_addin <- function()
7777
}
7878
alt_fun_msg <- ""
7979
} else {# Find the function and its namespace
80-
cur_fun <- sub("^([^(]+)(.*)$", "\\1", cur_sel)
80+
if (grepl("(", cur_sel, fixed = TRUE)) {
81+
cur_fun <- sub("^([^(]+)(.*)$", "\\1", cur_sel)
82+
} else {
83+
cur_fun <- sub("^([.a-zA-Z0-9_$]+)(.*)$", "\\1", cur_sel)
84+
}
8185
if (cur_fun == "") {
8286
fun_env_names <- character(0) # Nothing
8387
} else {
@@ -103,8 +107,16 @@ switch_repo_addin <- function()
103107
}
104108
l <- length(fun_env_names)
105109
if (!l) {# Function not found
106-
fun <- ""
107-
ns <- ""
110+
# Ask help system directly
111+
cmd <- parse(text = paste0("?", cur_fun))
112+
if (length(eval(cmd))) {# Help page found
113+
fun <- cur_fun
114+
ns <- "" # Unknown
115+
alt_fun_msg <- ""
116+
} else {# Not found
117+
fun <- ""
118+
ns <- ""
119+
}
108120
} else if (l == 1) { # || (l == 2 &&
109121
# Only one function with this name found
110122
fun <- cur_fun
@@ -312,14 +324,7 @@ switch_repo_addin <- function()
312324
message <- warning_message
313325
}
314326

315-
#context <- try(suppressMessages(get_help(message)), silent = TRUE)
316-
#if (inherits(context, "try-error")) {
317-
# stop("Error while invoking help: ", context)
318-
#} else if (is.null(context) || !length(context) || !is.list(context)) {
319-
# # User cancelled or nothing selected
320-
# return(invisible())
321-
#}
322-
context <- get_help(message)
327+
context <- suppressMessages(get_help(message))
323328
if (is.null(context) || !length(context) || !is.list(context)) {
324329
# User cancelled or nothing selected
325330
return(invisible())
@@ -404,10 +409,6 @@ switch_repo_addin <- function()
404409

405410
#' Get word or selection at cursor
406411
#'
407-
#' Uses the {rstudioapi} to get the word the cursor is on or active selection in
408-
#' the active document. This is useful for addins that want bind keys to trigger
409-
#' commands using the cursor context.
410-
#'
411412
#' This function defines a word as a possibly namespaced R symbol. So a cursor
412413
#' on the name of `pkg::var(foo)` will return 'pkg::var'. `$` is considered a
413414
#' separator.
@@ -417,9 +418,12 @@ switch_repo_addin <- function()
417418
#'
418419
#' @returns a character vector containing the current word at the cursor or
419420
#' primary selection
420-
#' @importFrom rstudioapi getActiveDocumentContext primary_selection
421+
#' @importFrom rstudioapi getSourceEditorContext primary_selection
421422
.get_word_or_selection <- function() {
422-
context <- rstudioapi::getActiveDocumentContext()
423+
# PhG: this work only in R scripts, but not Rmd files
424+
# -> getSourceEditorContext instead
425+
#context <- rstudioapi::getActiveDocumentContext()
426+
context <- rstudioapi::getSourceEditorContext()
423427
current_selection <- rstudioapi::primary_selection(context)
424428
if (!.is_zero_length_selection(current_selection)) {
425429
return(current_selection$text)

man/dot-get_word_or_selection.Rd

Lines changed: 2 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)