unconf17 icon indicating copy to clipboard operation
unconf17 copied to clipboard

Extensions to R / RStudio's autocompletion system

Open kevinushey opened this issue 7 years ago • 10 comments

It would be handy to allow packages to register their own autocompletion routines for certain functions. A simple example might be shiny::runExample() -- users might write

shiny::runExample(<TAB>)

The autocompletion system could then provide the set of available examples as autocompletion candidates.

kevinushey avatar Apr 22 '17 23:04 kevinushey

@kevinushey yeah, that would be nice.

gaborcsardi avatar Apr 23 '17 05:04 gaborcsardi

Definitely think this could be useful, I experimented a little with this for base R's completion at https://github.com/tidyverse/readxl/pull/320/files.

People completions independently won't work well because you can only define one custom completion function at a time. But a completion package package authors can use to register completion functions and will handle the fallbacks for you will work well.

We also need a way to hook into RStudio's completion machinery and provide helper functions to make registration of completion functions easier.

jimhester avatar Apr 24 '17 12:04 jimhester

Ideally we'd have some way to take advantage of the file autocomplete, but with some function that filter the list of files.

hadley avatar Apr 25 '17 19:04 hadley

Here's the infrastructure I'm imagining:

We could develop an R package (call it autocomp or something similar), and it would be responsible for maintaining a set of autocompletion functions, as registered by other users / packages.

Suppose we wanted to register an autocompleter for shiny::runExample(). We could do this with an interface like:

# imagine this living in a package's .onLoad or similar
autocomp::register_completer("shiny::runExample", "example", function(token) {
  examples <- list.files(system.file("examples", package = "shiny"))
  types <- rep(autocomp::completion_types$FILE, length(examples))
  autocomp::completions(examples, types)
})

The first argument is used to define the package + function name; the second the argument for which this completer should be registered, the third the actual completion function (which should return a set of completion results).

The 'token' object would simply be the R code lying before the user's cursor; we can think about what other context might need to be stuffed in there.

The R package could register its top-level own custom completer (ie, by defining rc.options("custom.completer"), to ensure that registered completers work in plain-old R sessions, while hosting environments (e.g. RStudio) could do its own work to override + use an autocomp-registered completion engine.

kevinushey avatar Apr 25 '17 19:04 kevinushey

Application numero uno: autocompleting username/repo in install_github() from some reasonable set of options.

jennybc avatar Apr 27 '17 17:04 jennybc

@jennybc from your history.

gaborcsardi avatar Apr 27 '17 17:04 gaborcsardi

Jenny, just use https://github.com/jimhester/autoinst :stuck_out_tongue_winking_eye:

jimhester avatar Apr 27 '17 17:04 jimhester

@jimhester it must be magical if it knows what I am about to install from GitHub. :)

gaborcsardi avatar Apr 27 '17 17:04 gaborcsardi

Well the lookup has to error first, but it uses http://rpkg.gepuro.net/ to figure out GitHub packages, which is clearly not perfect, but it works pretty well.

jimhester avatar Apr 27 '17 17:04 jimhester

Check my comment on https://github.com/ropensci/unconf18/issues/65#issuecomment-723088752

Keeping this in case anyone is interested in this matter.

Just a prototype which is doing following things

  • It can auto-complete future::plan
  • dplyr::filter unique cases (like iris %>% filter(Species == <TAB>))
  • match.arg type arguments in user defined or any functions (like cor(method = <TAB>))

Screenshot 2020-11-06 191109 Screenshot 2020-11-06 190940 Screenshot 2020-11-06 190906 Screenshot 2020-11-06 190749

It can be extended to any custom packages/ functions I guess.

bedantaguru avatar Nov 06 '20 13:11 bedantaguru