Home

Awesome

cmp-nvim-r

Completion source for nvim-cmp using Nvim-R as backend.

Installation

Use a plugin manager such as vim-plug or packer.

Setup

Register the source for nvim-cmp:

require'cmp'.setup {
  sources = {
    { name = 'cmp_nvim_r' },
  }
}

Configuration

cmp-nvim-r

cmp-nvim-r has the following options:

Below is an example of how to set theses options:

require("cmp_nvim_r").setup({
  filetypes = {"r", "rmd", "quarto"},
  doc_width = 58
  quarto_intel = "~/Downloads/quarto-1.1.251/share/editor/tools/yaml/yaml-intelligence-resources.json"
  })

nvim-cmp

The source cmp_nvim_r does not require any special configuration of nvim-cmp to work, and people have different preferences and workflows. Anyway, I share below what worked best for me:

An example following the above suggestions:

cmp.setup({
    formatting = {
        fields = {'abbr', 'kind', 'menu'},
        format = lspkind.cmp_format({
            mode = 'symbol', -- show only symbol annotations
            maxwidth = 50, -- prevent the popup from showing more than provided characters
            ellipsis_char = '...', -- the truncated part when popup menu exceed maxwidth
            before = function(entry, item)
                local menu_icon = {
                    nvim_lsp = '',
                    vsnip = '',
                    path = '',
                    cmp_zotcite = 'z',
                    cmp_nvim_r = 'R'
                }
                item.menu = menu_icon[entry.source.name]
                return item
            end,
        })
    },
    sources = cmp.config.sources({
        { name = 'vsnip' },
        { name = 'cmp_zotcite' },
        { name = 'cmp_nvim_r' },
        { name = 'nvim_lsp' },
        { name = 'path', option = { trailing_slash = true } },
    }),
})

languageserver

Since cmp-nvim-r and the R package languageserver provide completions for the same code, and completions from the languageserver might be the first ones to be displayed by nvim-cmp, you may want to put this in your ~/.Rprofile:

# Disable completion from the language server
options(languageserver.server_capabilities =
        list(completionProvider = FALSE, completionItemResolve = FALSE))