junnplus/lsp-setup.nvim

github github
lspneovim-0.5
stars 225
issues 3
subscribers 5
forks 12
CREATED

2022-04-24

UPDATED

5 months ago


lsp-setup.nvim

A simple wrapper for nvim-lspconfig and mason-lspconfig (optional) to easily setup LSP servers.

Installation

  • Neovim >= 0.8 (>= 0.10 or nightly for inlay hints)
  • nvim-lspconfig
  • mason.nvim & mason-lspconfig.nvim (optional)

lazy.nvim

{
    'junnplus/lsp-setup.nvim',
    dependencies = {
        'neovim/nvim-lspconfig',
        'williamboman/mason.nvim', -- optional
        'williamboman/mason-lspconfig.nvim', -- optional
    },
}

packer.nvim

use {
    'junnplus/lsp-setup.nvim',
    requires = {
        'neovim/nvim-lspconfig',
        'williamboman/mason.nvim', -- optional
        'williamboman/mason-lspconfig.nvim', -- optional
    }
}

Usage

require('lsp-setup').setup({
    servers = {
        pylsp = {},
        clangd = {}
    }
})

You can replace pylsp with the LSP server name you need, see available LSPs.

Also support installing custom versions of LSP servers (requires mason and mason-lspconfig), for example:

require('lsp-setup').setup({
    servers = {
        ['rust_analyzer@nightly'] = {}
    }
})

LSP servers returns a table will automatically setup server process using lspconfig. You can also pass a nil function to setup the server manually, see Integrations/rust-tools.nvim.

Inlay hints

Inlay hints are only available in Neovim >= 0.10 or nightly.

require('lsp-setup').setup({
    inlay_hints = {
        enabled = true,
    }
})
require('lsp-setup').setup({
    servers = {
        tsserver = {
            settings = {
                typescript = {
                    inlayHints = {
                        includeInlayParameterNameHints = 'all',
                        includeInlayParameterNameHintsWhenArgumentMatchesName = false,
                        includeInlayFunctionParameterTypeHints = true,
                        includeInlayVariableTypeHints = true,
                        includeInlayVariableTypeHintsWhenTypeMatchesName = false,
                        includeInlayPropertyDeclarationTypeHints = true,
                        includeInlayFunctionLikeReturnTypeHints = true,
                        includeInlayEnumMemberValueHints = true,
                    }
                },
            }
        },
    }
})
require('lsp-setup').setup({
    servers = {
        volar = {
            settings = {
                typescript = {
                    inlayHints = {
                        enumMemberValues = {
                            enabled = true,
                        },
                        functionLikeReturnTypes = {
                            enabled = true,
                        },
                        propertyDeclarationTypes = {
                            enabled = true,
                        },
                        parameterTypes = {
                            enabled = true,
                            suppressWhenArgumentMatchesName = true,
                        },
                        variableTypes = {
                            enabled = true,
                        },
                    }
                },
            }
        }
    }
})
require('lsp-setup').setup({
    servers = {
        gopls = {
            settings = {
                gopls = {
                    hints = {
                        rangeVariableTypes = true,
                        parameterNames = true,
                        constantValues = true,
                        assignVariableTypes = true,
                        compositeLiteralFields = true,
                        compositeLiteralTypes = true,
                        functionTypeParameters = true,
                    },
                },
            },
        },
    }
})
require('lsp-setup').setup({
    servers = {
        rust_analyzer = {
            settings = {
                ['rust-analyzer'] = {
                    inlayHints = {
                        bindingModeHints = {
                            enable = false,
                        },
                        chainingHints = {
                            enable = true,
                        },
                        closingBraceHints = {
                            enable = true,
                            minLines = 25,
                        },
                        closureReturnTypeHints = {
                            enable = 'never',
                        },
                        lifetimeElisionHints = {
                            enable = 'never',
                            useParameterNames = false,
                        },
                        maxLength = 25,
                        parameterHints = {
                            enable = true,
                        },
                        reborrowHints = {
                            enable = 'never',
                        },
                        renderColons = true,
                        typeHints = {
                            enable = true,
                            hideClosureInitialization = false,
                            hideNamedConstructor = false,
                        },
                    }
                },
            },
        },
    }
})
require('lsp-setup').setup({
    servers = {
        lua_ls = {
            settings = {
                Lua = {
                    hint = {
                        enable = false,
                        arrayIndex = "Auto",
                        await = true,
                        paramName = "All",
                        paramType = true,
                        semicolon = "SameLine",
                        setType = false,
                    },
                },
            },
        },
    }
})
require('lsp-setup').setup({
    servers = {
        zls = {
            settings = {
                zls = {
                    enable_inlay_hints = true,
                    inlay_hints_show_builtin = true,
                    inlay_hints_exclude_single_argument = true,
                    inlay_hints_hide_redundant_param_names = false,
                    inlay_hints_hide_redundant_param_names_last_token = false,
                }
            }
        },
    }
})

Setup structure

require('lsp-setup').setup({
    -- Default mappings
    -- gD = 'lua vim.lsp.buf.declaration()',
    -- gd = 'lua vim.lsp.buf.definition()',
    -- gt = 'lua vim.lsp.buf.type_definition()',
    -- gi = 'lua vim.lsp.buf.implementation()',
    -- gr = 'lua vim.lsp.buf.references()',
    -- K = 'lua vim.lsp.buf.hover()',
    -- ['<C-k>'] = 'lua vim.lsp.buf.signature_help()',
    -- ['<space>rn'] = 'lua vim.lsp.buf.rename()',
    -- ['<space>ca'] = 'lua vim.lsp.buf.code_action()',
    -- ['<space>f'] = 'lua vim.lsp.buf.formatting()',
    -- ['<space>e'] = 'lua vim.diagnostic.open_float()',
    -- ['[d'] = 'lua vim.diagnostic.goto_prev()',
    -- [']d'] = 'lua vim.diagnostic.goto_next()',
    default_mappings = true,
    -- Custom mappings, will overwrite the default mappings for the same key
    -- Example mappings for telescope pickers:
    -- gd = 'lua require"telescope.builtin".lsp_definitions()',
    -- gi = 'lua require"telescope.builtin".lsp_implementations()',
    -- gr = 'lua require"telescope.builtin".lsp_references()',
    mappings = {},
    -- Global on_attach
    on_attach = function(client, bufnr)
        -- Support custom the on_attach function for global
        -- Formatting on save as default
        require('lsp-setup.utils').format_on_save(client)
    end,
    -- Global capabilities
    capabilities = vim.lsp.protocol.make_client_capabilities(),
    -- Configuration of LSP servers 
    servers = {
        -- Install LSP servers automatically (requires mason and mason-lspconfig)
        -- LSP server configuration please see: https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md
        -- pylsp = {},
        -- rust_analyzer = {
        --     settings = {
        --         ['rust-analyzer'] = {
        --             cargo = {
        --                 loadOutDirsFromCheck = true,
        --             },
        --             procMacro = {
        --                 enable = true,
        --             },
        --         },
        --     },
        -- },
    },
    -- Configuration of LSP inlay hints
    inlay_hints = {
        enabled = false,
        highlight = 'Comment',
    }
})

Integrations

cmp-nvim-lsp or coq_nvim

If installed, will auto advertise capabilities to LSP servers.

neodev

-- Setup lua_ls with neodev
require('neodev').setup()
require('lsp-setup').setup({
    servers = {
        lua_ls = {}
    }
})

rust-tools.nvim

require('lsp-setup').setup({
    servers = {
        rust_analyzer = function()
            require('rust-tools').setup({
                server = {
                    settings = {
                        ['rust-analyzer'] = {
                            cargo = {
                                loadOutDirsFromCheck = true,
                            },
                            procMacro = {
                                enable = true,
                            },
                        },
                    },
                },
            })
            -- no need to return anything
        end,
    }
})

Contributing

Bug reports and feature requests are welcome! PRs are doubly welcome!