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