Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add experimental custom autocompletion #597

Merged
merged 51 commits into from
Feb 18, 2024
Merged

Add experimental custom autocompletion #597

merged 51 commits into from
Feb 18, 2024

Conversation

sorhawell
Copy link
Collaborator

@sorhawell sorhawell commented Dec 17, 2023

Close #580

  • implement custom Rstudio code completion
  • native R / radian code completion, to be as good as Rstudio completion or what possible.
  • support vscode-R completion via their webserver feature
  • add pl$polars_code_completion()

also to support completion:

  • added $columns()-method and names() for GroupBy and LazyGroupBy
  • improved "method not found" error message
  • added missing .DollarNames.RLazyGroupBy method
  • deprecated pl$extra_auto_completion() the old way to activate native R completion.

@sorhawell

This comment was marked as outdated.

@sorhawell
Copy link
Collaborator Author

completions for chains, icons shows if method / sub-namespace ("list")
image

... and assignment (columns<-)
image

Auto completions can be used to "tab-start" an expression from a column name
image

works for lazy too

image

@sorhawell
Copy link
Collaborator Author

sorhawell commented Dec 19, 2023

interactive syntax explaining via messages. In ipython-polars I miss this feature. Sometimes I tab-complete and nothing happens and I don't know why. Most often because some earlier error in expression.

image

alias is missing an argument
image

image

@sorhawell
Copy link
Collaborator Author

sorhawell commented Dec 19, 2023

@eitsupi @etiennebacher

It is possible to make very sweet Rstudio auto completion.
I would be possible to also add interactive help-docs or table views of DataFrame.

So far only data.table, dplyr and reticulate and maybe 3 other packages have their completions in the rstudio-source code. At some point we may apply to get our completions included. I guess it will be denied as we use full evaluation of the entire syntax which could slow down the IDE. I have a 3 seconds timeout to block any unintended eager evaluation. R timeout cannot block rust code, before returning to R.

Here I make use the completions methods can be wrapped with custom handlers and injected into the rstudio ide environment. In theory multiple packages could all wrap the same rstudio completion without conflicting.

This approach is using the private api of Rstudio-api, but judging from the blame history. This code part is quite stable on a yearly basis.

@sorhawell
Copy link
Collaborator Author

sorhawell commented Dec 19, 2023

to activate auto-completion run this

source("~/Documents/projs/r-polars/R/rstudio_completion.R")
.dev$activate_polars_rstudio_completion()
library(polars)

the weird .dev-prefix would not stay. It is just a convenient way to update completion functions without restarting session or building polars package to do faster "trial and error".

@sorhawell
Copy link
Collaborator Author

sorhawell commented Dec 20, 2023

We also have some reasonable code completion for native R terminals which could be further refined. In the past I was discouraged how bad the native R utils completion worked combined with Rstudio IDE. But that is no longer a problem with this PR. We can detect what IDE is being used.

@etiennebacher
Copy link
Collaborator

Love this, the examples look amazing! I'll try a bit more later.

@sorhawell sorhawell changed the title experiment with rstudio autocompletion pl$polars_code_completion() to improve native R and Rstudio completions Dec 22, 2023
@etiennebacher etiennebacher mentioned this pull request Dec 23, 2023
@etiennebacher
Copy link
Collaborator

I don't know how to test completion in "native R". I ran vanilla R in bash and in Powershell and couldn't get any completion (for any function, not just polars)

@etiennebacher etiennebacher marked this pull request as ready for review February 16, 2024 13:01
Copy link
Collaborator

@eitsupi eitsupi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

I am wondering if there is any worth in the option to enable/disable this.

R/autocompletion.R Outdated Show resolved Hide resolved
Merge branch 'main' into rstudio_autocomplete2

# Conflicts:
#	NEWS.md
@etiennebacher
Copy link
Collaborator

I am wondering if there is any worth in the option to enable/disable this.

I wanted to enable this automatically at first, but I'm not confident on how it will interact with other packages that might add their own custom completion (also @sorhawell wrote most of this, I'm not so familiar with the autocompletion internals). I'd rather keep this experimental and opt-in for now

@sorhawell
Copy link
Collaborator Author

sorhawell commented Feb 16, 2024

@eitsupi first time using task to update deps and build. That was a very out-of-the-box experience. Nice!

@etiennebacher
native R terminal has no drop down for multiple suggestions, and there is something annoying where it will
delete what alraady has been written. Right now it works best for the very final completion.

> library(polars)
> pl$code_completion()
Using code completion in 'native' mode.
> # writes this and tab:  pl$col()$ab
> pl$col()$abs() # completes s()
polars Expr: col("").abs()
> rc.status()$comps # use rc.status()$comps for unit testing
[1] "$abs()"
>

it works much better with the radian terminal

writes this and tab: pl$col()$a

image image

@sorhawell
Copy link
Collaborator Author

sorhawell commented Feb 16, 2024

I found this for unit testing of native-native + native-radian

> utils:::.assignToken("pl$col()$a")
> utils:::.completeToken()
> utils:::.retrieveCompletions()
 [1] "pl$col()$abs()"             "pl$col()$add()"             "pl$col()$agg_groups()"      "pl$col()$alias()"          
 [5] "pl$col()$all()"             "pl$col()$and()"             "pl$col()$any()"             "pl$col()$append()"         
 [9] "pl$col()$approx_n_unique()" "pl$col()$arccos()"          "pl$col()$arccosh()"         "pl$col()$arcsin()"         
[13] "pl$col()$arcsinh()"         "pl$col()$arctan()"          "pl$col()$arctanh()"         "pl$col()$arg_max()"        
[17] "pl$col()$arg_min()"         "pl$col()$arg_sort()"        "pl$col()$arg_unique()"      "pl$col()$argsort()"        
[21] "pl$col()$arr()"   

Merge branch 'main' into rstudio_autocomplete2

# Conflicts:
#	NEWS.md
R/autocompletion.R Outdated Show resolved Hide resolved
R/autocompletion.R Outdated Show resolved Hide resolved
@eitsupi eitsupi mentioned this pull request Feb 18, 2024
@eitsupi eitsupi added this to the 0.14 milestone Feb 18, 2024
@etiennebacher etiennebacher changed the title pl$polars_code_completion() to improve native R and Rstudio completions Add experimental custom autocompletion Feb 18, 2024
R/autocompletion.R Outdated Show resolved Hide resolved
Copy link
Collaborator

@eitsupi eitsupi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Greate, let's merge.

@eitsupi eitsupi merged commit c2ef53b into main Feb 18, 2024
18 checks passed
@eitsupi eitsupi deleted the rstudio_autocomplete2 branch February 18, 2024 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

better code completion with r-polars
3 participants