Open jimhester opened 7 years ago
I did not know about completeme
@jimhester did you see this though: https://github.com/rstudio/rstudio/pull/1462
I did, this should make some of that PR unnecessary.
The backtracking happens here IIUC:
Changes to studio would still be needed for emoji completion in other contexts right ? comments, markdown text ...
Pretty sure comments could be handled with this as well (they are not yet). rmarkdown text / commit messages would still need IDE changes.
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
)
})
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.
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.
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)
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
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.
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
This works on the R console
And in the IDE
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?