languageserver icon indicating copy to clipboard operation
languageserver copied to clipboard

Hover code suggestion not work in box and R6 package

Open wdiao-zju opened this issue 3 years ago • 6 comments

Hover suggestion not work in box and R6 package, The example in box package as follows:

'R/run_diff.R' file includes many functions such as calculate_diff, plot_diff and so on. the codes as follows:

box::use(R/run_diff)
run_diff$calculate_diff(object, ...) 

When I hover my mouse on run_diff$calculate_diff(), no function pamameter suggestiong is shown. When I use source("R/run_diff.R"), it works well that the function pamameter suggestion is shown.

R6 and box bring new OOB framework that isimilar to python into R, it may be a trend in future in R.

wdiao-zju avatar Apr 20 '22 04:04 wdiao-zju

As the author of the ‘box’ package I’d be happy to assist with adding support for this; however, I currently don’t know enough about the ‘languageserver’ package to do this on my own.

klmr avatar Aug 08 '22 11:08 klmr

You could check https://github.com/REditorSupport/languageserver/blob/master/R/document.R#L228 to see how the parser hooks work.

randy3k avatar Aug 11 '22 17:08 randy3k

As the author of the ‘box’ package I’d be happy to assist with adding support for this; however, I currently don’t know enough about the ‘languageserver’ package to do this on my own.

@klmr I have did some reseach and I don't know if I am right:

  1. in languageserver package, there is a parser_hooks list allow user to do some customize parsing, the logic of implmenting box support should occur there. related lines. and there is an env in languageserver which is different from the R environment, env is where languageserver stores the metadata about the document allow for completion and hover support and so on. the importing of box should be reflected in env by update it.
  2. in box, module import are handled here.
  3. the import process of box can be considered (by languageserver) as assign a new environment to the module name, and attach exported objects (declared in box module), handle document parsing (roxygen documents in box module).
  4. so I think the implementation can be something like:

on box side, we need a "import list" describing what should be imported, and parse the roxygen comments with this to get hovering documentation support in languagerserver. the resulted import list and documents should be placed into the languageserver env functions signatures and documents by similar action like action$assign.

I was planning to implement this on my own but I overestimated my power, I can't even read box and languageserver code (but I really want this feature, modular R import is something I've been waiting for so looong). I don't know if this helps.

TTTPOB avatar Feb 11 '23 08:02 TTTPOB

@TTTPOB Thanks, this is a good analysis. On the ‘box’ side I will have to export some functions that return the parsed ‘use’ specification and load information (probably related to klmr/box#179). I’m still not entirely sure how to extract and assign the required definitions without actually loading the module. We might have to satisfy us with a heuristic that misses dynamically defined names inside a module.

What I’m missing in parse_hooks (and in general anywhere in ‘languageserver’) is the handling of source calls. Presumably these are handled somewhere, right? If I could use this as a template it would make adding an implementation for box::use much easier.

klmr avatar Feb 11 '23 11:02 klmr

@klmr to my knowledge r languageserver do not handle source calls... for example if you have a file structure like this, if you only opened main.r in vscode editor, you won't get name completion of the a_long_variable_name. image

instead, languageserver choose to link all opened files. that is to say, if you open main.r and fake_dir/fake_source.r at the same time, you will get completion from the other file (bidirectional), even without the source call. image image

TTTPOB avatar Feb 12 '23 03:02 TTTPOB