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    if nixCats('neonixdev') then
18      -- NOTE: Lazydev will make your Lua LSP stronger for Neovim config
19      -- NOTE: we are also using this as an opportunity to show you how to lazy load plugins!
20      -- This plugin was added to the optionalPlugins section of the main flake.nix of this repo.
21      -- Thus, it is not loaded and must be packadded.
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          -- NOTE: Use `:nixCats pawsible` to see the names of all plugins downloaded via Nix for packad.
27          vim.cmd.packadd('lazydev.nvim')
28          require('lazydev').setup({
29            library = {
30            --   -- See the configuration section for more details
31            --   -- Load luvit types when the `vim.uv` word is found
32            --   -- { path = "luvit-meta/library", words = { "vim%.uv" } },
33              -- adds type hints for nixCats global
34              { path = require('nixCats').nixCatsPath .. '/lua', words = { "nixCats" } },
35            },
36          })
37        end
38      })
39      -- NOTE: use BirdeeHub/lze to manage the autocommands for you if the above seems tedious.
40      -- Or, use the wrapper for lazy.nvim included in the luaUtils template.
41      -- NOTE: AFTER DIRECTORIES WILL NOT BE SOURCED BY PACKADD!!!!!
42      -- this must be done by you manually if,
43      -- for example, you wanted to lazy load nvim-cmp sources
44  
45      servers.lua_ls = {
46        settings = {
47          Lua = {
48            formatters = {
49              ignoreComments = true,
50            },
51            signatureHelp = { enabled = true },
52            diagnostics = {
53              globals = { 'nixCats' },
54              disable = { 'missing-fields' },
55            },
56          },
57          telemetry = { enabled = false },
58        },
59        filetypes = { 'lua' },
60      }
61    if require('nixCatsUtils').isNixCats then
62      -- nixd requires some configuration.
63      -- luckily, the nixCats plugin is here to pass whatever we need!
64      -- we passed this in via the `extra` table in our packageDefinitions
65      -- for additional configuration options, refer to:
66      -- https://github.com/nix-community/nixd/blob/main/nixd/docs/configuration.md
67      servers.nixd = {
68        settings = {
69          nixd = {
70            nixpkgs = {
71              -- in the extras set of your package definition:
72              -- nixdExtras.nixpkgs = ''import ${pkgs.path} {}''
73              expr = nixCats.extra("nixdExtras.nixpkgs") or [[import <nixpkgs> {}]],
74            },
75            options = {
76              -- If you integrated with your system flake,
77              -- you should use inputs.self as the path to your system flake
78              -- that way it will ALWAYS work, regardless
79              -- of where your config actually was.
80              nixos = {
81                -- nixdExtras.nixos_options = ''(builtins.getFlake "path:${builtins.toString inputs.self.outPath}").nixosConfigurations.configname.options''
82                expr = nixCats.extra("nixdExtras.nixos_options")
83              },
84              -- If you have your config as a separate flake, inputs.self would be referring to the wrong flake.
85              -- You can override the correct one into your package definition on import in your main configuration,
86              -- or just put an absolute path to where it usually is and accept the impurity.
87              ["home-manager"] = {
88                -- nixdExtras.home_manager_options = ''(builtins.getFlake "path:${builtins.toString inputs.self.outPath}").homeConfigurations.configname.options''
89                expr = nixCats.extra("nixdExtras.home_manager_options")
90              }
91            },
92            formatting = {
93              command = { "nixfmt" }
94            },
95            diagnostic = {
96              suppress = {
97                "sema-escaping-with"
98              }
99            }
100         }
101       }
102     }
103   else
104     servers.rnix = {}
105     servers.nil_ls = {}
106   end
107 
108   end
109 
110   if not require('nixCatsUtils').isNixCats and nixCats('lspDebugMode') then
111     vim.lsp.set_log_level("debug")
112   end
113 
114   -- This is this flake's version of what kickstarter has set up for mason handlers.
115   -- This is a convenience function that calls lspconfig on the LSPs we downloaded via nix
116   -- This will not download your LSP --- Nix does that.
117 
118   --  Add any additional override configuration in the following tables. They will be passed to
119   --  the `settings` field of the server config. You must look up that documentation yourself.
120   --  All of them are listed in https://github.com/neovim/nvim-lspconfig/blob/master/doc/configs.md
121   --
122   --  If you want to override the default filetypes that your language server will attach to you can
123   --  define the property 'filetypes' to the map in question.
124   --  You may do the same thing with cmd
125 
126   -- servers.clangd = {}
127   -- servers.gopls = {}
128   -- servers.pyright = {}
129   -- servers.rust_analyzer = {}
130   -- servers.tsserver = {}
131   -- servers.html = { filetypes = { 'html', 'twig', 'hbs'} }
132 
133 
134   -- If you were to comment out this autocommand
135   -- and instead pass the on attach function directly to
136   -- nvim-lspconfig, it would do the same thing.
137   vim.api.nvim_create_autocmd('LspAttach', {
138     group = vim.api.nvim_create_augroup('nixCats-lsp-attach', { clear = true }),
139     callback = function(event)
140       require('myLuaConf.LSPs.caps-on_attach').on_attach(vim.lsp.get_client_by_id(event.data.client_id), event.buf)
141     end
142   })
143   if require('nixCatsUtils').isNixCats then
144     for server_name, cfg in pairs(servers) do
145       require('lspconfig')[server_name].setup({
146         capabilities = require('myLuaConf.LSPs.caps-on_attach').get_capabilities(server_name),
147         -- this line is interchangeable with the above LspAttach autocommand
148         -- on_attach = require('myLuaConf.LSPs.caps-on_attach').on_attach,
149         settings = (cfg or {}).settings,
150         filetypes = (cfg or {}).filetypes,
151         cmd = (cfg or {}).cmd,
152         root_pattern = (cfg or {}).root_pattern,
153       })
154     end
155 
156   else
157     require('mason').setup()
158     local mason_lspconfig = require 'mason-lspconfig'
159     mason_lspconfig.setup {
160       ensure_installed = vim.tbl_keys(servers),
161     }
162     mason_lspconfig.setup_handlers {
163       function(server_name)
164         require('lspconfig')[server_name].setup {
165           capabilities = require('myLuaConf.LSPs.caps-on_attach').get_capabilities(server_name),
166           -- this line is interchangeable with the above LspAttach autocommand
167           -- on_attach = require('myLuaConf.LSPs.caps-on_attach').on_attach,
168           settings = (servers[server_name] or {}).settings,
169           filetypes = (servers[server_name] or {}).filetypes,
170         }
171       end,
172     }
173   end
174 
175 ---------------------------------------------------------------------------------
176                                                  nixCats.LSPs.caps-on_attach
177 
178 The above snippet mentions another file at the end.
179 
180 caps-on_attach.lua or 'myLuaConf.LSPs.caps-on_attach'
181 
182 It is simply a file containing the function we want to run on LSP attach, and a
183 function to return our completion capabilities object.
184 
185 It looks like this:
186 
187 
188   local M = {}
189   function M.on_attach(_, bufnr)
190     -- we create a function that lets us more easily define mappings specific
191     -- for LSP related items. It sets the mode, buffer and description for us each time.
192 
193     local nmap = function(keys, func, desc)
194       if desc then
195         desc = 'LSP: ' .. desc
196       end
197 
198       vim.keymap.set('n', keys, func, { buffer = bufnr, desc = desc })
199     end
200 
201     nmap('<leader>rn', vim.lsp.buf.rename, '[R]e[n]ame')
202     nmap('<leader>ca', vim.lsp.buf.code_action, '[C]ode [A]ction')
203 
204     nmap('gd', vim.lsp.buf.definition, '[G]oto [D]efinition')
205     nmap('gr', require('telescope.builtin').lsp_references, '[G]oto [R]eferences')
206     nmap('gI', require('telescope.builtin').lsp_implementations, '[G]oto [I]mplementation')
207     nmap('<leader>D', vim.lsp.buf.type_definition, 'Type [D]efinition')
208     nmap('<leader>ds', require('telescope.builtin').lsp_document_symbols, '[D]ocument [S]ymbols')
209     nmap('<leader>ws', require('telescope.builtin').lsp_dynamic_workspace_symbols, '[W]orkspace [S]ymbols')
210 
211     -- See `:help K` for why this keymap
212     nmap('K', vim.lsp.buf.hover, 'Hover Documentation')
213     nmap('<C-k>', vim.lsp.buf.signature_help, 'Signature Documentation')
214 
215     -- Lesser used LSP functionality
216     nmap('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration')
217     nmap('<leader>wa', vim.lsp.buf.add_workspace_folder, '[W]orkspace [A]dd Folder')
218     nmap('<leader>wr', vim.lsp.buf.remove_workspace_folder, '[W]orkspace [R]emove Folder')
219     nmap('<leader>wl', function()
220       print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
221     end, '[W]orkspace [L]ist Folders')
222 
223     -- Create a command `:Format` local to the LSP buffer
224     vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_)
225       vim.lsp.buf.format()
226     end, { desc = 'Format current buffer with LSP' })
227 
228   end
229 
230   function M.get_capabilities(server_name)
231     -- you may wish to know which server you're using to set different capabilities
232     -- if server_name is omitted, it will be nil
233 
234     -- nvim-cmp supports additional completion capabilities, so broadcast that to servers
235     -- if you make a package without it, make sure to check if it exists with nixCats!
236     local capabilities = vim.lsp.protocol.make_client_capabilities()
237     capabilities = vim.tbl_deep_extend('force', capabilities, require('cmp_nvim_lsp').default_capabilities())
238     capabilities.textDocument.completion.completionItem.snippetSupport = true
239     return capabilities
240   end
241   return M
242 
243 
244 
245 =================================================================================
246 vim:tw=78:ts=8:ft=help:norl: