1 =================================================================================
2 MASON AND LSPCONFIG nixCats.LSPs
3 nixCats.LSPs.mason
4
5 This is a method you may use to make sure mason only tries to download stuff
6 when you did not install Neovim via nixCats
7
8 It functions the same as what kickstart.nvim does for its mason setup.
9 However, when loaded via Nix,
10 it skips mason and passes them straight to nvim-lspconfig
11
12 When installing via paq-nvim, install mason via paq-nvim. Then wherever you set up
13 mason, do this.
14 The stuff added to servers table is what is passed to lspconfig/mason
15
16
17 local servers = {}
18 if nixCats('neonixdev') then
19
20
21
22
23 vim.api.nvim_create_autocmd('FileType', {
24 group = vim.api.nvim_create_augroup('nixCats-lazydev', { clear = true }),
25 pattern = { 'lua' },
26 callback = function(event)
27
28 vim.cmd.packadd('lazydev.nvim')
29 require('lazydev').setup({
30 library = {
31
32
33
34
35 { path = require('nixCats').nixCatsPath .. '/lua', words = { "nixCats" } },
36 },
37 })
38 end
39 })
40
41
42
43
44
45
46 servers.lua_ls = {
47 settings = {
48 Lua = {
49 formatters = {
50 ignoreComments = true,
51 },
52 signatureHelp = { enabled = true },
53 diagnostics = {
54 globals = { 'nixCats' },
55 disable = { 'missing-fields' },
56 },
57 },
58 telemetry = { enabled = false },
59 },
60 filetypes = { 'lua' },
61 }
62 if require('nixCatsUtils').isNixCats then
63
64
65
66
67
68 servers.nixd = {
69 settings = {
70 nixd = {
71 nixpkgs = {
72
73
74 expr = nixCats.extra("nixdExtras.nixpkgs") or [[import <nixpkgs> {}]],
75 },
76 options = {
77
78
79
80
81 nixos = {
82
83 expr = nixCats.extra("nixdExtras.nixos_options")
84 },
85
86
87
88 ["home-manager"] = {
89
90 expr = nixCats.extra("nixdExtras.home_manager_options")
91 }
92 },
93 formatting = {
94 command = { "nixfmt" }
95 },
96 diagnostic = {
97 suppress = {
98 "sema-escaping-with"
99 }
100 }
101 }
102 }
103 }
104 else
105 servers.rnix = {}
106 servers.nil_ls = {}
107 end
108
109 end
110
111 if not require('nixCatsUtils').isNixCats and nixCats('lspDebugMode') then
112 vim.lsp.set_log_level("debug")
113 end
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138 vim.api.nvim_create_autocmd('LspAttach', {
139 group = vim.api.nvim_create_augroup('nixCats-lsp-attach', { clear = true }),
140 callback = function(event)
141 require('myLuaConf.LSPs.caps-on_attach').on_attach(vim.lsp.get_client_by_id(event.data.client_id), event.buf)
142 end
143 })
144 if require('nixCatsUtils').isNixCats then
145 for server_name, cfg in pairs(servers) do
146 require('lspconfig')[server_name].setup({
147 capabilities = require('myLuaConf.LSPs.caps-on_attach').get_capabilities(server_name),
148
149
150 settings = (cfg or {}).settings,
151 filetypes = (cfg or {}).filetypes,
152 cmd = (cfg or {}).cmd,
153 root_pattern = (cfg or {}).root_pattern,
154 })
155 end
156
157 else
158 require('mason').setup()
159 local mason_lspconfig = require 'mason-lspconfig'
160 mason_lspconfig.setup {
161 ensure_installed = vim.tbl_keys(servers),
162 }
163 mason_lspconfig.setup_handlers {
164 function(server_name)
165 require('lspconfig')[server_name].setup {
166 capabilities = require('myLuaConf.LSPs.caps-on_attach').get_capabilities(server_name),
167
168
169 settings = (servers[server_name] or {}).settings,
170 filetypes = (servers[server_name] or {}).filetypes,
171 }
172 end,
173 }
174 end
175
176 ---------------------------------------------------------------------------------
177 nixCats.LSPs.caps-on_attach
178
179 The above snippet mentions another file at the end.
180
181 caps-on_attach.lua or 'myLuaConf.LSPs.caps-on_attach'
182
183 It is simply a file containing the function we want to run on LSP attach, and a
184 function to return our completion capabilities object.
185
186 It looks like this:
187
188
189 local M = {}
190 function M.on_attach(_, bufnr)
191
192
193
194 local nmap = function(keys, func, desc)
195 if desc then
196 desc = 'LSP: ' .. desc
197 end
198
199 vim.keymap.set('n', keys, func, { buffer = bufnr, desc = desc })
200 end
201
202 nmap('<leader>rn', vim.lsp.buf.rename, '[R]e[n]ame')
203 nmap('<leader>ca', vim.lsp.buf.code_action, '[C]ode [A]ction')
204
205 nmap('gd', vim.lsp.buf.definition, '[G]oto [D]efinition')
206 nmap('gr', require('telescope.builtin').lsp_references, '[G]oto [R]eferences')
207 nmap('gI', require('telescope.builtin').lsp_implementations, '[G]oto [I]mplementation')
208 nmap('<leader>D', vim.lsp.buf.type_definition, 'Type [D]efinition')
209 nmap('<leader>ds', require('telescope.builtin').lsp_document_symbols, '[D]ocument [S]ymbols')
210 nmap('<leader>ws', require('telescope.builtin').lsp_dynamic_workspace_symbols, '[W]orkspace [S]ymbols')
211
212
213 nmap('K', vim.lsp.buf.hover, 'Hover Documentation')
214 nmap('<C-k>', vim.lsp.buf.signature_help, 'Signature Documentation')
215
216
217 nmap('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration')
218 nmap('<leader>wa', vim.lsp.buf.add_workspace_folder, '[W]orkspace [A]dd Folder')
219 nmap('<leader>wr', vim.lsp.buf.remove_workspace_folder, '[W]orkspace [R]emove Folder')
220 nmap('<leader>wl', function()
221 print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
222 end, '[W]orkspace [L]ist Folders')
223
224
225 vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_)
226 vim.lsp.buf.format()
227 end, { desc = 'Format current buffer with LSP' })
228
229 end
230
231 function M.get_capabilities(server_name)
232
233
234
235
236
237 local capabilities = vim.lsp.protocol.make_client_capabilities()
238 capabilities = vim.tbl_deep_extend('force', capabilities, require('cmp_nvim_lsp').default_capabilities())
239 capabilities.textDocument.completion.completionItem.snippetSupport = true
240 return capabilities
241 end
242 return M
243
244
245
246 =================================================================================
247 vim:tw=78:ts=8:ft=help:norl: