LSP
⌨️ Customizing LSP Keymaps
The syntax for adding, deleting and changing LSP Keymaps,
is the same as for plugin keymaps,
but you need to configure it using the init()
method.
-- LSP keymaps
{
"neovim/nvim-lspconfig",
init = function()
local keys = require("lazyvim.plugins.lsp.keymaps").get()
-- change a keymap
keys[#keys + 1] = { "K", "<cmd>echo 'hello'<cr>" }
-- disable a keymap
keys[#keys + 1] = { "K", false }
-- add a keymap
keys[#keys + 1] = { "H", "<cmd>echo 'hello'<cr>" }
end,
}
nvim-lspconfig
lspconfig
- Options
- Full Spec
opts = {
-- options for vim.diagnostic.config()
diagnostics = {
underline = true,
update_in_insert = false,
virtual_text = { spacing = 4, prefix = "●" },
severity_sort = true,
},
-- Automatically format on save
autoformat = true,
-- options for vim.lsp.buf.format
-- `bufnr` and `filter` is handled by the LazyVim formatter,
-- but can be also overridden when specified
format = {
formatting_options = nil,
timeout_ms = nil,
},
-- LSP Server Settings
---@type lspconfig.options
servers = {
jsonls = {},
lua_ls = {
-- mason = false, -- set to false if you don't want this server to be installed with mason
settings = {
Lua = {
workspace = {
checkThirdParty = false,
},
completion = {
callSnippet = "Replace",
},
},
},
},
},
-- you can do any additional lsp server setup here
-- return true if you don't want this server to be setup with lspconfig
---@type table<string, fun(server:string, opts:_.lspconfig.options):boolean?>
setup = {
-- example to setup with typescript.nvim
-- tsserver = function(_, opts)
-- require("typescript").setup({ server = opts })
-- return true
-- end,
-- Specify * to use this function as a fallback for any server
-- ["*"] = function(server, opts) end,
},
}
{
"neovim/nvim-lspconfig",
event = { "BufReadPre", "BufNewFile" },
dependencies = {
{ "folke/neoconf.nvim", cmd = "Neoconf", config = true },
{ "folke/neodev.nvim", opts = { experimental = { pathStrict = true } } },
"mason.nvim",
"williamboman/mason-lspconfig.nvim",
{
"hrsh7th/cmp-nvim-lsp",
cond = function()
return require("lazyvim.util").has("nvim-cmp")
end,
},
},
---@class PluginLspOpts
opts = {
-- options for vim.diagnostic.config()
diagnostics = {
underline = true,
update_in_insert = false,
virtual_text = { spacing = 4, prefix = "●" },
severity_sort = true,
},
-- Automatically format on save
autoformat = true,
-- options for vim.lsp.buf.format
-- `bufnr` and `filter` is handled by the LazyVim formatter,
-- but can be also overridden when specified
format = {
formatting_options = nil,
timeout_ms = nil,
},
-- LSP Server Settings
---@type lspconfig.options
servers = {
jsonls = {},
lua_ls = {
-- mason = false, -- set to false if you don't want this server to be installed with mason
settings = {
Lua = {
workspace = {
checkThirdParty = false,
},
completion = {
callSnippet = "Replace",
},
},
},
},
},
-- you can do any additional lsp server setup here
-- return true if you don't want this server to be setup with lspconfig
---@type table<string, fun(server:string, opts:_.lspconfig.options):boolean?>
setup = {
-- example to setup with typescript.nvim
-- tsserver = function(_, opts)
-- require("typescript").setup({ server = opts })
-- return true
-- end,
-- Specify * to use this function as a fallback for any server
-- ["*"] = function(server, opts) end,
},
},
---@param opts PluginLspOpts
config = function(_, opts)
-- setup autoformat
require("lazyvim.plugins.lsp.format").autoformat = opts.autoformat
-- setup formatting and keymaps
require("lazyvim.util").on_attach(function(client, buffer)
require("lazyvim.plugins.lsp.format").on_attach(client, buffer)
require("lazyvim.plugins.lsp.keymaps").on_attach(client, buffer)
end)
-- diagnostics
for name, icon in pairs(require("lazyvim.config").icons.diagnostics) do
name = "DiagnosticSign" .. name
vim.fn.sign_define(name, { text = icon, texthl = name, numhl = "" })
end
vim.diagnostic.config(opts.diagnostics)
local servers = opts.servers
local capabilities = require("cmp_nvim_lsp").default_capabilities(vim.lsp.protocol.make_client_capabilities())
local function setup(server)
local server_opts = vim.tbl_deep_extend("force", {
capabilities = vim.deepcopy(capabilities),
}, servers[server] or {})
if opts.setup[server] then
if opts.setup[server](server, server_opts) then
return
end
elseif opts.setup["*"] then
if opts.setup["*"](server, server_opts) then
return
end
end
require("lspconfig")[server].setup(server_opts)
end
local have_mason, mlsp = pcall(require, "mason-lspconfig")
local available = have_mason and mlsp.get_available_servers() or {}
local ensure_installed = {} ---@type string[]
for server, server_opts in pairs(servers) do
if server_opts then
server_opts = server_opts == true and {} or server_opts
-- run manual setup if mason=false or if this is a server that cannot be installed with mason-lspconfig
if server_opts.mason == false or not vim.tbl_contains(available, server) then
setup(server)
else
ensure_installed[#ensure_installed + 1] = server
end
end
end
if have_mason then
mlsp.setup({ ensure_installed = ensure_installed })
mlsp.setup_handlers({ setup })
end
end,
}
neoconf.nvim
- Options
- Full Spec
opts = {}
{ "folke/neoconf.nvim", cmd = "Neoconf", config = true }
neodev.nvim
- Options
- Full Spec
opts = { experimental = { pathStrict = true } }
{ "folke/neodev.nvim", opts = { experimental = { pathStrict = true } } }
mason-lspconfig.nvim
- Options
- Full Spec
opts = nil
"williamboman/mason-lspconfig.nvim"
cmp-nvim-lsp
- Options
- Full Spec
opts = nil
{
"hrsh7th/cmp-nvim-lsp",
cond = function()
return require("lazyvim.util").has("nvim-cmp")
end,
}
null-ls.nvim
formatters
- Options
- Full Spec
opts = function()
local nls = require("null-ls")
return {
root_dir = require("null-ls.utils").root_pattern(".null-ls-root", ".neoconf.json", "Makefile", ".git"),
sources = {
nls.builtins.formatting.fish_indent,
nls.builtins.diagnostics.fish,
nls.builtins.formatting.stylua,
nls.builtins.formatting.shfmt,
nls.builtins.diagnostics.flake8,
},
}
end
{
"jose-elias-alvarez/null-ls.nvim",
event = { "BufReadPre", "BufNewFile" },
dependencies = { "mason.nvim" },
opts = function()
local nls = require("null-ls")
return {
root_dir = require("null-ls.utils").root_pattern(".null-ls-root", ".neoconf.json", "Makefile", ".git"),
sources = {
nls.builtins.formatting.fish_indent,
nls.builtins.diagnostics.fish,
nls.builtins.formatting.stylua,
nls.builtins.formatting.shfmt,
nls.builtins.diagnostics.flake8,
},
}
end,
}
mason.nvim
cmdline tools and lsp servers
- Options
- Full Spec
opts = {
ensure_installed = {
"stylua",
"shfmt",
"flake8",
},
}
{
"williamboman/mason.nvim",
cmd = "Mason",
keys = { { "<leader>cm", "<cmd>Mason<cr>", desc = "Mason" } },
opts = {
ensure_installed = {
"stylua",
"shfmt",
"flake8",
},
},
---@param opts MasonSettings | {ensure_installed: string[]}
config = function(_, opts)
require("mason").setup(opts)
local mr = require("mason-registry")
for _, tool in ipairs(opts.ensure_installed) do
local p = mr.get_package(tool)
if not p:is_installed() then
p:install()
end
end
end,
}