The workspace server started by run() uses lintr for diagnostics and styler
for formatting by default. You can also use the Jarl and Air executables,
supply a custom R backend, or change the styler style in the server's R profile. The settings reference covers diagnostics_backend, formatting_backend,
and backend_options. Choose a built-in backend
With run_embedded(), a host pulls syntax diagnostics, can check infix spacing with
lintr, and can format a whole document with styler. See the
embedded host guide.
Change how styler formats a selection
The default formatting style is styler::tidyverse_style(indent_by = options$tabSize).
To format spacing and indentation while preserving assignment operators, give the server
a function that accepts the editor's formatting options and returns a styler style.
options(languageserver.formatting_style = function(options) {
styler::tidyverse_style(scope = "indention", indent_by = options$tabSize)
})To keep = assignments while applying the other default transformations, use this
function instead.
options(languageserver.formatting_style = function(options) {
style <- styler::tidyverse_style(indent_by = options$tabSize)
style$token$force_assignment_op <- NULL
style
})Restart the server to pick up the profile, then choose Format Selection or Format Document. Styler customization
Supply an R linter
Pass a descriptor to the languageserver.diagnostics_backend R option. This example
reports a warning on the first tab of any line that begins with a tab.
options(languageserver.diagnostics_backend = list(
id = "leading-tab",
document_types = "r",
run = function(request) {
lines <- which(startsWith(request$content, "\t"))
lapply(lines, function(line) {
list(
range = list(
start = list(line = line - 1L, character = 0L),
end = list(line = line - 1L, character = 1L)
),
severity = 2L,
message = "Indent with spaces."
)
})
}
))After a restart, type a literal tab at the beginning of a line in an R file. The
diagnostic source will be leading-tab. To return to lintr, remove the option from the
profile and restart.
Adapter contract
idUnique, stable name. The server sets this as the source of each diagnostic.
document_typesOne or both of
"r"and"literate".runFunction taking a request and returning a list of LSP diagnostics or text edits. It runs in a worker and must be serializable.
modesRequired for a formatting adapter set through
languageserver.formatting_backend. Select from"document","range","ranges", and"on_type".needs_workspace_globalsOptional. Set to
TRUEfor a diagnostics adapter that needsrequest$globals, including package imports and static source dependencies.config_filesOptional configuration filenames to watch in ancestor directories. Absolute paths are also accepted.
packageandversionOptional R package dependency and its minimum version. Supply both when the adapter requires a particular package.
executableOptional name of a required command-line executable. Its location can be set in
languageserver.backend_options.
Read the current buffer from request$content, a character vector with one element per
line. Requests also contain the document URI, original and resolved paths, language,
workspace root, and working directory. A formatting request additionally includes its
mode and editor options. Incoming range, ranges, and on-type point use zero-based R
code-point coordinates (row and col). Return LSP ranges using zero-based line and
UTF-16 character offsets, and include newText for each formatting edit. The server
handles publication and replies.
Use R options for descriptors containing functions. Workspace JSON configuration can select the built-in backends by name. Adapter implementation and Adapter examples in the test suite