Skip to content

An implementation of the Language Server Protocol for R

License

Notifications You must be signed in to change notification settings

WLOGSolutions/languageserver

 
 

Repository files navigation

languageserver: An implementation of the Language Server Protocol for R

CRAN_Status_Badge Build Status

languageserver is an implement 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

source("https://install-github.me/REditorSupport/languageserver")

Rmarkdown

The R package knitr is required to enable languageserver for Rmarkdown files. languageserver doesn't specify knitr as a dependency however, users may need to install it manually.

Language Clients

These editors are supported by installing the corresponding package.

let g:LanguageClient_serverCommands = {
    \ 'r': ['R', '--slave', '-e', 'languageserver::run()'],
    \ }
(lsp-register-client
    (make-lsp-client :new-connection
        (lsp-stdio-connection '("R" "--slave" "-e" "languageserver::run()"))
        :major-modes '(ess-r-mode inferior-ess-r-mode)
        :server-id 'lsp-R))

Services Implemented

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

  • textDocumentSync (diagnostics)
  • hoverProvider
  • completionProvider
  • signatureHelpProvider
  • definitionProvider
  • referencesProvider
  • documentHighlightProvider
  • documentSymbolProvider
  • workspaceSymbolProvider
  • codeActionProvider
  • codeLensProvider
  • documentFormattingProvider
  • documentRangeFormattingProvider
  • documentOnTypeFormattingProvider
  • renameProvider
  • documentLinkProvider
  • executeCommandProvider

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 master branch, the linters can be specified by creating the .lintr file at the project or home directory. Details can be found at lintr documentation.

Until a new version of lintr is released, you could consider the following settings in .Rprofile:

setHook(
    packageEvent("languageserver", "onLoad"),
    function(...) {
        options(languageserver.default_linters = lintr::with_defaults(
            line_length_linter = lintr::line_length_linter(100),
            object_usage_linter = NULL
        ))
    }
)

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

About

An implementation of the Language Server Protocol for R

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • R 93.1%
  • C 6.9%