File: tab_complete.R

package info (click to toggle)
r-cran-v8 6.0.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 436 kB
  • sloc: javascript: 980; cpp: 424; sh: 23; makefile: 8
file content (27 lines) | stat: -rw-r--r-- 847 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#' @importFrom utils rc.options
tab_complete <- function(ct, buf){
  # Check if this is a completable expression
  if(!ct$validate(paste0(buf, "xx"))){
    return(character())
  }

  # Buffer ends with a dot
  if(substr(buf, nchar(buf), nchar(buf)) == "."){
    base <- substr(buf, 1, nchar(buf)-1)
    token <- ""
  } else {
    base <- head(strsplit(buf, ".", fixed=T)[[1]], -1)
    token <- tail(strsplit(buf, ".", fixed=T)[[1]], 1)
  }
  object <- paste(c("this", base), collapse=".")
  comps <- tryCatch({
    fields <- ct$call("Object.getOwnPropertyNames", JS(object))
    suggestions <- grep(paste0("^", token, "."), fields, value = TRUE)
    if(length(base) && length(suggestions)) {
      paste(paste(base, collapse="."), suggestions, sep = ".")
    } else {
      suggestions
    }
  }, error = function(e) character());
  sort(comps)
}