Skip to content
This repository has been archived by the owner on May 19, 2021. It is now read-only.

Extensions to R / RStudio's autocompletion system #52

Open
kevinushey opened this issue Apr 22, 2017 · 10 comments
Open

Extensions to R / RStudio's autocompletion system #52

kevinushey opened this issue Apr 22, 2017 · 10 comments

Comments

@kevinushey
Copy link

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.

@gaborcsardi
Copy link
Contributor

@kevinushey yeah, that would be nice.

@jimhester
Copy link
Contributor

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.

@hadley
Copy link
Member

hadley commented Apr 25, 2017

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

@kevinushey
Copy link
Author

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.

@jennybc
Copy link
Member

jennybc commented Apr 27, 2017

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

@gaborcsardi
Copy link
Contributor

@jennybc from your history.

@jimhester
Copy link
Contributor

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

@gaborcsardi
Copy link
Contributor

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

@jimhester
Copy link
Contributor

jimhester commented Apr 27, 2017

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.

@bedantaguru
Copy link

Check my comment on ropensci/unconf18#65 (comment)

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants