Skip to content

Completion only works for packages loaded by default #44

Closed

Description

@wngrtn

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.

Activity

  1. adouzzy commented on Jul 18, 2019

    @adouzzy

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

  2. andycraig commented on Jul 18, 2019

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

  3. izahn commented on Jul 26, 2019

    @izahn
    Contributor

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

  4. andycraig commented on Jul 27, 2019

    @andycraig
    Contributor

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

  5. izahn commented on Jul 27, 2019

    @izahn
    Contributor

    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

  6. randy3k commented on Jul 27, 2019

    @randy3k
    Member

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

  7. izahn commented on Jul 27, 2019

    @izahn
    Contributor

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

  8. izahn commented on Jul 27, 2019

    @izahn
    Contributor

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

  9. andycraig commented on Jul 28, 2019

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

  10. krassowski commented on Sep 8, 2019

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

  11. randy3k commented on Sep 9, 2019

    @randy3k
    Member

    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?

  12. krassowski commented on Sep 9, 2019

    @krassowski
    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
  13. andycraig commented on Sep 10, 2019

    @andycraig
    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?

  14. andycraig commented on Oct 3, 2019

    @andycraig
    Contributor

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

  15. renkun-ken commented on Feb 17, 2020

    @renkun-ken
    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).

  16. icecat2005 commented on May 18, 2021

    @icecat2005

    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.

  17. jkroes commented on Feb 21, 2022

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

  18. renkun-ken commented on Feb 22, 2022

    @renkun-ken
    Member

    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.

  19. GladioFeng commented on Jan 18, 2024

    @GladioFeng

    in vscode, there are some problems.

  20. chmzs commented on Nov 7, 2024

    @chmzs

    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

  21. renkun-ken commented on Nov 19, 2026

    @renkun-ken
    Member

    I checked completion over LSP on current master. An unsaved R file containing library(dplyr) offered left_join, and a fenced Quarto R cell containing library(stringr) offered str_squish. Closing. Feel free to reopen with the document and client trace if a package named in the document still gives no completions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions