languageserver: An implementation of the Language Server Protocol for R

Gitter Build Status Github Action codecov CRAN_Status_Badge CRAN Downloads

languageserver is an implementation of the Microsoft’s Language Server Protocol for the language of R.

It is released on CRAN and can be easily installed by

install.packages("languageserver")

The development version of languageserver could be installed by running the following in R:

# install.packages("remotes")
remotes::install_github("REditorSupport/languageserver")

Language Clients

The following editors are supported by installing the corresponding extensions:

Services Implemented

languageserver is still under active development, the following services have been implemented:

Settings

languageserver exposes the following settings via LSP configuration.

settings default description
r.lsp.debug false increase verbosity for debug purpose
r.lsp.log_file null file to log debug messages, fallback to stderr if empty
r.lsp.diagnostics true enable file diagnostics via lintr
r.lsp.rich_documentation true rich documentation with enhanced markdown features
r.lsp.snippet_support true enable snippets in auto completion
r.lsp.max_completions 200 maximum number of completion items
r.lsp.lint_cache false toggle caching of lint results
r.lsp.server_capabilities {} override server capabilities defined in capabilities.R. See FAQ below.
r.lsp.link_file_size_limit 16384 maximum file size (in bytes) that supports document links

These settings could also specified in .Rprofile file via options(languageserver.<SETTING_NAME> = <VALUE>). For example,

options(languageserver.snippet_support = FALSE)

will turn off snippet support globally. LSP configuration settings are always overriden by options().

FAQ

Linters

With lintr v2.0.0, the linters can be specified by creating the .lintr file at the project or home directory. Details can be found at lintr documentation.

Customizing server capbabilities

Server capabilities are defined in capabilities.R. Users could override the capabilities by specifying the LSP configuration setting server_capabilities or options(languageserver.server_capabilities) in .Rprofile. For example, to turn off definitionProvider, one could either use LSP configuration

"r": {
    "lsp": {
        "server_capabilities": {
            "definitionProvider": false
        }
    }
}

or R options

options(
    languageserver.server_capabilities = list(
        definitionProvider = FALSE
    )
)

Customizing formatting style

The language server uses styler to perform code formatting. It uses styler::tidyverse_style(indent_by = options$tabSize) as the default style where options is the formatting options.

The formatting style can be customized by specifying languageserver.formatting_style option which is supposed to be a function that accepts an options argument mentioned above. You could consider to put the code in .Rprofile.

styler::tidyverse_style provides numerous arguments to customize the formatting behavior. For example, to make it only work at indention scope:

options(languageserver.formatting_style = function(options) {
    styler::tidyverse_style(scope = "indention", indent_by = options$tabSize)
})

To disable assignment operator fix (replacing = with <-):

options(languageserver.formatting_style = function(options) {
    style <- styler::tidyverse_style(indent_by = options$tabSize)
    style$token$force_assignment_op <- NULL
    style
})

To further customize the formatting style, please refer to Customizing styler.