Skip to content

Commit 3436e0e

Browse files
kartikvashisthaoriori1703
authored andcommitted
feat: improved lsp setup for nvim > 0.11
1 parent e244058 commit 3436e0e

1 file changed

Lines changed: 87 additions & 47 deletions

File tree

init.lua

Lines changed: 87 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
--[[
2-
32
=====================================================================
43
==================== READ THIS BEFORE CONTINUING ====================
54
=====================================================================
@@ -601,52 +600,76 @@ require('lazy').setup({
601600
end,
602601
})
603602

604-
-- Enable the following language servers
605-
-- Feel free to add/remove any LSPs that you want here. They will automatically be installed.
606-
-- See `:help lsp-config` for information about keys and how to configure
607-
---@type table<string, vim.lsp.Config>
603+
-- LSP servers and clients are able to communicate to each other what features they support.
604+
-- By default, Neovim doesn't support everything that is in the LSP specification.
605+
-- When you add blink.cmp, luasnip, etc. Neovim now has *more* capabilities.
606+
-- So, we create new capabilities with blink.cmp, and then broadcast that to the servers.
607+
-- NOTE: The following line is now commented as blink.cmp extends capabilites by default from its internal code:
608+
-- https://github.com/Saghen/blink.cmp/blob/102db2f5996a46818661845cf283484870b60450/plugin/blink-cmp.lua
609+
-- It has been left here as a comment for educational purposes (as the predecessor completion plugin required this explicit step).
610+
--
611+
-- local capabilities = require("blink.cmp").get_lsp_capabilities()
612+
613+
-- Language servers can broadly be installed in the following ways:
614+
-- 1) via the mason package manager; or
615+
-- 2) via your system's package manager; or
616+
-- 3) via a release binary from a language server's repo that's accessible somewhere on your system.
617+
--
608618
local servers = {
609-
-- clangd = {},
610-
-- gopls = {},
611-
-- pyright = {},
612-
-- rust_analyzer = {},
619+
-- Add any additional override configuration in any of the following tables. Available keys are:
620+
-- - cmd (table): Override the default command used to start the server
621+
-- - filetypes (table): Override the default list of associated filetypes for the server
622+
-- - capabilities (table): Override fields in capabilities. Can be used to disable certain LSP features.
623+
-- - settings (table): Override the default settings passed when initializing the server.
624+
-- For example, to see the options for `lua_ls`, you could go to: https://luals.github.io/wiki/settings/
613625
--
614-
-- Some languages (like typescript) have entire language plugins that can be useful:
615-
-- https://github.com/pmizio/typescript-tools.nvim
616-
--
617-
-- But for many setups, the LSP (`ts_ls`) will work just fine
618-
-- ts_ls = {},
619-
620-
stylua = {}, -- Used to format Lua code
621-
622-
-- Special Lua Config, as recommended by neovim help docs
623-
lua_ls = {
624-
on_init = function(client)
625-
if client.workspace_folders then
626-
local path = client.workspace_folders[1].name
627-
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
628-
end
629-
630-
client.config.settings.Lua = vim.tbl_deep_extend('force', client.config.settings.Lua, {
631-
runtime = {
632-
version = 'LuaJIT',
633-
path = { 'lua/?.lua', 'lua/?/init.lua' },
634-
},
635-
workspace = {
636-
checkThirdParty = false,
637-
-- NOTE: this is a lot slower and will cause issues when working on your own configuration.
638-
-- See https://github.com/neovim/nvim-lspconfig/issues/3189
639-
library = vim.tbl_extend('force', vim.api.nvim_get_runtime_file('', true), {
640-
'${3rd}/luv/library',
641-
'${3rd}/busted/library',
642-
}),
643-
},
644-
})
645-
end,
646-
settings = {
647-
Lua = {},
626+
-- Feel free to add/remove any LSPs here that you want to install via Mason. They will automatically be installed and setup.
627+
mason = {
628+
-- clangd = {},
629+
-- gopls = {},
630+
-- pyright = {},
631+
-- rust_analyzer = {},
632+
-- ... etc. See `:help lspconfig-all` for a list of all the pre-configured LSPs
633+
--
634+
-- Some languages (like typescript) have entire language plugins that can be useful:
635+
-- https://github.com/pmizio/typescript-tools.nvim
636+
--
637+
-- But for many setups, the LSP (`ts_ls`) will work just fine
638+
-- ts_ls = {},
639+
--
640+
lua_ls = {
641+
on_init = function(client)
642+
if client.workspace_folders then
643+
local path = client.workspace_folders[1].name
644+
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
645+
end
646+
647+
client.config.settings.Lua = vim.tbl_deep_extend('force', client.config.settings.Lua, {
648+
runtime = {
649+
version = 'LuaJIT',
650+
path = { 'lua/?.lua', 'lua/?/init.lua' },
651+
},
652+
workspace = {
653+
checkThirdParty = false,
654+
-- NOTE: this is a lot slower and will cause issues when working on your own configuration.
655+
-- See https://github.com/neovim/nvim-lspconfig/issues/3189
656+
library = vim.tbl_extend('force', vim.api.nvim_get_runtime_file('', true), {
657+
'${3rd}/luv/library',
658+
'${3rd}/busted/library',
659+
}),
660+
},
661+
})
662+
end,
663+
settings = {
664+
Lua = {},
665+
},
648666
},
649667
},
668+
-- This table contains config for all language servers that are *not* installed via Mason.
669+
-- Structure is identical to the mason table from above.
670+
others = {
671+
-- dartls = {},
672+
},
650673
}
651674

652675
-- Ensure the servers and tools above are installed
@@ -656,17 +679,34 @@ require('lazy').setup({
656679
-- :Mason
657680
--
658681
-- You can press `g?` for help in this menu.
659-
local ensure_installed = vim.tbl_keys(servers or {})
682+
--
683+
-- `mason` had to be setup earlier: to configure its options see the
684+
-- `dependencies` table for `nvim-lspconfig` above.
685+
--
686+
-- You can add other tools here that you want Mason to install
687+
-- for you, so that they are available from within Neovim.
688+
local ensure_installed = vim.tbl_keys(servers.mason or {})
660689
vim.list_extend(ensure_installed, {
661690
-- You can add other tools here that you want Mason to install
662691
})
663692

664693
require('mason-tool-installer').setup { ensure_installed = ensure_installed }
665694

666-
for name, server in pairs(servers) do
667-
vim.lsp.config(name, server)
668-
vim.lsp.enable(name)
695+
-- Either merge all additional server configs from the `servers.mason` and `servers.others` tables
696+
-- to the default language server configs as provided by nvim-lspconfig or
697+
-- define a custom server config that's unavailable on nvim-lspconfig.
698+
for server, config in pairs(vim.tbl_extend('keep', servers.mason, servers.others)) do
699+
if vim.fn.empty(config) ~= 1 then vim.lsp.config(server, config) end
669700
end
701+
702+
-- After configuring our language servers, we now enable them
703+
require('mason-lspconfig').setup {
704+
ensure_installed = {}, -- explicitly set to an empty table (Kickstart populates installs via mason-tool-installer)
705+
automatic_enable = true, -- automatically run vim.lsp.enable() for all servers that are installed via Mason
706+
}
707+
708+
-- Manually run vim.lsp.enable for all language servers that are *not* installed via Mason
709+
if vim.fn.empty(servers.others) ~= 1 then vim.lsp.enable(vim.tbl_keys(servers.others)) end
670710
end,
671711
},
672712

0 commit comments

Comments
 (0)