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      -- NOTE: Lazydev will make your Lua LSP stronger for Neovim config
20      -- NOTE: we are also using this as an opportunity to show you how to lazy load plugins!
21      -- This plugin was added to the optionalPlugins section of the main flake.nix of this repo.
22      -- Thus, it is not loaded and must be packadded.
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          -- NOTE: Use `:nixCats pawsible` to see the names of all plugins downloaded via Nix for packad.
28          vim.cmd.packadd('lazydev.nvim')
29          require('lazydev').setup({
30            library = {
31            --   -- See the configuration section for more details
32            --   -- Load luvit types when the `vim.uv` word is found
33            --   -- { path = "luvit-meta/library", words = { "vim%.uv" } },
34              -- adds type hints for nixCats global
35              { path = require('nixCats').nixCatsPath .. '/lua', words = { "nixCats" } },
36            },
37          })
38        end
39      })
40      -- NOTE: use BirdeeHub/lze to manage the autocommands for you if the above seems tedious.
41      -- Or, use the wrapper for lazy.nvim included in the luaUtils template.
42      -- NOTE: AFTER DIRECTORIES WILL NOT BE SOURCED BY PACKADD!!!!!
43      -- this must be done by you manually if,
44      -- for example, you wanted to lazy load nvim-cmp sources
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      -- nixd requires some configuration.
64      -- luckily, the nixCats plugin is here to pass whatever we need!
65      -- we passed this in via the `extra` table in our packageDefinitions
66      -- for additional configuration options, refer to:
67      -- https://github.com/nix-community/nixd/blob/main/nixd/docs/configuration.md
68      servers.nixd = {
69        settings = {
70          nixd = {
71            nixpkgs = {
72              -- in the extras set of your package definition:
73              -- nixdExtras.nixpkgs = ''import ${pkgs.path} {}''
74              expr = nixCats.extra("nixdExtras.nixpkgs") or [[import <nixpkgs> {}]],
75            },
76            options = {
77              -- If you integrated with your system flake,
78              -- you should use inputs.self as the path to your system flake
79              -- that way it will ALWAYS work, regardless
80              -- of where your config actually was.
81              nixos = {
82                -- nixdExtras.nixos_options = ''(builtins.getFlake "path:${builtins.toString inputs.self.outPath}").nixosConfigurations.configname.options''
83                expr = nixCats.extra("nixdExtras.nixos_options")
84              },
85              -- If you have your config as a separate flake, inputs.self would be referring to the wrong flake.
86              -- You can override the correct one into your package definition on import in your main configuration,
87              -- or just put an absolute path to where it usually is and accept the impurity.
88              ["home-manager"] = {
89                -- nixdExtras.home_manager_options = ''(builtins.getFlake "path:${builtins.toString inputs.self.outPath}").homeConfigurations.configname.options''
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   -- This is this flake's version of what kickstarter has set up for mason handlers.
116   -- This is a convenience function that calls lspconfig on the LSPs we downloaded via nix
117   -- This will not download your LSP --- Nix does that.
118 
119   --  Add any additional override configuration in the following tables. They will be passed to
120   --  the `settings` field of the server config. You must look up that documentation yourself.
121   --  All of them are listed in https://github.com/neovim/nvim-lspconfig/blob/master/doc/configs.md
122   --
123   --  If you want to override the default filetypes that your language server will attach to you can
124   --  define the property 'filetypes' to the map in question.
125   --  You may do the same thing with cmd
126 
127   -- servers.clangd = {}
128   -- servers.gopls = {}
129   -- servers.pyright = {}
130   -- servers.rust_analyzer = {}
131   -- servers.tsserver = {}
132   -- servers.html = { filetypes = { 'html', 'twig', 'hbs'} }
133 
134 
135   -- If you were to comment out this autocommand
136   -- and instead pass the on attach function directly to
137   -- nvim-lspconfig, it would do the same thing.
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         -- this line is interchangeable with the above LspAttach autocommand
149         -- on_attach = require('myLuaConf.LSPs.caps-on_attach').on_attach,
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           -- this line is interchangeable with the above LspAttach autocommand
168           -- on_attach = require('myLuaConf.LSPs.caps-on_attach').on_attach,
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     -- we create a function that lets us more easily define mappings specific
192     -- for LSP related items. It sets the mode, buffer and description for us each time.
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     -- See `:help K` for why this keymap
213     nmap('K', vim.lsp.buf.hover, 'Hover Documentation')
214     nmap('<C-k>', vim.lsp.buf.signature_help, 'Signature Documentation')
215 
216     -- Lesser used LSP functionality
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     -- Create a command `:Format` local to the LSP buffer
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     -- you may wish to know which server you're using to set different capabilities
233     -- if server_name is omitted, it will be nil
234 
235     -- nvim-cmp supports additional completion capabilities, so broadcast that to servers
236     -- if you make a package without it, make sure to check if it exists with nixCats!
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: