Home Explore Blog CI



neovim

9th chunk of `runtime/doc/lsp.txt`
2d060396903515a608bfe73484c893fa82295421cd9680830000000100000fa0
 variables and member fields (constants)
@lsp.mod.static          Class members (static members)

==============================================================================
EVENTS                                                            *lsp-events*

LspAttach                                                          *LspAttach*
    After an LSP client performs "initialize" and attaches to a buffer. The
    |autocmd-pattern| is the buffer name. The client ID is passed in the
    Lua handler |event-data| argument.

    Example: >lua
    vim.api.nvim_create_autocmd('LspAttach', {
      callback = function(ev)
        local client = vim.lsp.get_client_by_id(ev.data.client_id)
        -- ...
      end
    })
<
    Note: If the LSP server performs dynamic registration, capabilities may be
    registered any time _after_ LspAttach. In that case you may want to handle
    the "registerCapability" event.

    Example: >lua
    vim.lsp.handlers['client/registerCapability'] = (function(overridden)
      return function(err, res, ctx)
        local result = overridden(err, res, ctx)
        local client = vim.lsp.get_client_by_id(ctx.client_id)
        if not client then
          return
        end
        for bufnr, _ in pairs(client.attached_buffers) do
          -- Call your custom on_attach logic...
          -- my_on_attach(client, bufnr)
        end
        return result
      end
    end)(vim.lsp.handlers['client/registerCapability'])

LspDetach                                                          *LspDetach*
    Just before an LSP client detaches from a buffer. The |autocmd-pattern| is
    the buffer name. The client ID is passed in the Lua handler |event-data|
    argument.

    Example: >lua
    vim.api.nvim_create_autocmd('LspDetach', {
      callback = function(args)
        -- Get the detaching client
        local client = vim.lsp.get_client_by_id(args.data.client_id)

        -- Remove the autocommand to format the buffer on save, if it exists
        if client:supports_method('textDocument/formatting') then
          vim.api.nvim_clear_autocmds({
            event = 'BufWritePre',
            buffer = args.buf,
          })
        end
      end,
    })
<

LspNotify                                                          *LspNotify*
    This event is triggered after each successful notification sent to an
    LSP server.

    The client_id, LSP method, and parameters are sent in the Lua handler
    |event-data| table argument.

    Example: >lua
    vim.api.nvim_create_autocmd('LspNotify', {
      callback = function(args)
        local bufnr = args.buf
        local client_id = args.data.client_id
        local method = args.data.method
        local params = args.data.params

        -- do something with the notification
        if method == 'textDocument/...' then
          update_buffer(bufnr)
        end
      end,
    })
<

LspProgress                                                       *LspProgress*
    Upon receipt of a progress notification from the server. Notifications can
    be polled from a `progress` ring buffer of a |vim.lsp.Client| or use
    |vim.lsp.status()| to get an aggregate message.

    If the server sends a "work done progress", the `pattern` is set to `kind`
    (one of `begin`, `report` or `end`).

    The Lua handler |event-data| argument has `client_id` and `params`
    properties, where `params` is the request params sent by the server (see
    `lsp.ProgressParams`).

    Example: >vim
        autocmd LspProgress * redrawstatus
<

LspRequest                                                        *LspRequest*
    For each request sent to an LSP server, this event is triggered for
    every change to the request's status. The status can be one of
    `pending`, `complete`, or `cancel` and is sent as the {type} on the
    "data" table passed to the callback function.

    It triggers when the initial request is sent ({type} == `pending`) and
    when the LSP server responds ({type}

Title: LSP Events: LspAttach, LspDetach, LspNotify, LspProgress, and LspRequest
Summary
This section details several LSP events in Neovim. `LspAttach` triggers after an LSP client attaches to a buffer, providing access to the client ID. It also shows how to properly handle dynamic capability registration. `LspDetach` fires just before a client detaches, useful for cleaning up client-specific autocommands. `LspNotify` is triggered after a successful notification sent to the LSP server, allowing inspection of the method and parameters. `LspProgress` occurs upon receipt of a progress notification. `LspRequest` is triggered when request statuses change.