@@ -35,7 +35,7 @@ switch_repo_addin <- function()
35
35
# ' @importFrom methods findFunction
36
36
# ' @importFrom svMisc assign_temp get_temp
37
37
# ' @importFrom rstudioapi sendToConsole
38
- # ' @importFrom utils methods
38
+ # ' @importFrom utils ? methods
39
39
.sdd_help_addin <- function () {
40
40
41
41
get_help <- function (message ) {
@@ -55,12 +55,12 @@ switch_repo_addin <- function()
55
55
cur_sel <- " "
56
56
# Default empty message for functions
57
57
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" ,
59
59
" before calling this addin if you want help on it..."
60
60
)
61
61
62
62
# Only look for a function if selection is short
63
- if (nchar(cur_sel ) < 40 ) {
63
+ if (nchar(cur_sel ) < 120 ) {
64
64
# If cur_sel is something like ns::fun, we already know ns
65
65
if (grepl(" ::" , cur_sel )) {
66
66
fun <- sub(" ^([^:]+)(::)([^(]+)(.*)$" , " \\ 3" , cur_sel )
@@ -77,7 +77,11 @@ switch_repo_addin <- function()
77
77
}
78
78
alt_fun_msg <- " "
79
79
} 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
+ }
81
85
if (cur_fun == " " ) {
82
86
fun_env_names <- character (0 ) # Nothing
83
87
} else {
@@ -103,8 +107,16 @@ switch_repo_addin <- function()
103
107
}
104
108
l <- length(fun_env_names )
105
109
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
+ }
108
120
} else if (l == 1 ) { # || (l == 2 &&
109
121
# Only one function with this name found
110
122
fun <- cur_fun
@@ -312,14 +324,7 @@ switch_repo_addin <- function()
312
324
message <- warning_message
313
325
}
314
326
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 ))
323
328
if (is.null(context ) || ! length(context ) || ! is.list(context )) {
324
329
# User cancelled or nothing selected
325
330
return (invisible ())
@@ -404,10 +409,6 @@ switch_repo_addin <- function()
404
409
405
410
# ' Get word or selection at cursor
406
411
# '
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
- # '
411
412
# ' This function defines a word as a possibly namespaced R symbol. So a cursor
412
413
# ' on the name of `pkg::var(foo)` will return 'pkg::var'. `$` is considered a
413
414
# ' separator.
@@ -417,9 +418,12 @@ switch_repo_addin <- function()
417
418
# '
418
419
# ' @returns a character vector containing the current word at the cursor or
419
420
# ' primary selection
420
- # ' @importFrom rstudioapi getActiveDocumentContext primary_selection
421
+ # ' @importFrom rstudioapi getSourceEditorContext primary_selection
421
422
.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()
423
427
current_selection <- rstudioapi :: primary_selection(context )
424
428
if (! .is_zero_length_selection(current_selection )) {
425
429
return (current_selection $ text )
0 commit comments