languageserver

repository·master·Indexed 20 days ago

https://github.com/reditorsupport/languageserver

An implementation of the Microsoft Language Server Protocol (LSP) for the R programming language. It provides IDE features such as autocompletion, diagnostics, and code navigation. The package integrates with the lintr package for linting and the styler package for code formatting.

Tokens
2.8K
Snippets
9
Records
10
Agent score
22%

What's inside languageserver

  1. Configure language clients for editors

    master

    The languageserver supports various editors via LSP extensions. Below are specific configuration examples for NeoVim and Coc.nvim.

    # NeoVim LSP configuration
    vim.lsp.config['r_language_server'] = {
    	settings = {
    		filetypes = { "r", "rmd" },
    	},
    }
    vim.api.nvim_create_autocmd("FileType", {
    	pattern = { "r", "rmd" },
    	callback = function()
    		vim.lsp.start(vim.lsp.config["r_language_server"])
    	end,
    })
    # Coc.nvim configuration
    {
        "languageserver": {
            "R": {
                "command": "/usr/bin/R",
                "args" : [ "--no-echo", "-e", "languageserver::run()"],
                "filetypes" : ["r"]
            }
        }
    }
  2. Install the languageserver R package

    master

    The languageserver package can be installed from CRAN for the stable version, or from r-universe/GitHub for development builds. Before installing the R package, ensure your system has the necessary build dependencies installed via your package manager (apt, dnf, or apk).

    # Install stable version from CRAN
    install.packages("languageserver")
    
    # Install latest development build from r-universe
    install.packages("languageserver", repos = c(
        reditorsupport = "https://reditorsupport.r-universe.dev",
        getOption("repos")
    ))
    
    # Install latest development version from GitHub
    # requires 'remotes' package
    remotes::install_github("REditorSupport/languageserver")
  3. Customize code formatting style

    master

    The language server uses the styler package for code formatting. By default, it uses styler::tidyverse_style(indent_by = options$tabSize).

    To customize the formatting, set the languageserver.formatting_style R option to a function that accepts an options argument (the formatting options provided by the LSP). You can define this function in your .Rprofile.

    # Example: Limit formatting to the current indentation scope
    options(languageserver.formatting_style = function(options) {
        styler::tidyverse_style(scope = "indention", indent_by = options$tabSize)
    })
  4. Install system dependencies for languageserver

    master

    Depending on your operating system, you must install specific system libraries before installing the R package to ensure all features (like XML parsing or SSL support) work correctly.

    # On Debian, Ubuntu, etc.
    apt install --assume-yes --no-install-recommends build-essential libcurl4-openssl-dev libssl-dev libxml2-dev libuv1-dev r-base
    
    # On Fedora, Centos, etc.
    dnf install --assumeyes --setopt=install_weak_deps=False @development-tools libcurl-devel libxml2-devel openssl-devel libuv-devel R
    
    # On Alpine
    apk add --no-cache curl-dev g++ gcc libxml2-dev linux-headers make R R-dev
  5. Configure languageserver via .Rprofile

    master

    You can configure languageserver settings globally by using the options() function in your .Rprofile file. Note that LSP configuration settings provided by the editor are always overridden by these R options.

    # Example: Disable snippet support globally
    options(languageserver.snippet_support = FALSE)
  6. Configure linters via .lintr

    master
    The language server supports linters through the lintr package. Starting with lintr v2.0.0, you can specify which linters to use by creating a .lintr file in your project directory or your home directory. Refer to the lintr documentation for specific configuration details.
  7. Customize server capabilities

    master

    You can override the default LSP server capabilities (such as definitionProvider) using either your editor's LSP configuration settings or R options in your .Rprofile. This is useful if you want to disable specific features provided by the server.

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

    OR

    options(
        languageserver.server_capabilities = list(
            definitionProvider = FALSE
        )
    )
  8. Disable assignment operator fix in formatting

    master

    If you want to use styler for formatting but prevent it from automatically replacing = with <-, you can customize the languageserver.formatting_style function to remove the force_assignment_op token.

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

    master

    The languageserver implements a wide range of Language Server Protocol services for R, including diagnostics, completion, definition providers, formatting, and more. Note that executeCommandProvider is currently not implemented.

    [x] textDocumentSync
    [x] publishDiagnostics
    [x] hoverProvider
    [x] completionProvider
    [x] completionItemResolve
    [x] signatureHelpProvider
    [x] definitionProvider
    [x] referencesProvider
    [x] documentHighlightProvider
    [x] documentSymbolProvider
    [x] workspaceSymbolProvider
    [x] codeActionProvider
    [x] codeLensProvider
    [x] documentFormattingProvider
    [x] documentRangeFormattingProvider
    [x] documentOnTypeFormattingProvider
    [x] renameProvider
    [x] prepareRenameProvider
    [x] documentLinkProvider
    [x] documentLinkResolve
    [x] colorProvider
    [x] colorPresentation
    [x] foldingRangeProvider
    [x] selectionRangeProvider
    [x] prepareCallHierarchy
    [x] callHierarchyIncomingCalls
    [x] callHierarchyOutgoingCalls
    [x] prepareTypeHierarchy
    [x] typeHierarchySupertypes
    [x] typeHierarchySubtypes
    [x] semanticTokens
    [x] linkedEditingRange
    [ ] executeCommandProvider
    [x] inlineValueProvider
    [x] inlayHintProvider
  10. Reference: languageserver LSP settings

    master

    The following settings are exposed via LSP configuration. They can also be set in .Rprofile using options(languageserver.<SETTING_NAME> = <VALUE>).

    setting                          | 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.inlay_hints_minimum_arguments` | `2`     | minimum supplied arguments before parameter-name inlay hints are shown
    `r.lsp.inlay_hints_minimum_argument_length` | `2` | minimum argument-name length for an inlay hint, excluding an initial `.`
    `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.parse_delay`              | `0.15`  | seconds to debounce parsing after an edit
    `r.lsp.diagnostics_delay`         | `0.75`  | seconds to debounce diagnostics after the current parse
    `r.lsp.parse_cache_max_mb`       | `64`    | maximum memory used by cached document parse versions
    `r.lsp.diagnostics_cache_max_mb`  | `16`    | maximum memory used by cached diagnostics
    `r.lsp.server_capabilities`      | `{}`    | override server capabilities defined in capabilities.R
    `r.lsp.link_file_size_limit`     | 16384   | maximum file size (in bytes) that supports document links