Learn R Programming

⚠️There's a newer version (0.3.16) of this package.Take me there.

languageserver: An implementation of the Language Server Protocol for R

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("devtools")
devtools::install_github("REditorSupport/languageserver")

Language Clients

These editors are supported by installing the corresponding package.

  • VSCode: vscode-r-lsp

  • Atom: atom-ide-r

  • Sublime Text: R-IDE

  • Vim/NeoVim: LanguageClient-neovim with settings

    let g:LanguageClient_serverCommands = {
        \ 'r': ['R', '--slave', '-e', 'languageserver::run()'],
        \ }

    or, if you use coc.nvim, you can do one of two things:

    • Install coc-r-lsp with:

      :CocInstall coc-r-lsp
    • or install the languageserver package in R

      install.packages("languageserver")
      # or install the developement version
      # devtools::install_github("REditorSupport/languageserver")

      Then add the following to your Coc config:

      "languageserver": {
          "R": {
              "command": "/usr/bin/R",
              "args" : [ "--slave", "-e", "languageserver::run()"],
              "filetypes" : ["r"]
          }
      }
  • Emacs: lsp-mode

  • JupyterLab: jupyterlab-lsp

Services Implemented

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

Settings

languageserver exposes the following settings via workspace/didChangeConfiguration

{
    "r.lsp.debug": {
      "type": "boolean",
      "default": false,
      "description": "Debug R Language Server"
    },
    "r.lsp.diagnostics": {
      "type": "boolean",
      "default": true,
      "description": "Enable Diagnostics"
    }
}

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. The option languageserver.default_linters is now deprecated in favor of the .lintr approach.

Customizing server capbabilities

Server capabilities are defined in capabilities.R. Users could override the settings by specifiying languageserver.server_capabilities option in .Rprofile. For example, the following code will turn off definitionProvider,

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

Please only use this option to disable providers and do not enable any providers that have not been implemented. Changing any other entries may cause unexpected behaviors on the server.

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.

Using persistent cache for formatting by styler

With styler v1.3, the formatting of top-level expressions can be cached by R.cache, which significantly improves the formatting performance by skipping the expressions that are known in cache to be already formatted. By default, the cache only works within the current session.

To make it work across sessions, set the R option R.cache.rootPath or environment variable R_CACHE_ROOTPATH to an existent path. For more details, see styler caching.

Copy Link

Version

Install

install.packages('languageserver')

Monthly Downloads

15,797

Version

0.3.10

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Randy Lai

Last Published

April 20th, 2021

Functions in languageserver (0.3.10)

call_hierarchy_incoming_calls

callHierarchy/incomingCalls request handler
color_presentation_reply

The response to a textDocument/colorPresentation Request
did_open_text_document_params

Parameters for didOpen notifications
completion_item_resolve

completionItem/resolve request handler
Logger

A basic logger class
did_save_text_document_params

Parameters for didSave notifications
expr_range

Expression range in UTF-16 code units
extract_blocks

Extract the R code blocks of a Rmarkdown file
on_initialized

initialized handler
enclosed_by_quotes

check if a position is inside quotes
find_config

Find the lintr config file
Workspace

A data structure for a session workspace
Session

Single R Session for Session Pool
SessionPool

Session Pool with Many R Sessions
ResponseErrorMessage

Response Error Message class
Message

Base Message class
GlobalEnv

A class for storing global environment information
LanguageServer

The language server
document_folding_range_reply

Get all the folding ranges in the document
PackageNamespace

A class for storing package information
document_color_reply

The response to a textDocument/documentColor Request
Notification

Notification Message class
cancel_request

cancel request notification handler
call_hierarchy_outgoing_calls

callHierarchy/outgoingCalls request handler
code_lens_params

Parameters for code lens requests
document_symbol_reply

Get all the symbols in the document
check_scope

Check if a token is in a R code block in an Rmarkdown file
completion_reply

The response to a textDocument/completion request
document_on_type_formatting_params

Parameters for document on type formatting requests
document_link_resolve

documentLink/resolve request handler
completion_params

Parameters for completion requests
completion_item_resolve_reply

The response to a completionItem/resolve request
document_uri

A document URI
code_action_params

Parameters for code action requests
document_formatting_params

Parameters for document formatting requests
constant_completion

Complete language constants
document_highlight_reply

The response to a textDocument/documentHighlight Request
find_package

Find the root package folder
on_shutdown

shutdown request handler
process_is_detached

