languageserver
languageserver copied to clipboard
Completion only works for packages loaded by default
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.
It works for dplyr::left_join. You may also consider add library(dplyr) to your .Rprofile
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).
I also find that objects exported by packages attached via library(<pgkname>) are not available for completion. I'm using eglot in Emacs.
@wngrtn @izahn Can you provide steps that can be followed to reproduce this problem?
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
- Start Emacs with
emacs -q - Load packages and set up
eglotwith
(package-initialize)
(require 'eglot)
(require 'ess-site)
(add-hook 'ess-r-mode-hook 'eglot-ensure)
- Open an R file, write
library(stringr)at the top, followed bystrand pressC-M-ito trigger Emacscompletion-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.


You need to save the file first to allow the language server to "load" the specific packages.
Saving the file doesn't make any difference; the behavior is still exactly as reported above.
Oh wait, it does work if I write str and then save before requesting completions.
@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.
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.
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?
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 whileggplot2::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"
- lintr wrongly reports
- 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 overgeom_bar()afterlibrary(ggplot2)will not.
- hover over
- signatures work with the same caveats:
- no signature:

- signature:

- no signature:
@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?
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
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).
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.
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).
With Emacs, lsp-mode, and company, only some base package functions have completable arguments. E.g.,
abbreviateworks, but the variants ofseqdo 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.
in vscode, there are some problems.