Skip to content

Commit 68a7686

Browse files
committed
feat: improve LSP setup for nvim > 0.11
Based on nvim-lua#1590 And use `expert` for Elixir
1 parent 90e6c3b commit 68a7686

File tree

1 file changed

+94
-87
lines changed

1 file changed

+94
-87
lines changed

init.lua

Lines changed: 94 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -619,94 +619,91 @@ require('lazy').setup({
619619
},
620620
}
621621

622+
-- LSP servers and clients are able to communicate to each other what features they support.
623+
-- By default, Neovim doesn't support everything that is in the LSP specification.
624+
-- When you add blink.cmp, luasnip, etc. Neovim now has *more* capabilities.
625+
-- So, we create new capabilities with blink.cmp, and then broadcast that to the servers.
626+
--
622627
-- Enable the following language servers
623628
-- Feel free to add/remove any LSPs that you want here. They will automatically be installed.
624629
--
625-
-- Add any additional override configuration in the following tables. Available keys are:
626-
-- - cmd (table): Override the default command used to start the server
627-
-- - filetypes (table): Override the default list of associated filetypes for the server
628-
-- - capabilities (table): Override fields in capabilities. Can be used to disable certain LSP features.
629-
-- - settings (table): Override the default settings passed when initializing the server.
630-
-- For example, to see the options for `lua_ls`, you could go to: https://luals.github.io/wiki/settings/
630+
-- NOTE: The following line is now commented as blink.cmp extends capabilites by default from its internal code:
631+
-- https://github.com/Saghen/blink.cmp/blob/102db2f5996a46818661845cf283484870b60450/plugin/blink-cmp.lua
632+
-- It has been left here as a comment for educational purposes (as the predecessor completion plugin required this explicit step).
633+
-- local capabilities = require("blink.cmp").get_lsp_capabilities()
634+
--
635+
-- Language servers can broadly be installed in the following ways:
636+
-- 1) via the mason package manager; or
637+
-- 2) via your system's package manager; or
638+
-- 3) via a release binary from a language server's repo that's accessible somewhere on your system.
639+
-- The servers table comprises of the following sub-tables:
640+
-- 1. mason
641+
-- 2. others
642+
-- Both these tables have an identical structure of language server names as keys and
643+
-- a table of language server configuration as values.
644+
---@class LspServersConfig
645+
---@field mason table<string, vim.lsp.Config>
646+
---@field others table<string, vim.lsp.Config>
631647
local servers = {
632-
-- clangd = {},
633-
-- gopls = {},
634-
-- pyright = {},
635-
-- rust_analyzer = {},
636-
-- ... etc. See `:help lspconfig-all` for a list of all the pre-configured LSPs
637-
--
638-
-- Some languages (like typescript) have entire language plugins that can be useful:
639-
-- https://github.com/pmizio/typescript-tools.nvim
640-
--
641-
-- But for many setups, the LSP (`ts_ls`) will work just fine
642-
-- ts_ls = {},
643-
--
644-
645-
lua_ls = {
646-
-- cmd = { ... },
647-
-- filetypes = { ... },
648-
-- capabilities = {},
649-
settings = {
650-
Lua = {
651-
completion = {
652-
callSnippet = 'Replace',
648+
-- Add any additional override configuration in the following tables. Available keys are:
649+
-- - cmd (table): Override the default command used to start the server
650+
-- - filetypes (table): Override the default list of associated filetypes for the server
651+
-- - capabilities (table): Override fields in capabilities. Can be used to disable certain LSP features.
652+
-- - settings (table): Override the default settings passed when initializing the server.
653+
-- For example, to see the options for `lua_ls`, you could go to: https://luals.github.io/wiki/settings/
654+
-- Feel free to add/remove any LSPs here that you want to install via Mason. They will automatically be installed and setup.
655+
mason = {
656+
-- clangd = {},
657+
-- gopls = {},
658+
-- pyright = {},
659+
-- rust_analyzer = {},
660+
-- ... etc. See `:help lspconfig-all` for a list of all the pre-configured LSPs
661+
--
662+
-- Some languages (like typescript) have entire language plugins that can be useful:
663+
-- https://github.com/pmizio/typescript-tools.nvim
664+
--
665+
-- But for many setups, the LSP (`ts_ls`) will work just fine
666+
-- ts_ls = {},
667+
--
668+
lua_ls = {
669+
-- cmd = { ... },
670+
-- filetypes = { ... },
671+
-- capabilities = {},
672+
settings = {
673+
Lua = {
674+
completion = {
675+
callSnippet = 'Replace',
676+
},
677+
-- You can toggle below to ignore Lua_LS's noisy `missing-fields` warnings
678+
-- diagnostics = { disable = { 'missing-fields' } },
653679
},
654-
-- You can toggle below to ignore Lua_LS's noisy `missing-fields` warnings
655-
-- diagnostics = { disable = { 'missing-fields' } },
656680
},
657681
},
658-
},
659-
-- -- TODO: is it properly configured?
660-
-- elixirls = {
661-
-- settings = {
662-
-- elixirLS = {
663-
-- dialyzerEnabled = false,
664-
-- incrementalDialyzer = false,
665-
-- suggestSpecs = false,
666-
-- fetchDeps = false,
667-
-- mcpEnabled = true,
668-
-- },
669-
-- },
670-
-- },
671-
expert = {
672-
-- Use Nightly Builds
673-
-- https://github.com/elixir-lang/expert/tree/main?tab=readme-ov-file#nightly-builds
674-
cmd = { '/Users/nicolognudi/code/personal/expert_darwin_arm64' },
675-
root_markers = { 'mix.exs', '.git' },
676-
filetypes = { 'elixir', 'eelixir', 'heex' },
677-
},
678-
ts_ls = {},
679-
typos_lsp = {
680-
init_options = {
681-
-- Custom config. Used together with a config file found in the workspace or its parents,
682-
-- taking precedence for settings declared in both.
683-
-- Equivalent to the typos `--config` cli argument.
684-
config = '~/.config/typos/typos.toml',
685-
-- How typos are rendered in the editor, can be one of an Error, Warning, Info or Hint.
686-
-- Defaults to error.
687-
diagnosticSeverity = 'Warning',
682+
expert = {
683+
-- Use Nightly Builds
684+
-- https://github.com/elixir-lang/expert/tree/main?tab=readme-ov-file#nightly-builds
685+
cmd = { '/Users/nicolognudi/code/personal/expert_darwin_arm64' },
686+
root_markers = { 'mix.exs', '.git' },
687+
filetypes = { 'elixir', 'eelixir', 'heex' },
688+
},
689+
ts_ls = {},
690+
typos_lsp = {
691+
init_options = {
692+
-- Custom config. Used together with a config file found in the workspace or its parents,
693+
-- taking precedence for settings declared in both.
694+
-- Equivalent to the typos `--config` cli argument.
695+
config = '~/.config/typos/typos.toml',
696+
-- How typos are rendered in the editor, can be one of an Error, Warning, Info or Hint.
697+
-- Defaults to error.
698+
diagnosticSeverity = 'Warning',
699+
},
688700
},
689701
},
690-
}
691-
692-
---@type MasonLspconfigSettings
693-
require('mason-lspconfig').setup {
694-
ensure_installed = {}, -- explicitly set to an empty table (Kickstart populates installs via mason-tool-installer)
695-
-- NOTE: When the keys of servers are passed as a table to automatic_enable,
696-
-- it causes mason-lspconfig to ignore any LSPs that are installed via Mason
697-
-- but not explicitly listed in servers.
698-
-- https://github.com/nvim-lua/kickstart.nvim/pull/1475/files#r2094166972
699-
automatic_enable = vim.tbl_keys(servers or {}),
700-
-- TODO: remove this ?
701-
-- handlers = {
702-
-- function(server_name)
703-
-- local config = servers[server_name] or {}
704-
-- vim.lsp.config(server_name, config)
705-
-- -- It should not be necessary with Mason 2
706-
-- -- https://github.com/nvim-lua/kickstart.nvim/pull/1475#discussion_r2077249925
707-
-- -- vim.lsp.enable(server_name)
708-
-- end,
709-
-- },
702+
-- This table contains config for all language servers that are *not* installed via Mason.
703+
-- Structure is identical to the mason table from above.
704+
others = {
705+
-- dartls = {},
706+
},
710707
}
711708

712709
-- Ensure the servers and tools above are installed
@@ -722,20 +719,30 @@ require('lazy').setup({
722719
--
723720
-- You can add other tools here that you want Mason to install
724721
-- for you, so that they are available from within Neovim.
725-
local ensure_installed = vim.tbl_keys(servers or {})
722+
local ensure_installed = vim.tbl_keys(servers.mason or {})
726723
vim.list_extend(ensure_installed, {
727724
'stylua', -- Used to format Lua code
728-
'markdownlint', -- Used to format Markdown files
729725
})
730726
require('mason-tool-installer').setup { ensure_installed = ensure_installed }
731-
-- Installed LSPs are configured and enabled automatically with mason-lspconfig
732-
-- The loop below is for overriding the default configuration of LSPs with the ones in the servers table
733-
for server_name, config in pairs(servers) do
734-
vim.lsp.config(server_name, config)
727+
-- Either merge all additional server configs from the `servers.mason` and `servers.others` tables
728+
-- to the default language server configs as provided by nvim-lspconfig or
729+
-- define a custom server config that's unavailable on nvim-lspconfig.
730+
for server, config in pairs(vim.tbl_extend('keep', servers.mason, servers.others)) do
731+
if not vim.tbl_isempty(config) then
732+
vim.lsp.config(server, config)
733+
end
735734
end
736735

737-
-- NOTE: Some servers may require an old setup until they are updated. For the full list refer here: https://github.com/neovim/nvim-lspconfig/issues/3705
738-
-- These servers will have to be manually set up with require("lspconfig").server_name.setup{}
736+
-- After configuring our language servers, we now enable them
737+
---@type MasonLspconfigSettings
738+
require('mason-lspconfig').setup {
739+
ensure_installed = {}, -- explicitly set to an empty table (Kickstart populates installs via mason-tool-installer)
740+
automatic_enable = true, -- automatically run vim.lsp.enable() for all servers that are installed via Mason
741+
}
742+
-- Manually run vim.lsp.enable for all language servers that are *not* installed via Mason
743+
if not vim.tbl_isempty(servers.others) then
744+
vim.lsp.enable(vim.tbl_keys(servers.others))
745+
end
739746
end,
740747
},
741748

0 commit comments

Comments
 (0)