Skip to content

Commit 8d88982

Browse files
committed
feat: improved lsp setup for nvim > 0.11 nvim-lua#1590 + my lsp server config
nvim-lua#1590
1 parent a95bbf4 commit 8d88982

File tree

1 file changed

+75
-48
lines changed

1 file changed

+75
-48
lines changed

init.lua

Lines changed: 75 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -659,51 +659,73 @@ require('lazy').setup({
659659
-- By default, Neovim doesn't support everything that is in the LSP specification.
660660
-- When you add blink.cmp, luasnip, etc. Neovim now has *more* capabilities.
661661
-- So, we create new capabilities with blink.cmp, and then broadcast that to the servers.
662-
local capabilities = require('blink.cmp').get_lsp_capabilities()
663-
664-
-- Enable the following language servers
665-
-- Feel free to add/remove any LSPs that you want here. They will automatically be installed.
662+
-- NOTE: The following line is now commented as blink.cmp extends capabilites by default from its internal code:
663+
-- https://github.com/Saghen/blink.cmp/blob/102db2f5996a46818661845cf283484870b60450/plugin/blink-cmp.lua
664+
-- It has been left here as a comment for educational purposes (as the predecessor completion plugin required this explicit step).
666665
--
667-
-- Add any additional override configuration in the following tables. Available keys are:
668-
-- - cmd (table): Override the default command used to start the server
669-
-- - filetypes (table): Override the default list of associated filetypes for the server
670-
-- - capabilities (table): Override fields in capabilities. Can be used to disable certain LSP features.
671-
-- - settings (table): Override the default settings passed when initializing the server.
672-
-- For example, to see the options for `lua_ls`, you could go to: https://luals.github.io/wiki/settings/
666+
-- local capabilities = require("blink.cmp").get_lsp_capabilities()
667+
668+
-- Language servers can broadly be installed in the following ways:
669+
-- 1) via the mason package manager; or
670+
-- 2) via your system's package manager; or
671+
-- 3) via a release binary from a language server's repo that's accessible somewhere on your system.
672+
673+
-- The servers table comprises of the following sub-tables:
674+
-- 1. mason
675+
-- 2. others
676+
-- Both these tables have an identical structure of language server names as keys and
677+
-- a table of language server configuration as values.
678+
---@class LspServersConfig
679+
---@field mason table<string, vim.lsp.Config>
680+
---@field others table<string, vim.lsp.Config>
673681
local servers = {
674-
-- clangd = {},
675-
-- gopls = {},
676-
-- pyright = {},
677-
-- rust_analyzer = {},
678-
-- ... etc. See `:help lspconfig-all` for a list of all the pre-configured LSPs
679-
--
680-
-- Some languages (like typescript) have entire language plugins that can be useful:
681-
-- https://github.com/pmizio/typescript-tools.nvim
682-
--
683-
-- But for many setups, the LSP (`ts_ls`) will work just fine
684-
-- ts_ls = {},
682+
-- Add any additional override configuration in any of the following tables. Available keys are:
683+
-- - cmd (table): Override the default command used to start the server
684+
-- - filetypes (table): Override the default list of associated filetypes for the server
685+
-- - capabilities (table): Override fields in capabilities. Can be used to disable certain LSP features.
686+
-- - settings (table): Override the default settings passed when initializing the server.
687+
-- For example, to see the options for `lua_ls`, you could go to: https://luals.github.io/wiki/settings/
685688
--
686-
687-
lua_ls = {
688-
-- cmd = { ... },
689-
-- filetypes = { ... },
690-
-- capabilities = {},
691-
settings = {
692-
Lua = {
693-
completion = {
694-
callSnippet = 'Replace',
689+
-- Feel free to add/remove any LSPs here that you want to install via Mason. They will automatically be installed and setup.
690+
mason = {
691+
-- clangd = {},
692+
-- gopls = {},
693+
-- pyright = {},
694+
-- rust_analyzer = {},
695+
-- ... etc. See `:help lspconfig-all` for a list of all the pre-configured LSPs
696+
--
697+
-- Some languages (like typescript) have entire language plugins that can be useful:
698+
-- https://github.com/pmizio/typescript-tools.nvim
699+
--
700+
-- But for many setups, the LSP (`ts_ls`) will work just fine
701+
-- ts_ls = {},
702+
--
703+
lua_ls = {
704+
-- cmd = { ... },
705+
-- filetypes = { ... },
706+
-- capabilities = {},
707+
settings = {
708+
Lua = {
709+
completion = {
710+
callSnippet = 'Replace',
711+
},
712+
-- You can toggle below to ignore Lua_LS's noisy `missing-fields` warnings
713+
-- diagnostics = { disable = { 'missing-fields' } },
695714
},
696-
-- You can toggle below to ignore Lua_LS's noisy `missing-fields` warnings
697-
-- diagnostics = { disable = { 'missing-fields' } },
698715
},
699716
},
700-
},
701717

702-
beancount = {
703-
init_options = {
704-
journal_file = '~/Documents/paisa/ledger/main.beancount',
718+
beancount = {
719+
init_options = {
720+
journal_file = '~/Documents/paisa/ledger/main.beancount',
721+
},
705722
},
706723
},
724+
-- This table contains config for all language servers that are *not* installed via Mason.
725+
-- Structure is identical to the mason table from above.
726+
others = {
727+
-- dartls = {},
728+
},
707729
}
708730

709731
-- Ensure the servers and tools above are installed
@@ -719,26 +741,31 @@ require('lazy').setup({
719741
--
720742
-- You can add other tools here that you want Mason to install
721743
-- for you, so that they are available from within Neovim.
722-
local ensure_installed = vim.tbl_keys(servers or {})
744+
local ensure_installed = vim.tbl_keys(servers.mason or {})
723745
vim.list_extend(ensure_installed, {
724746
'stylua', -- Used to format Lua code
725747
})
726748
require('mason-tool-installer').setup { ensure_installed = ensure_installed }
727749

750+
-- Either merge all additional server configs from the `servers.mason` and `servers.others` tables
751+
-- to the default language server configs as provided by nvim-lspconfig or
752+
-- define a custom server config that's unavailable on nvim-lspconfig.
753+
for server, config in pairs(vim.tbl_extend('keep', servers.mason, servers.others)) do
754+
if not vim.tbl_isempty(config) then
755+
vim.lsp.config(server, config)
756+
end
757+
end
758+
759+
-- After configuring our language servers, we now enable them
728760
require('mason-lspconfig').setup {
729761
ensure_installed = {}, -- explicitly set to an empty table (Kickstart populates installs via mason-tool-installer)
730-
automatic_installation = false,
731-
handlers = {
732-
function(server_name)
733-
local server = servers[server_name] or {}
734-
-- This handles overriding only values explicitly passed
735-
-- by the server configuration above. Useful when disabling
736-
-- certain features of an LSP (for example, turning off formatting for ts_ls)
737-
server.capabilities = vim.tbl_deep_extend('force', {}, capabilities, server.capabilities or {})
738-
require('lspconfig')[server_name].setup(server)
739-
end,
740-
},
762+
automatic_enable = true, -- automatically run vim.lsp.enable() for all servers that are installed via Mason
741763
}
764+
765+
-- Manually run vim.lsp.enable for all language servers that are *not* installed via Mason
766+
if not vim.tbl_isempty(servers.others) then
767+
vim.lsp.enable(vim.tbl_keys(servers.others))
768+
end
742769
end,
743770
},
744771

0 commit comments

Comments
 (0)