]> git.rmz.io Git - dotfiles.git/blob - nvim/lua/plugins/lsp/init.lua
lazyvim: absorb lspconfig
[dotfiles.git] / nvim / lua / plugins / lsp / init.lua
1 return {
2 { "neovim/nvim-lspconfig",
3 event = "LazyFile",
4 dependencies = {
5 "mason.nvim",
6 { "williamboman/mason-lspconfig.nvim", config = function() end }, -- don't configure yet
7 },
8 ---@class PluginLspOpts
9 opts = {
10 ---@type vim.diagnostic.Opts
11 diagnostics = {
12 underline = true,
13 update_in_insert = false,
14 virtual_text = {
15 spacing = 4,
16 source = "if_many",
17 prefix = "●",
18 },
19 severity_sort = true,
20 signs = {
21 text = {
22 [vim.diagnostic.severity.ERROR] = " ",
23 [vim.diagnostic.severity.WARN] = " ",
24 [vim.diagnostic.severity.HINT] = " ",
25 [vim.diagnostic.severity.INFO] = " ",
26 },
27 },
28 },
29 inlay_hints = { enabled = false, },
30 codelens = { enabled = false, },
31 document_highlight = { enabled = true, },
32 capabilities = {
33 workspace = {
34 fileOperations = {
35 didRename = true,
36 willRename = true,
37 },
38 },
39 },
40 format = {
41 formatting_options = nil,
42 timeout_ms = nil,
43 },
44 -- LSP Server Settings
45 ---@type lspconfig.options
46 servers = {
47 lua_ls = {
48 settings = {
49 Lua = {
50 workspace = { checkThirdParty = false, },
51 codeLens = { enable = true, },
52 completion = { callSnippet = "Replace", },
53 doc = { privateName = { "^_" }, },
54 hint = {
55 enable = true,
56 setType = false,
57 paramType = true,
58 paramName = "Disable",
59 semicolon = "Disable",
60 arrayIndex = "Disable",
61 },
62 diagnostics = {
63 disable = { "missing-fields", },
64 },
65 },
66 },
67 },
68 -- TODO: Add clangd extensions
69 -- https://github.com/p00f/clangd_extensions.nvim
70 },
71 -- you can do any additional lsp server setup here
72 -- return true if you don't want this server to be setup with lspconfig
73 ---@type table<string, fun(server:string, opts:_.lspconfig.options):boolean?>
74 setup = {
75 -- example to setup with typescript.nvim
76 -- tsserver = function(_, opts)
77 -- require("typescript").setup({ server = opts })
78 -- return true
79 -- end,
80 -- Specify * to use this function as a fallback for any server
81 -- ["*"] = function(server, opts) end,
82 },
83 },
84 ---@param opts PluginLspOpts
85 config = function(_, opts)
86 local lspconfig = require('lspconfig')
87 for server, config in pairs(opts.servers) do
88 local capabilities = vim.tbl_deep_extend("force",
89 vim.lsp.protocol.make_client_capabilities() or {},
90 require('blink.cmp').get_lsp_capabilities() or {},
91 config.capabilities or {}
92 )
93 config.capabilities = capabilities
94 lspconfig[server].setup(config)
95 end
96
97 -- setup keymaps
98 LazyVim.lsp.on_attach(function(client, buffer)
99 require("plugins.lsp.keymaps").on_attach(client, buffer)
100 end)
101
102 LazyVim.lsp.setup()
103 LazyVim.lsp.on_dynamic_capability(require("plugins.lsp.keymaps").on_attach)
104 -- TODO: should vim.diagnostics be condigured directly?
105 vim.diagnostic.config(vim.deepcopy(opts.diagnostics))
106
107 -- get all the servers that are available through mason-lspconfig
108 local have_mason, mlsp = pcall(require, "mason-lspconfig")
109 local all_mslp_servers = {}
110 if have_mason then
111 all_mslp_servers = vim.tbl_keys(require("mason-lspconfig.mappings.server").lspconfig_to_package)
112 end
113
114 local ensure_installed = {} ---@type string[]
115 for server, server_opts in pairs(opts.servers) do
116 if server_opts then
117 server_opts = server_opts == true and {} or server_opts
118 if server_opts.enabled ~= false then
119 -- run manual setup if mason=false or if this is a server that cannot be installed with mason-lspconfig
120 if server_opts.mason == false or not vim.tbl_contains(all_mslp_servers, server) then
121 require("lspconfig")[server].setup(server_opts)
122 else
123 ensure_installed[#ensure_installed + 1] = server
124 end
125 end
126 end
127 end
128
129 if have_mason then
130 mlsp.setup({
131 ensure_installed = vim.tbl_deep_extend(
132 "force",
133 ensure_installed,
134 LazyVim.opts("mason-lspconfig.nvim").ensure_installed or {}
135 ),
136 handlers = { setup },
137 })
138 end
139 end
140 },
141 { "williamboman/mason.nvim",
142 cmd = "Mason",
143 keys = { { "<leader>cm", "<cmd>Mason<cr>", desc = "Mason" } },
144 build = ":MasonUpdate",
145 opts_extend = { "ensure_installed" },
146 opts = {
147 ensure_installed = {
148 "stylua",
149 "shfmt",
150 },
151 },
152 ---@param opts MasonSettings | {ensure_installed: string[]}
153 config = function(_, opts)
154 require("mason").setup(opts)
155 local mr = require("mason-registry")
156 mr:on("package:install:success", function()
157 vim.defer_fn(function()
158 -- trigger FileType event to possibly load this newly installed LSP server
159 require("lazy.core.handler.event").trigger({
160 event = "FileType",
161 buf = vim.api.nvim_get_current_buf(),
162 })
163 end, 100)
164 end)
165
166 mr.refresh(function()
167 for _, tool in ipairs(opts.ensure_installed) do
168 local p = mr.get_package(tool)
169 if not p:is_installed() then
170 p:install()
171 end
172 end
173 end)
174 end,
175 },
176 }