emo icon indicating copy to clipboard operation
emo copied to clipboard

Register a emoji completions for :xyz: within quotes

Open jimhester opened this issue 6 years ago • 11 comments

This works on the R console screenshot 2017-08-23 11 14 16

And in the IDE screenshot 2017-08-23 11 26 43

The IDE seems to be putting backticks around the completions however, I guess trying to be helpful, but here it is actually not. @kevinushey is there anyway to opt out of the quoting?

jimhester avatar Aug 23 '17 15:08 jimhester

I did not know about completeme

@jimhester did you see this though: https://github.com/rstudio/rstudio/pull/1462

romainfrancois avatar Aug 23 '17 15:08 romainfrancois

I did, this should make some of that PR unnecessary.

jimhester avatar Aug 23 '17 15:08 jimhester

The backtracking happens here IIUC:

https://github.com/rstudio/rstudio/blob/a079f5e1a66267fddfc3dffd3ffb57da6a002e77/src/gwt/src/org/rstudio/studio/client/workbench/views/console/shell/assist/RCompletionManager.java#L2055

Changes to studio would still be needed for emoji completion in other contexts right ? comments, markdown text ...

romainfrancois avatar Aug 23 '17 15:08 romainfrancois

Pretty sure comments could be handled with this as well (they are not yet). rmarkdown text / commit messages would still need IDE changes.

jimhester avatar Aug 23 '17 16:08 jimhester

perhaps the results from the completer could embed some more information, so that in turn .rs.getCustomRCompletions would make a more appropriate object to send back to the rpc :

at the moment, we get:

> .rs.getCustomRCompletions( '":monkey')
$token
[1] ":monkey"

$results
[1] "\U0001f648 : see_no_evil_monkey"   "\U0001f649 : hear_no_evil_monkey"  "\U0001f64a : speak_no_evil_monkey" "\U0001f435 : monkey_face"          "\U0001f412 : monkey"              

$packages
[1] "" "" "" "" ""

$quote
[1] FALSE FALSE FALSE FALSE FALSE

$type
[1] 0 0 0 0 0

$fguess
[1] ""

$excludeOtherCompletions
[1] FALSE
attr(,"class")
[1] "rs.scalar"

$overrideInsertParens
[1] FALSE
attr(,"class")
[1] "rs.scalar"

$cacheable
[1] TRUE
attr(,"class")
[1] "rs.scalar"

$helpHandler
NULL

in particular type = 0, perhaps if this was another type rstudio would not backtick it.

.rs.addFunction("getCustomRCompletions", function(line)
{
   utils:::.assignLinebuffer(line)
   utils:::.assignEnd(nchar(line))
   token <- utils:::.guessTokenFromLine()
   utils:::.completeToken()
   results <- utils:::.retrieveCompletions()
   
   packages <- sub('^package:', '', .rs.which(results))
   
   # ensure spaces around =
   results <- sub("=$", " = ", results)
   
   choose = packages == '.GlobalEnv'
   results.sorted = c(results[choose], results[!choose])
   packages.sorted = c(packages[choose], packages[!choose])
   
   packages.sorted = sub('^\\.GlobalEnv$', '', packages.sorted)
   
   .rs.makeCompletions(
      token = token,
      results = results.sorted,
      packages = packages.sorted
   )
})

romainfrancois avatar Aug 23 '17 16:08 romainfrancois

The issue here is likely that RStudio expects that you're trying to complete an R identifier, and when it detects that it contains non-syntactic characters, it quotes it with backticks to make it a valid completion candidate. This makes sense in RStudio when you have e.g.

data <- list("_invalid" = 1)
data$<TAB>

So (as @romainfrancois out) we probably need some way for completeme to signal to RStudio what 'type' of completion a particular candidate is.

kevinushey avatar Aug 23 '17 16:08 kevinushey

Yes I think you are correct, we need to set the completion type for the IDE.

.rs.acCompletionTypes seems to hold the currently defined types.

jimhester avatar Aug 23 '17 16:08 jimhester

Right, although currently RStudio doesn't provide any mechanism for the custom completer to report the completion types, and that isn't really a public API (although we could make it public or at least just quietly allow its use behind the scenes)

kevinushey avatar Aug 23 '17 16:08 kevinushey

on the same note, would be cool to hand some more information to completeme, e.g these : https://github.com/rstudio/rstudio/blob/master/src/cpp/session/modules/SessionRCompletions.R#L1798

romainfrancois avatar Aug 23 '17 16:08 romainfrancois

I think we can probably just use the completion environment to transfer the information. This is how the terminal completion works, so if the IDE adds the additional session information to that environment the completers can then use it, and also read the completion type(s) from the environment as well.

jimhester avatar Aug 23 '17 16:08 jimhester

This is exciting.

we can actually gather the information from rstudio using (robustified 🙊) this:

calls <- sys.calls()
frames <- sys.frames() 

if( sys.calls()[[6]][[1]] == as.name( ".rs.getCustomRCompletions" ) ){
    functionCallString <- frames[[1]]$functionCallString
    # ....
}

I think I can use this to implement more meaty roxygen completions right now. But I guess having rstudio to actually fill the env with some extra information would be better/less hacky.

Similarly, completeme could hijack getCustomRCompletions

assign( ".rs.getCustomRCompletions", function(){ ... }, env = as.environment( "tools:rstudio") )

so that we can organize for functions registered by completeme to directly call .rs.makeCompletions to set the completion type ...

What would be awesome would be if completeme could directly emit some html to use on the popup. Some use cases on the top of my head

  • color completions, perhaps the popup would show the color.
  • customize the icon, this would be great for font awesome completions
  • we could show some mini cheat sheet. I'm thinking of ggplot2 for example.

romainfrancois avatar Aug 23 '17 17:08 romainfrancois