Skip to content

Commit cd22b72

Browse files
committed
feat: improved lsp setup for nvim > 0.11
Referring to nvim-lua#1590
1 parent 513dc38 commit cd22b72

File tree

1 file changed

+73
-52
lines changed

1 file changed

+73
-52
lines changed

init.lua

Lines changed: 73 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -749,47 +749,67 @@ require('lazy').setup({
749749
-- By default, Neovim doesn't support everything that is in the LSP specification.
750750
-- When you add blink.cmp, luasnip, etc. Neovim now has *more* capabilities.
751751
-- So, we create new capabilities with blink.cmp, and then broadcast that to the servers.
752-
local capabilities = require('blink.cmp').get_lsp_capabilities()
753-
754-
-- Enable the following language servers
755-
-- Feel free to add/remove any LSPs that you want here. They will automatically be installed.
756-
--
757-
-- Add any additional override configuration in the following tables. Available keys are:
758-
-- - cmd (table): Override the default command used to start the server
759-
-- - filetypes (table): Override the default list of associated filetypes for the server
760-
-- - capabilities (table): Override fields in capabilities. Can be used to disable certain LSP features.
761-
-- - settings (table): Override the default settings passed when initializing the server.
762-
-- For example, to see the options for `lua_ls`, you could go to: https://luals.github.io/wiki/settings/
752+
-- NOTE: The following line is now commented as blink.cmp extends capabilites by default from its internal code:
753+
-- https://github.com/Saghen/blink.cmp/blob/102db2f5996a46818661845cf283484870b60450/plugin/blink-cmp.lua
754+
-- It has been left here as a comment for educational purposes (as the predecessor completion plugin required this explicit step).
755+
-- local capabilities = require('blink.cmp').get_lsp_capabilities()
756+
757+
-- Language servers can broadly be installed in the following ways:
758+
-- 1) via the mason package manager; or
759+
-- 2) via your system's package manager; or
760+
-- 3) via a release binary from a language server's repo that's accessible somewhere on your system.
761+
762+
-- The servers table comprises of the following sub-tables:
763+
-- 1. mason
764+
-- 2. others
765+
-- Both these tables have an identical structure of language server names as keys and
766+
-- a table of language server configuration as values.
767+
---@class LspServersConfig
768+
---@field mason table<string, vim.lsp.Config>
769+
---@field others table<string, vim.lsp.Config>
763770
local servers = {
764-
clangd = {
765-
'clangd',
766-
},
767-
-- gopls = {},
768-
pylsp = {},
769-
-- rust_analyzer = {},
770-
-- ... etc. See `:help lspconfig-all` for a list of all the pre-configured LSPs
771-
--
772-
-- Some languages (like typescript) have entire language plugins that can be useful:
773-
-- https://github.com/pmizio/typescript-tools.nvim
774-
--
775-
-- But for many setups, the LSP (`ts_ls`) will work just fine
776-
-- ts_ls = {},
777-
--
771+
-- Add any additional override configuration in any of the following tables. Available keys are:
772+
-- - cmd (table): Override the default command used to start the server
773+
-- - filetypes (table): Override the default list of associated filetypes for the server
774+
-- - capabilities (table): Override fields in capabilities. Can be used to disable certain LSP features.
775+
-- - settings (table): Override the default settings passed when initializing the server.
776+
-- For example, to see the options for `lua_ls`, you could go to: https://luals.github.io/wiki/settings/
777+
mason = {
778+
clangd = {
779+
'clangd',
780+
},
781+
-- gopls = {},
782+
pylsp = {},
783+
-- rust_analyzer = {},
784+
-- ... etc. See `:help lspconfig-all` for a list of all the pre-configured LSPs
785+
--
786+
-- Some languages (like typescript) have entire language plugins that can be useful:
787+
-- https://github.com/pmizio/typescript-tools.nvim
788+
--
789+
-- But for many setups, the LSP (`ts_ls`) will work just fine
790+
-- ts_ls = {},
791+
--
778792

779-
lua_ls = {
780-
-- cmd = { ... },
781-
-- filetypes = { ... },
782-
-- capabilities = {},
783-
settings = {
784-
Lua = {
785-
completion = {
786-
callSnippet = 'Replace',
793+
lua_ls = {
794+
-- cmd = { ... },
795+
-- filetypes = { ... },
796+
-- capabilities = {},
797+
settings = {
798+
Lua = {
799+
completion = {
800+
callSnippet = 'Replace',
801+
},
802+
-- You can toggle below to ignore Lua_LS's noisy `missing-fields` warnings
803+
-- diagnostics = { disable = { 'missing-fields' } },
787804
},
788-
-- You can toggle below to ignore Lua_LS's noisy `missing-fields` warnings
789-
-- diagnostics = { disable = { 'missing-fields' } },
790805
},
791806
},
792807
},
808+
-- This table contains config for all language servers that are *not* installed via Mason.
809+
-- Structure is identical to the mason table from above.
810+
others = {
811+
-- dartls = {},
812+
},
793813
}
794814

795815
-- Ensure the servers and tools above are installed
@@ -805,29 +825,32 @@ require('lazy').setup({
805825
--
806826
-- You can add other tools here that you want Mason to install
807827
-- for you, so that they are available from within Neovim.
808-
local ensure_installed = vim.tbl_keys(servers or {})
828+
local ensure_installed = vim.tbl_keys(servers.mason or {})
809829
vim.list_extend(ensure_installed, {
810-
'autopep8',
811830
'stylua', -- Used to format Lua code
812831
'clang-format',
813832
})
814833
require('mason-tool-installer').setup { ensure_installed = ensure_installed }
815834

835+
-- Either merge all additional server configs from the `servers.mason` and `servers.others` tables
836+
-- to the default language server configs as provided by nvim-lspconfig or
837+
-- define a custom server config that's unavailable on nvim-lspconfig.
838+
for server, config in pairs(vim.tbl_extend('keep', servers.mason, servers.others)) do
839+
if not vim.tbl_isempty(config) then
840+
vim.lsp.config(server, config)
841+
end
842+
end
843+
844+
-- After configuring our language servers, we now enable them
816845
require('mason-lspconfig').setup {
817846
ensure_installed = {}, -- explicitly set to an empty table (Kickstart populates installs via mason-tool-installer)
818-
automatic_installation = false,
819-
automatic_enable = true,
820-
handlers = {
821-
function(server_name)
822-
local server = servers[server_name] or {}
823-
-- This handles overriding only values explicitly passed
824-
-- by the server configuration above. Useful when disabling
825-
-- certain features of an LSP (for example, turning off formatting for ts_ls)
826-
server.capabilities = vim.tbl_deep_extend('force', {}, capabilities, server.capabilities or {})
827-
require('lspconfig')[server_name].setup(server)
828-
end,
829-
},
847+
automatic_enable = true, -- automatically run vim.lsp.enable() for all servers that are installed via Mason
830848
}
849+
850+
-- Manually run vim.lsp.enable for all language servers that are *not* installed via Mason
851+
if not vim.tbl_isempty(servers.others) then
852+
vim.lsp.enable(vim.tbl_keys(servers.others))
853+
end
831854
end,
832855
},
833856

@@ -1170,9 +1193,7 @@ require('lazy').setup({
11701193
-- Or use telescope!
11711194
-- In normal mode type `<space>sh` then write `lazy.nvim-plugin`
11721195
-- you can continue same window with `<space>sr` which resumes last telescope search
1173-
},
1174-
---@diagnostic disable-next-line: missing-fields
1175-
{
1196+
}, {
11761197
ui = {
11771198
-- If you are using a Nerd Font: set icons to an empty table which will use the
11781199
-- default lazy.nvim defined Nerd Font icons, otherwise define a unicode icons table

0 commit comments

Comments
 (0)