languageserver icon indicating copy to clipboard operation
languageserver copied to clipboard

Completion only works for packages loaded by default

Open wngrtn opened this issue 5 years ago • 19 comments

I use languageserver via coc-r-lsp and in combination with nvim-r. Completion works fine for packages that R loads at startup: base, utils, ...

I expected getting completion for the functions provided by a package after loading it, e.g. being offered left_join as a completion option after running library(dplyr), but that doesn't work.

wngrtn avatar Jul 16 '19 19:07 wngrtn

It works for dplyr::left_join. You may also consider add library(dplyr) to your .Rprofile

adouzzy avatar Jul 18 '19 07:07 adouzzy

Hi @wngrtn, the language server should know about left_join if the text library(dplyr) appears in an R document that is opened or saved, so could you try that? If you run library(dplyr) in the R console, for example, the language server won't know about it (see https://github.com/REditorSupport/languageserver/issues/1#issuecomment-329336950).

andycraig avatar Jul 18 '19 12:07 andycraig

I also find that objects exported by packages attached via library(<pgkname>) are not available for completion. I'm using eglot in Emacs.

izahn avatar Jul 26 '19 12:07 izahn

@wngrtn @izahn Can you provide steps that can be followed to reproduce this problem?

andycraig avatar Jul 27 '19 00:07 andycraig

I don't do anything special.

Software Emacs 26.2, latest release of languageserver from CRAN, latest release of eglot from elpa and ess from melpa.

Reproduction steps

  1. Start Emacs with emacs -q
  2. Load packages and set up eglot with
(package-initialize)
(require 'eglot)
(require 'ess-site)
(add-hook 'ess-r-mode-hook 'eglot-ensure)
  1. Open an R file, write library(stringr) at the top, followed by str and press C-M-i to trigger Emacs completion-at-point.

Expected result I should see a bunch of functions starting with str_ exported from the stringr package.

Actual result I only see completion suggestions from base utils and graphics packages, nothing from the stringr package. See screen shots below.

lsp1

lsp2

izahn avatar Jul 27 '19 00:07 izahn

You need to save the file first to allow the language server to "load" the specific packages.

randy3k avatar Jul 27 '19 01:07 randy3k

Saving the file doesn't make any difference; the behavior is still exactly as reported above.

izahn avatar Jul 27 '19 03:07 izahn

Oh wait, it does work if I write str and then save before requesting completions.

izahn avatar Jul 27 '19 03:07 izahn

@izahn Thank you for the nice reproducible example!

The equivalent process in VSCode works without the second save. That is: open an R file, write library(stringr) at the top, save, write str, don't save, see suggestions starting with str_ from the stringr library. Perhaps this is somehow emacs-specific.

andycraig avatar Jul 28 '19 08:07 andycraig

This does not work for me either in JupyterLab-lsp. I guess that this is because the notebooks as presented to the LSP server only exist as "virtual" documents (sent with with textDocumentSync) rather than actual files on the disk.

krassowski avatar Sep 08 '19 15:09 krassowski

Currently, some features such as completion and caching user functions are only available via textDocument/didOpen/textDocument/didSave but not textDocument/didChange. The reason for implementing such restriction is to avoid language server being too slow. Perhaps we will need to think again if it is a reasonable tradeoff.

@krassowski Could you comment what are working and what are not working in JupyterLab-lsp?

randy3k avatar Sep 09 '19 06:09 randy3k

Thanks for the quick replay! Everything works except for imported symbols, and variables & functions defined within the notebook/file.

In particular:

  • completion works fine except for the external libraries and symbls from within the file, e.g.:
    • library(ggplot2); geom_<tab> does not work while ggplot2::geom_<tab> does
  • inspections work fine except for the imported functions and functions defined within the file/notebook:
    • lintr wrongly reports no visible global function definition "xxx"
  • hover and ranges work fine, again except for symbols imported or defined within the file:
    • hover over ggplot2::geom_bar() will show the documentation while hover over geom_bar() after library(ggplot2) will not.
  • signatures work with the same caveats:
    • no signature: Screenshot from 2019-09-09 13-23-54
    • signature: Screenshot from 2019-09-09 13-24-17

krassowski avatar Sep 09 '19 12:09 krassowski

@randy3k @izahn I think this might be an eglot issue. The README says that eglot ‘Sends textDocument/didChange for groups of edits, not one per each tiny change’. This suggests that when a completion is requested, a didChange may not have been sent and so the language server might not know what characters are at the cursor. That the completion works when the file is saved would seem to support this.

@izahn eglot mentions this in the section contesting it with lsp-model.el, so it might be worth seeing if completions work with that?

andycraig avatar Sep 10 '19 03:09 andycraig

Open issue on eglot for allowing customisation of the time before changes are sent to the language server: https://github.com/joaotavora/eglot/issues/258

andycraig avatar Oct 03 '19 23:10 andycraig

Does this problem still persist?

We've made didOpen, didChange, and didClose all trigger parse and package resolving (#204, #205) if needed, and we also use the text (if exists) from the requests before reading from file (#197, #198).

renkun-ken avatar Feb 17 '20 09:02 renkun-ken

I have the same problem using languageserver on Sublime Text 3. I only get suggested completions for base and utils packages. I can however get the completions if I start by the package name. E.g. tidyverse::select()

The issue is not resolved by saving the file after typing the library function.

icecat2005 avatar May 18 '21 02:05 icecat2005

With Emacs, lsp-mode, and company, only some base package functions have completable arguments. E.g., abbreviate works but the variants of seq do not (e.g., seq.int).

jkroes avatar Feb 21 '22 20:02 jkroes

With Emacs, lsp-mode, and company, only some base package functions have completable arguments. E.g., abbreviate works, but the variants of seq do not (e.g., seq.int).

We use formals() to get function arguments for completion. Unfortunately, formals() returns NULL with primitive functions like seq.int and sum. Resolved via #526.

renkun-ken avatar Feb 22 '22 01:02 renkun-ken

in vscode, there are some problems.

GladioFeng avatar Jan 18 '24 05:01 GladioFeng