Home Explore Blog CI



neovim

13th chunk of `runtime/doc/lsp.txt`
a0ffc060ae19404c0acea3ef32af085bcc9eecd4d5f6d57e0000000100000fb9
                       *vim.lsp.buf_request_sync()*
buf_request_sync({bufnr}, {method}, {params}, {timeout_ms})
    Sends a request to all server and waits for the response of all of them.

    Calls |vim.lsp.buf_request_all()| but blocks Nvim while awaiting the
    result. Parameters are the same as |vim.lsp.buf_request_all()| but the
    result is different. Waits a maximum of {timeout_ms}.

    Attributes: ~
        Since: 0.5.0

    Parameters: ~
      • {bufnr}       (`integer`) Buffer handle, or 0 for current.
      • {method}      (`string`) LSP method name
      • {params}      (`table|(fun(client: vim.lsp.Client, bufnr: integer): table?)?`)
                      Parameters to send to the server. Can also be passed as
                      a function that returns the params table for cases where
                      parameters are specific to the client.
      • {timeout_ms}  (`integer?`, default: `1000`) Maximum time in
                      milliseconds to wait for a result.

    Return (multiple): ~
        (`table<integer, {error: lsp.ResponseError?, result: any}>?`) result
        Map of client_id:request_result.
        (`string?`) err On timeout, cancel, or error, `err` is a string
        describing the failure reason, and `result` is nil.

commands                                                    *vim.lsp.commands*
    Registry (a table) for client-side handlers, for custom server-commands
    that are not in the LSP specification.

    If an LSP response contains a command which is not found in
    `vim.lsp.commands`, the command will be executed via the LSP server using
    `workspace/executeCommand`.

    Each key in the table is a unique command name, and each value is a
    function which is called when an LSP action (code action, code lenses,
    …) triggers the command.
    • Argument 1 is the `Command`: >
      Command
      title: String
      command: String
      arguments?: any[]
<
    • Argument 2 is the |lsp-handler| `ctx`.

    Example: >lua
        vim.lsp.commands['java.action.generateToStringPrompt'] = function(_, ctx)
          require("jdtls.async").run(function()
            local _, result = request(ctx.bufnr, 'java/checkToStringStatus', ctx.params)
            local fields = ui.pick_many(result.fields, 'Include item in toString?', function(x)
              return string.format('%s: %s', x.name, x.type)
            end)
            local _, edit = request(ctx.bufnr, 'java/generateToString', { context = ctx.params; fields = fields; })
            vim.lsp.util.apply_workspace_edit(edit, offset_encoding)
          end)
        end
<

config({name}, {cfg})                                       *vim.lsp.config()*
    Sets the default configuration for an LSP client (or all clients if the
    special name "*" is used).

    Can also be accessed by table-indexing (`vim.lsp.config[…]`) to get the
    resolved config, or redefine the config (instead of "merging" with the
    config chain).

    Examples:
    • Add root markers for ALL clients: >lua
      vim.lsp.config('*', {
        root_markers = { '.git', '.hg' },
      })
<
    • Add capabilities to ALL clients: >lua
      vim.lsp.config('*', {
      capabilities = {
        textDocument = {
          semanticTokens = {
            multilineTokenSupport = true,
          }
        }
      }
    })
<
    • Add root markers and capabilities for "clangd": >lua
      vim.lsp.config('clangd', {
      root_markers = { '.clang-format', 'compile_commands.json' },
      capabilities = {
        textDocument = {
          completion = {
            completionItem = {
              snippetSupport = true,
            }
          }
        }
      }
    })
<
    • (Re-)define the "clangd" configuration (overrides the resolved chain): >lua
      vim.lsp.config.clangd = {
      cmd = {
        'clangd',
        '--clang-tidy',
        '--background-index',
        '--offset-encoding=utf-8',
      },
      root_markers = { '.clangd', 'compile_commands.json'

Title: vim.lsp.buf_request_sync() and Client-Side Command Handling
Summary
This section describes the `vim.lsp.buf_request_sync()` function, which sends a request to all LSP servers and waits for their responses, blocking Neovim in the process. It also covers the `vim.lsp.commands` registry, a table for client-side handlers of custom server commands not in the LSP specification. Additionally, the `vim.lsp.config()` function is introduced, which allows setting default configurations for LSP clients, including root markers and capabilities.