Home Explore Blog CI



neovim

14th chunk of `runtime/doc/lsp.txt`
fcc7a58627f1f55aad225f6fba9e0fd3f1967821c6009a170000000100000fb9
 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' },
      filetypes = { 'c', 'cpp' },
    }
<
    • Get the resolved configuration for "luals": >lua
      local cfg = vim.lsp.config.luals
<

    Attributes: ~
        Since: 0.11.0

    Parameters: ~
      • {name}  (`string`)
      • {cfg}   (`vim.lsp.Config`) See |vim.lsp.Config|.

enable({name}, {enable})                                    *vim.lsp.enable()*
    Auto-starts LSP when a buffer is opened, based on the |lsp-config|
    `filetypes`, `root_markers`, and `root_dir` fields.

    Examples: >lua
        vim.lsp.enable('clangd')
        vim.lsp.enable({'luals', 'pyright'})
<

    Example: *lsp-restart* Passing `false` stops and detaches the client(s).
    Thus you can "restart" LSP by disabling and re-enabling a given config: >lua
        vim.lsp.enable('clangd', false)
        vim.lsp.enable('clangd', true)
<

    Example: To dynamically decide whether LSP is activated, define a
    |lsp-root_dir()| function which calls `on_dir()` only when you want that
    config to activate: >lua
        vim.lsp.config('lua_ls', {
          root_dir = function(bufnr, on_dir)
            if not vim.fn.bufname(bufnr):match('%.txt$') then
              on_dir(vim.fn.getcwd())
            end
          end
        })
<

    Attributes: ~
        Since: 0.11.0

    Parameters: ~
      • {name}    (`string|string[]`) Name(s) of client(s) to enable.
      • {enable}  (`boolean?`) `true|nil` to enable, `false` to disable
                  (actively stops and detaches clients as needed)

foldclose({kind}, {winid})                               *vim.lsp.foldclose()*
    Close all {kind} of folds in the the window with {winid}.

    To automatically fold imports when opening a file, you can use an autocmd: >lua
        vim.api.nvim_create_autocmd('LspNotify', {
          callback = function(args)
            if args.data.method == 'textDocument/didOpen' then
              vim.lsp.foldclose('imports', vim.fn.bufwinid(args.buf))
            end
          end,
        })
<

    Attributes: ~
        Since: 0.11.0

    Parameters: ~
      • {kind}   (`lsp.FoldingRangeKind`) Kind to close, one of "comment",
                 "imports" or "region".
      • {winid}  (`integer?`) Defaults to the current window.

foldexpr({lnum})                                          *vim.lsp.foldexpr()*
    Provides an interface between the built-in client and a `foldexpr`
    function.

    To use, set 'foldmethod' to "expr" and set the value of 'foldexpr': >lua
        vim.o.foldmethod = 'expr'
        vim.o.foldexpr = 'v:lua.vim.lsp.foldexpr()'
<

    Or use it only when supported by checking for the
    "textDocument/foldingRange" capability in an |LspAttach| autocommand.
    Example: >lua
        vim.o.foldmethod = 'expr'
        -- Default to treesitter folding
        vim.o.foldexpr = 'v:lua.vim.treesitter.foldexpr()'
        -- Prefer LSP folding if client supports it
        vim.api.nvim_create_autocmd('LspAttach', {
          callback = function(args)
            local client

Title: vim.lsp Configuration, Enabling, and Folding
Summary
This section provides examples of using `vim.lsp.config()` to configure LSP clients, including setting root markers and capabilities, and how to override existing configurations. It also describes `vim.lsp.enable()`, which auto-starts LSP based on filetypes, root markers, and root directories, and can be used to restart LSP. The section then covers `vim.lsp.foldclose()` to close specific fold types and `vim.lsp.foldexpr()` for integration with Vim's fold expression feature.