Closed jalvesaq closed 4 months ago
@PMassicotte, this pull request fixes https://github.com/R-nvim/cmp-r/issues/6 by removing the feature, as discussed on the issue page. So far, the only way of completing function arguments based on the class of the first argument would be to parse the documentation. But R itself displays complete and correct options for completion when we press TAB in the R Console. So there must be a way of doing it.
The solution seems to be in src/library/utils/R/completion.R
.
What feature it will remove exactly? Can you show an example?
Could we use something like attr(methods("print"), "info")
?
It is what I've described at https://github.com/R-nvim/cmp-r/issues/6#issuecomment-1984339786 :
Type these commands in the R Console (or radian):
library(tidyverse)
mt <- tibble(mtcars)
print(mt,
Then press <Tab>
and wait about 2 seconds for the completion options to be displayed. The arguments max_extra_cols
and max_footer_lines
(from the method print.tbl
) will be among the options.
If I understand, after this PR, max_extra_cols
won't show anymore?
This works for S3 and S4
Maybe we can merge this for now and explore attr(methods("print"), "info")
later.
As far as I remember, years ago, S3 methods were defined implicitly: any function with a dot in its name would be a candidate to be a method. For example, if you defined a function called print.factor
it would be used as the print method for objects of class factor
. The completion with cmp_r
was supposed to work for this old case, but even in this case, there is a bug. I will close this pull request, fix the bug, and, then, we can think about what to do next.
Ok cool we can look at this later.
Some pointer using the sloop
R package (https://sloop.r-lib.org/):
r$> library(sloop)
r$> s3_dispatch(print(tibble::tibble()))
print.tbl_df
=> print.tbl
* print.data.frame
* print.default
r$> s3_methods_class("tbl")
# A tibble: 2 × 4
generic class visible source
<chr> <chr> <lgl> <chr>
1 format tbl FALSE registered S3method
2 print tbl FALSE registered S3method
The command methods(print)
lists a lot of methods. Most of them have an *
at the end of their names and R.nvim
completion works only with the few mthods that don't have the *
. Example:
# The generic `print()` and the implicitly defined methods `print.factor()` and
# `print.table()` receive different arguments:
args(print)
args(print.factor)
args(print.table)
# R.nvim can correctly complete the arguments for `print.factor` and `print.table`:
fctr1 <- factor(c("a", "b", "a", "b", "a"))
fctr2 <- factor(c("c", "d", "d", "c", "d"))
tbl <- table(fctr1, fctr2)
print(fctr1, )
print(tbl, )
# But not of `print.lm*`
mdl <- lm(rnorm(100) ~ runif(100))
print(mdl, )
# If we do `\rh` over the word `print` in `print(mdl, )` R.nvim will open the
# documentation of the `print.lm*` method.
For the print(mdl, )
I am getting this:
Is this expected or should I get a different set of arguments?
Maybe print.lm
wasn't a good example because it doesn't have specific arguments.
print.summary.lm
is better: symbolic.cor
and signif.stars
should be among the arguments in the completion menu:
mdl <- lm(rnorm(100) ~ runif(100))
sm <- summary(mdl)
print(sm, )
Close https://github.com/R-nvim/cmp-r/issues/6