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