Skip to content

Language server

rainbow-fmt lsp is a Language Server Protocol server for document formatting, over standard input and output. Any editor with an LSP client can run it; the VS Code extension does.

What it does

  • initialize answers with documentFormattingProvider and full document synchronization (the client sends the whole text on each change).
  • textDocument/formatting formats the open document with the configuration that applies to its path, exactly as rainbow-fmt format would, verification included, and answers with one edit replacing the whole document — or no edit when nothing changes.
  • A document that cannot be formatted (a syntax error, a verification failure, an invalid configuration, a file type rainbow-fmt does not know) is answered with a JSON-RPC error carrying the same message the command line prints; editors show it.
  • shutdown then exit ends the server with exit code 0; an exit without shutdown, or the end of the input, with 1.

The server has no dependencies beyond rainbow-fmt. It does not watch configuration files: a changed rainbow.toml applies to the next format request, since the configuration is read per request.

Other editors

Point the client at rainbow-fmt lsp for the file types the packs format (json, jsonc, css, python, javascript, typescript, html, svelte). For example, in Neovim with nvim-lspconfig:

vim.lsp.config("rainbow_fmt", {
    cmd = { "rainbow-fmt", "lsp" },
    filetypes = { "json", "jsonc", "css", "python", "javascript", "typescript", "html", "svelte" },
    root_markers = { "rainbow.toml", "pyproject.toml", "package.json", ".git" },
})
vim.lsp.enable("rainbow_fmt")

Then vim.lsp.buf.format() formats the buffer.