check if the current process becomes an orphan
position

A position in a text document
text_document_code_action

textDocument/codeAction request handler
text_document_code_lens

textDocument/codeLens request handler
text_document_did_open

textDocument/didOpen notification handler
text_document_document_symbol

textDocument/documentSymbol request handler
text_document_did_close

textDocument/didClose notification handler
find_unbalanced_bracket

Search backwards in a document content for a specific character
text_document_folding_range

textDocument/foldingRange request handler
code_point_from_unit

Determine code points given code units
code_lens_resolve

codeLens/resolve request handler
code_point_to_unit

Determine code units given code points
did_change_text_document_params

Parameters for didChange notifications
hover_reply

The response to a textDocument/hover Request
on_exit

exit notification handler
formatting_reply

Format a document
package_completion

Complete a package name
on_type_formatting_reply

Format on type
purl

Strip out all the non R blocks in a R markdown file
ncodeunit

Calculate character offset based on the protocol
merge_list

Merge two lists
languageserver-package

languageserver: Language Server Protocol
is_rmarkdown

Check if a file is an RMarkdown file
log_write

Write to log
location

A location inside a resource
document_range_formatting_params

Parameters for document range formatting requests
document_symbol_params

Parameters for document symbol requests
did_close_text_document_params

Parameters for didClose notifications
rename_params

Parameters for rename requests
on_initialize

initialize handler
range_formatting_reply

Format a part of a document
run

Run the R language server
Request

Request Message class
definition_reply

Get the location of a specified function definition
Response

Response Message class
stdin_read_char

read a character from stdin
reference_params

Parameters for reference requests
text_document_definition

textDocument/definition request handler
stdin_read_line

read a line from stdin
text_document_will_save

textDocument/willSave notification handler
text_document_will_save_wait_until

textDocument/willSaveWaitUntil notification handler
text_document_formatting

textDocument/formatting request handler
signature_reply

the response to a textDocument/signatureHelp Request
seq_safe

Safer version of seq which returns empty vector if b < a
text_document_document_link

textDocument/documentLink request handler
range

A range in a text document
text_document_document_highlight

textDocument/documentHighlight request handler
text_document_completion

textDocument/completion request handler
text_document_hover

textDocument/hover request handler
text_document_rename

textDocument/rename request handler
text_document_selection_range

textDocument/selectionRange request handler
text_document_color_presentation

textDocument/colorPresentation request handler
diagnose_file

Run diagnostic on a file
did_change_configuration_params

Parameters for workspace/didChangeConfiguration notifications
diagnostics

diagnostics
workspace_symbol

workspace/symbol request handler
document_link_params

Parameters for document link requests
style_text

Edit code style
workspace_symbol_reply

Get all the symbols in the workspace matching a query
text_document_position_params

A text document and a position inside that document
symbol_information

Non-hierarchical symbol information
text_document_range_formatting

textDocument/rangeFormatting request handler
to_string

Transform any object to a string
text_document_on_type_formatting

textDocument/onTypeFormatting request handler
text_document_did_save

textDocument/didSave notification handler
text_document_references

textDocument/references request handler
tryCatchStack

tryCatch with stack captured
text_document_document_color

textDocument/documentColor request handler
will_save_text_document_params

Parameters for willSave notifications
text_document_did_change

textDocument/didChange notification handler
text_document_implementation

textDocument/implementation request handler
parse_document

Parse a document
path_from_uri

Paths and uris
is_directory

Check if a path is a directory
sanitize_names

sanitize package objects names
selection_range_reply

The response to a textDocument/selectionRange Request
is_package

check if a path is a package folder
document_link_reply

The response to a textDocument/documentLink Request
text_edit

A textual edit applicable to a text document
text_document_linked_editing_range

textDocument/linkedEditingRange request handler
text_document_prepare_call_hierarchy

textDocument/prepareCallHierarchy request handler
workspace_completion

Complete any object in the workspace
throttle

throttle a function execution
workspace_did_change_workspace_folder_params

workspace/didChangeWorkspaceFolders notification handler
text_document_signature_help

textDocument/signatureHelp request handler
text_document_prepare_rename

textDocument/prepareRename request handler
text_document_type_definition

textDocument/typeDefinition request handler
workspace_execute_command

workspace/executeCommand request handler
workspace_did_change_watched_files

workspace/didChangeWatchedFiles notification handler
workspace_did_change_configuration

workspace/didChangeConfiguration notification handler
arg_completion

Complete a function argument