posit-dev / positron

Positron, a next-generation data science IDE
Other
2.49k stars 77 forks source link

ark: support completion for ggplot2 aesthetics #522

Open kevinushey opened 1 year ago

kevinushey commented 1 year ago

Thomas Pedersen [2:22 AM] Would it be possible to get argument suggestions in a function that only takes ... based on the context it is called in?

Kevin Ushey [9:49 AM] Possibly… do you have a motivating example?

Thomas Pedersen [7:36 AM] For instance in ggplot2 with aes() where it would be nice to have auto completion of the aesthetics that are possible in the layer

I think an example of this code would be something like:

ggplot(mtcars) + geom_point(aes(|))

where the valid autocompletion results for that aes() call would be inferred based on the aesthetics accepted by geom_point().

DavisVaughan commented 10 months ago

This would be easier if ggplot2 would provide some kind of machine readable list of function -> supported aes, like roxygen2 does with its tags https://github.com/r-lib/roxygen2/blob/main/inst/roxygen2-tags.yml

teunbrand commented 2 months ago

Pardon for just barging into this issue. For building such a list, you can query the accepted arguments in aes() with the following function:

get_aes <- function(layer) {
  union(layer$geom$aesthetics(), layer$stat$aesthetics())
}
get_aes(ggplot2::geom_point())
#> [1] "x"      "y"      "shape"  "colour" "size"   "fill"   "alpha"  "stroke"
#> [9] "group"
get_aes(ggplot2::geom_polygon())
#> [1] "x"         "y"         "colour"    "fill"      "linewidth" "linetype" 
#> [7] "alpha"     "subgroup"  "group"

Created on 2024-07-12 with reprex v2.1.1

I'm not sure how well a static list would interface with ggplot2's extension system and it probably wouldn't catch any changes made using update_geom/stat_params() (but the latter is more of a technicality one could safely ignore in 99.9% of cases).

lionel- commented 2 months ago

I imagine we'll end up with a mix of static and dynamic annotations to instruct Ark about injected symbols.

kevinushey commented 2 months ago

I ended up doing a bunch of crazy nonsense in RStudio to make this work:

https://github.com/rstudio/rstudio/blob/490d31234ededa1261cdb5c52362c95a49c0086c/src/cpp/session/modules/SessionRCompletions.R#L2978-L3162

It'd be much nicer to have a straightforward solution that ark can use.