Home Explore Blog CI



neovim

3rd chunk of `runtime/doc/lsp.txt`
06d4f8912322bc31785be5d8b54d76c8afbc56fae88f61bb0000000100000fa1

|vim.tbl_deep_extend()|.

Example: given the following configs... >lua
  -- Defined in init.lua
  vim.lsp.config('*', {
    capabilities = {
      textDocument = {
        semanticTokens = {
          multilineTokenSupport = true,
        }
      }
    },
    root_markers = { '.git' },
  })

  -- Defined in <rtp>/lsp/clangd.lua
  return {
    cmd = { 'clangd' },
    root_markers = { '.clangd', 'compile_commands.json' },
    filetypes = { 'c', 'cpp' },
  }

  -- Defined in init.lua
  vim.lsp.config('clangd', {
    filetypes = { 'c' },
  })
<
...the merged result is: >lua
  {
    -- From the clangd configuration in <rtp>/lsp/clangd.lua
    cmd = { 'clangd' },

    -- From the clangd configuration in <rtp>/lsp/clangd.lua
    -- Overrides the "*" configuration in init.lua
    root_markers = { '.clangd', 'compile_commands.json' },

    -- From the clangd configuration in init.lua
    -- Overrides the clangd configuration in <rtp>/lsp/clangd.lua
    filetypes = { 'c' },

    -- From the "*" configuration in init.lua
    capabilities = {
      textDocument = {
        semanticTokens = {
          multilineTokenSupport = true,
        }
      }
    }
  }
<
                                                        *lsp-attach*
To use LSP features beyond those provided by Nvim (see |lsp-buf|), you can set
keymaps and options on |Client:on_attach()| or |LspAttach|. Not all language
servers provide the same capabilities; check `supports_method()` in your
LspAttach handler.
                                                        *lsp-lint* *lsp-format*
Example: Enable auto-completion and auto-formatting ("linting"): >lua

    vim.api.nvim_create_autocmd('LspAttach', {
      group = vim.api.nvim_create_augroup('my.lsp', {}),
      callback = function(args)
        local client = assert(vim.lsp.get_client_by_id(args.data.client_id))
        if client:supports_method('textDocument/implementation') then
          -- Create a keymap for vim.lsp.buf.implementation ...
        end

        -- Enable auto-completion. Note: Use CTRL-Y to select an item. |complete_CTRL-Y|
        if client:supports_method('textDocument/completion') then
          -- Optional: trigger autocompletion on EVERY keypress. May be slow!
          -- local chars = {}; for i = 32, 126 do table.insert(chars, string.char(i)) end
          -- client.server_capabilities.completionProvider.triggerCharacters = chars

          vim.lsp.completion.enable(true, client.id, args.buf, {autotrigger = true})
        end

        -- Auto-format ("lint") on save.
        -- Usually not needed if server supports "textDocument/willSaveWaitUntil".
        if not client:supports_method('textDocument/willSaveWaitUntil')
            and client:supports_method('textDocument/formatting') then
          vim.api.nvim_create_autocmd('BufWritePre', {
            group = vim.api.nvim_create_augroup('my.lsp', {clear=false}),
            buffer = args.buf,
            callback = function()
              vim.lsp.buf.format({ bufnr = args.buf, id = client.id, timeout_ms = 1000 })
            end,
          })
        end
      end,
    })
<
To see the capabilities for a given server, try this in a LSP-enabled buffer: >vim

    :lua =vim.lsp.get_clients()[1].server_capabilities

================================================================================
FAQ                                                     *lsp-faq*

- Q: How to force-reload LSP?
- A: Stop all clients, then reload the buffer. >vim
     :lua vim.lsp.stop_client(vim.lsp.get_clients())
     :edit

- Q: Why isn't completion working?
- A: In the buffer where you want to use LSP, check that 'omnifunc' is set to
     "v:lua.vim.lsp.omnifunc": `:verbose set omnifunc?`
     - Some other plugin may be overriding the option. To avoid that you could
       set the option in an |after-directory| ftplugin, e.g.
       "after/ftplugin/python.vim".

- Q: How do I run a request synchronously (e.g. for formatting on file save)?
- A: Check if the function

Title: LSP Configuration Examples and FAQ
Summary
This section provides examples of how LSP configurations are merged in Neovim using `vim.tbl_deep_extend()`. It also demonstrates how to extend LSP features using `Client:on_attach()` or `LspAttach`, including enabling auto-completion and auto-formatting based on server capabilities. A FAQ section addresses common issues like forcing LSP reloads, troubleshooting completion problems, and running requests synchronously.