Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completion only works for packages loaded by default #44

Open
wngrtn opened this issue Jul 16, 2019 · 20 comments
Open

Completion only works for packages loaded by default #44

wngrtn opened this issue Jul 16, 2019 · 20 comments

Comments

@wngrtn
Copy link

wngrtn commented Jul 16, 2019

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.

@adouzzy
Copy link

adouzzy commented Jul 18, 2019

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

@andycraig
Copy link
Contributor

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 #1 (comment)).

@izahn
Copy link
Contributor

izahn commented Jul 26, 2019

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

@andycraig
Copy link
Contributor

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

@izahn
Copy link
Contributor

izahn commented Jul 27, 2019

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

@randy3k
Copy link
Member

randy3k commented Jul 27, 2019

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

@izahn
Copy link
Contributor

izahn commented Jul 27, 2019

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

@izahn
Copy link
Contributor

izahn commented Jul 27, 2019

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

@andycraig
Copy link
Contributor

@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.

@krassowski
Copy link
Contributor

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.

@randy3k
Copy link
Member

randy3k commented Sep 9, 2019

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?

@krassowski
Copy link
Contributor

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

@andycraig
Copy link
Contributor

@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
Copy link
Contributor

Open issue on eglot for allowing customisation of the time before changes are sent to the language server: joaotavora/eglot#258

@renkun-ken
Copy link
Member

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).

@icecat2005
Copy link

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.

@jkroes
Copy link

jkroes commented Feb 21, 2022

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).

@renkun-ken
Copy link
Member

renkun-ken commented Feb 22, 2022

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.

@GladioFeng
Copy link

in vscode, there are some problems.

@chmzs
Copy link

chmzs commented Nov 7, 2024

When using quarto documentation, there is an issue with code autocompletion, which does not provide code suggestions for packages other than base r. The same code highlighting also shows only the function name of the base r

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests