languageserver
languageserver copied to clipboard
Auto-completion for R6 object
Thanks for this amazing package. It works brilliant for Vim on Linux. It seems like that auto-completion on R6 object methods is not supported. Is there any plan to add support for R6 object? Thanks!
It is not trivial. The easy part is to parse an R6 class and inspect its methods. The more tricky part is to deduce if a variable is an R6 object or not. It’s not always as easy as
var <- MyClass$new()
In particular, a variable could be assigned to any thing dynamically. With only lexical analysis, it’s not 100% guaranteed that the inspected result is correct (or even possible). An simple example could be
var <- if (condition) MyClass$new() else AnotherClass$new()
Not to mention that the variable could be a result of a complicated chain of actions.
One resolution that I am thinking is manual explicit annotations via roxygen
@param x MyClass
@return AnotherClass
foo <- function(x) {
AnotherClass$new(x)
}
Of course, this method also comes with its problems.
Thanks for the explanation.
Sorry I did not know the internal implementation of auto-completion. I am not sure if current implementation is totally different from what RStudio does. Probably I am underestimate the difficulty.
What you are pointing to is related to console auto completion. In a console, we know exactly if an object is R6 or not. However, we are working with editors and we don’t actually run the code. All the analysis are done lexically which makes the job difficult.
Oh I see where the difficulty comes from. This is probably a restriction when working on lsp. Anyway, still want to thank you for this amazing work and hope this feature can be implemented in the future.
One resolution that I am thinking is manual explicit annotations via roxygen
@param x MyClass @return AnotherClass foo <- function(x) { AnotherClass$new(x) }
Just see that roxygen2 now supports more intuitive documentation via https://github.com/r-lib/roxygen2/pull/922. Will it help to implement R6 autocomplete?
Yes, every extra bit of information will help.
@hongyuanjia If you are using VSCode, https://github.com/Ikuyadeu/vscode-R/pull/165 is my initial attempt for session symbol completion which is able to complete list, environments, S4 objects. R6 objects are basically environments so it works with R6 too.
If you are interested, you may have a try with my cutting edge testing build of vscode-R at https://github.com/renkun-ken/vscode-R/releases, which tracks my test branch with all my PRs at https://github.com/Ikuyadeu/vscode-R/pulls merged.
@renkun-ken thanks for the info! I will test if it works with current vscode-R release and yours and post here the results.
@renkun-ken unfortunately, neither current vscode-R release and your cutting-edge version can do the completion for my use case.
Here is a reproducible example.
library(eplusr)
path_idf <- system.file("extdata/1ZoneUncontrolled.idf", package = "eplusr")
idf <- read_idf(path_idf, use_idd("auto"))
#> IDD v8.8.0 has not been parsed before.
#> Try to locate `Energy+.idd` in EnergyPlus v8.8.0 installation folder 'C:/EnergyPlusV8-8-0'.
#> IDD file found: 'C:\EnergyPlusV8-8-0\Energy+.idd'.
#> Start parsing...
#> Parsing completed.
class(idf)
#> [1] "Idf" "R6"
names(idf)
#> [1] ".__enclos_env__" "clone" "print"
#> [4] "run" "save" "is_unsaved"
#> [7] "to_table" "string" "to_string"
#> [10] "is_valid" "validate" "replace_value"
#> [13] "search_value" "del_object" "ins_object"
#> [16] "set_object" "add_object" "dup_object"
#> [19] "paste" "update" "load"
#> [22] "insert" "rename" "del"
#> [25] "set" "add" "dup"
#> [28] "search_object" "object_in_class" "objects_in_relation"
#> [31] "object_relation" "objects_in_group" "objects_in_class"
#> [34] "objects" "object_unique" "object"
#> [37] "object_num" "is_valid_name" "is_valid_id"
#> [40] "object_name" "object_id" "definition"
#> [43] "is_valid_class" "is_valid_group" "class_name"
#> [46] "group_name" "path" "version"
#> [49] "initialize"
Created on 2020-01-01 by the reprex package (v0.3.0)
In vscode, when I type idf$, nothing from the names pops up.
@hongyuanjia Thanks for testing. Do you mind commenting at https://github.com/Ikuyadeu/vscode-R/pull/165, which is more relevant.
@renkun-ken Sure thing
This package only does static analysis at the moment and is not intended to provide any information related to a user's live session. However, we could still provide completion for classes defined in packages because their structures are known. It's a special case of #158. In this case, it is more of auto-completion for R6 classes or instances defined in packages than for R6 objects (or instances) living in a user session.