feat: improved lsp setup for nvim > 0.11

This commit is contained in:
Kartik Vashistha 2025-06-07 20:02:03 +01:00 committed by Ori Perry
parent e244058f03
commit 3436e0ea71
1 changed files with 86 additions and 46 deletions

132
init.lua
View File

@ -1,5 +1,4 @@
--[[ --[[
===================================================================== =====================================================================
==================== READ THIS BEFORE CONTINUING ==================== ==================== READ THIS BEFORE CONTINUING ====================
===================================================================== =====================================================================
@ -601,52 +600,76 @@ require('lazy').setup({
end, end,
}) })
-- Enable the following language servers -- LSP servers and clients are able to communicate to each other what features they support.
-- Feel free to add/remove any LSPs that you want here. They will automatically be installed. -- By default, Neovim doesn't support everything that is in the LSP specification.
-- See `:help lsp-config` for information about keys and how to configure -- When you add blink.cmp, luasnip, etc. Neovim now has *more* capabilities.
---@type table<string, vim.lsp.Config> -- So, we create new capabilities with blink.cmp, and then broadcast that to the servers.
-- NOTE: The following line is now commented as blink.cmp extends capabilites by default from its internal code:
-- https://github.com/Saghen/blink.cmp/blob/102db2f5996a46818661845cf283484870b60450/plugin/blink-cmp.lua
-- It has been left here as a comment for educational purposes (as the predecessor completion plugin required this explicit step).
--
-- local capabilities = require("blink.cmp").get_lsp_capabilities()
-- Language servers can broadly be installed in the following ways:
-- 1) via the mason package manager; or
-- 2) via your system's package manager; or
-- 3) via a release binary from a language server's repo that's accessible somewhere on your system.
--
local servers = { local servers = {
-- clangd = {}, -- Add any additional override configuration in any of the following tables. Available keys are:
-- gopls = {}, -- - cmd (table): Override the default command used to start the server
-- pyright = {}, -- - filetypes (table): Override the default list of associated filetypes for the server
-- rust_analyzer = {}, -- - capabilities (table): Override fields in capabilities. Can be used to disable certain LSP features.
-- - settings (table): Override the default settings passed when initializing the server.
-- For example, to see the options for `lua_ls`, you could go to: https://luals.github.io/wiki/settings/
-- --
-- Some languages (like typescript) have entire language plugins that can be useful: -- Feel free to add/remove any LSPs here that you want to install via Mason. They will automatically be installed and setup.
-- https://github.com/pmizio/typescript-tools.nvim mason = {
-- -- clangd = {},
-- But for many setups, the LSP (`ts_ls`) will work just fine -- gopls = {},
-- ts_ls = {}, -- pyright = {},
-- rust_analyzer = {},
-- ... etc. See `:help lspconfig-all` for a list of all the pre-configured LSPs
--
-- Some languages (like typescript) have entire language plugins that can be useful:
-- https://github.com/pmizio/typescript-tools.nvim
--
-- But for many setups, the LSP (`ts_ls`) will work just fine
-- ts_ls = {},
--
lua_ls = {
on_init = function(client)
if client.workspace_folders then
local path = client.workspace_folders[1].name
if path ~= vim.fn.stdpath 'config' and (vim.uv.fs_stat(path .. '/.luarc.json') or vim.uv.fs_stat(path .. '/.luarc.jsonc')) then return end
end
stylua = {}, -- Used to format Lua code client.config.settings.Lua = vim.tbl_deep_extend('force', client.config.settings.Lua, {
runtime = {
-- Special Lua Config, as recommended by neovim help docs version = 'LuaJIT',
lua_ls = { path = { 'lua/?.lua', 'lua/?/init.lua' },
on_init = function(client) },
if client.workspace_folders then workspace = {
local path = client.workspace_folders[1].name checkThirdParty = false,
if path ~= vim.fn.stdpath 'config' and (vim.uv.fs_stat(path .. '/.luarc.json') or vim.uv.fs_stat(path .. '/.luarc.jsonc')) then return end -- NOTE: this is a lot slower and will cause issues when working on your own configuration.
end -- See https://github.com/neovim/nvim-lspconfig/issues/3189
library = vim.tbl_extend('force', vim.api.nvim_get_runtime_file('', true), {
client.config.settings.Lua = vim.tbl_deep_extend('force', client.config.settings.Lua, { '${3rd}/luv/library',
runtime = { '${3rd}/busted/library',
version = 'LuaJIT', }),
path = { 'lua/?.lua', 'lua/?/init.lua' }, },
}, })
workspace = { end,
checkThirdParty = false, settings = {
-- NOTE: this is a lot slower and will cause issues when working on your own configuration. Lua = {},
-- See https://github.com/neovim/nvim-lspconfig/issues/3189 },
library = vim.tbl_extend('force', vim.api.nvim_get_runtime_file('', true), {
'${3rd}/luv/library',
'${3rd}/busted/library',
}),
},
})
end,
settings = {
Lua = {},
}, },
}, },
-- This table contains config for all language servers that are *not* installed via Mason.
-- Structure is identical to the mason table from above.
others = {
-- dartls = {},
},
} }
-- Ensure the servers and tools above are installed -- Ensure the servers and tools above are installed
@ -656,17 +679,34 @@ require('lazy').setup({
-- :Mason -- :Mason
-- --
-- You can press `g?` for help in this menu. -- You can press `g?` for help in this menu.
local ensure_installed = vim.tbl_keys(servers or {}) --
-- `mason` had to be setup earlier: to configure its options see the
-- `dependencies` table for `nvim-lspconfig` above.
--
-- You can add other tools here that you want Mason to install
-- for you, so that they are available from within Neovim.
local ensure_installed = vim.tbl_keys(servers.mason or {})
vim.list_extend(ensure_installed, { vim.list_extend(ensure_installed, {
-- You can add other tools here that you want Mason to install -- You can add other tools here that you want Mason to install
}) })
require('mason-tool-installer').setup { ensure_installed = ensure_installed } require('mason-tool-installer').setup { ensure_installed = ensure_installed }
for name, server in pairs(servers) do -- Either merge all additional server configs from the `servers.mason` and `servers.others` tables
vim.lsp.config(name, server) -- to the default language server configs as provided by nvim-lspconfig or
vim.lsp.enable(name) -- define a custom server config that's unavailable on nvim-lspconfig.
for server, config in pairs(vim.tbl_extend('keep', servers.mason, servers.others)) do
if vim.fn.empty(config) ~= 1 then vim.lsp.config(server, config) end
end end
-- After configuring our language servers, we now enable them
require('mason-lspconfig').setup {
ensure_installed = {}, -- explicitly set to an empty table (Kickstart populates installs via mason-tool-installer)
automatic_enable = true, -- automatically run vim.lsp.enable() for all servers that are installed via Mason
}
-- Manually run vim.lsp.enable for all language servers that are *not* installed via Mason
if vim.fn.empty(servers.others) ~= 1 then vim.lsp.enable(vim.tbl_keys(servers.others)) end
end, end,
}, },