]> src.twobees.de Git - dotfiles.git/blob - stow/nvim/.config/nvim/init.lua
up
[dotfiles.git] / stow / nvim / .config / nvim / init.lua
1 vim.g.mapleader = ' '
2 vim.g.maplocalleader = ' '
3
4 -- Install package manager
5 --    https://github.com/folke/lazy.nvim
6 --    `:help lazy.nvim.txt` for more info
7 local lazypath = vim.fn.stdpath 'data' .. '/lazy/lazy.nvim'
8 if not vim.loop.fs_stat(lazypath) then
9   vim.fn.system {
10     'git',
11     'clone',
12     '--filter=blob:none',
13     'https://github.com/folke/lazy.nvim.git',
14     '--branch=stable', -- latest stable release
15     lazypath,
16   }
17 end
18 vim.opt.rtp:prepend(lazypath)
19
20 require('lazy').setup({
21   -- NOTE: First, some plugins that don't require any configuration
22
23   'jose-elias-alvarez/null-ls.nvim',
24   'MunifTanjim/prettier.nvim',
25
26   '907th/vim-auto-save',
27
28   -- Git related plugins
29   'tpope/vim-fugitive',
30   'tpope/vim-rhubarb',
31
32   -- Detect tabstop and shiftwidth automatically
33   'tpope/vim-sleuth',
34
35
36   -- NOTE: This is where your plugins related to LSP can be installed.
37   --  The configuration is done below. Search for lspconfig to find it below.
38   {
39     -- LSP Configuration & Plugins
40     'neovim/nvim-lspconfig',
41     dependencies = {
42       -- Automatically install LSPs to stdpath for neovim
43       { 'williamboman/mason.nvim', config = true },
44       'williamboman/mason-lspconfig.nvim',
45
46       -- Useful status updates for LSP
47       -- NOTE: `opts = {}` is the same as calling `require('fidget').setup({})`
48       { 'j-hui/fidget.nvim',       opts = {},    tag = 'legacy' },
49
50       -- Additional lua configuration, makes nvim stuff amazing!
51       'folke/neodev.nvim',
52     },
53   },
54
55   {
56     -- Autocompletion
57     'hrsh7th/nvim-cmp',
58     dependencies = {
59       'hrsh7th/cmp-nvim-lsp',
60       { 'L3MON4D3/LuaSnip', version = "2.*", build = "make install_jsregexp" },
61       'saadparwaiz1/cmp_luasnip',
62       'rafamadriz/friendly-snippets'
63     },
64   },
65
66   -- Useful plugin to show you pending keybinds.
67   { 'folke/which-key.nvim',     opts = {} },
68   {
69     -- Adds git releated signs to the gutter, as well as utilities for managing changes
70     'lewis6991/gitsigns.nvim',
71     opts = {
72       -- See `:help gitsigns.txt`
73       signs = {
74         add = { text = '+' },
75         change = { text = '~' },
76         delete = { text = '_' },
77         topdelete = { text = '‾' },
78         changedelete = { text = '~' },
79       },
80       on_attach = function(bufnr)
81         vim.keymap.set('n', '[c', require('gitsigns').prev_hunk, { buffer = bufnr, desc = 'Go to Previous Hunk' })
82         vim.keymap.set('n', ']c', require('gitsigns').next_hunk, { buffer = bufnr, desc = 'Go to Next Hunk' })
83         vim.keymap.set('n', '<leader>ph', require('gitsigns').preview_hunk, { buffer = bufnr, desc = '[P]review [H]unk' })
84       end,
85     },
86   },
87
88   { "ellisonleao/gruvbox.nvim", priority = 1000 },
89   {
90     -- Set lualine as statusline
91     'nvim-lualine/lualine.nvim',
92     -- See `:help lualine.txt`
93     opts = {
94       options = {
95         icons_enabled = false,
96         theme = 'gruvbox',
97         component_separators = '|',
98         section_separators = '',
99       },
100     },
101   },
102
103   -- {  dir = '~/github/mynvimplugins/btoggle.nvim/' },
104   {  'tobser/btoggle.nvim' },
105
106   -- "gc" to comment visual regions/lines
107   { 'numToStr/Comment.nvim',         opts = {} },
108
109   -- Fuzzy Finder (files, lsp, etc)
110   { 'nvim-telescope/telescope.nvim', branch = '0.1.x', dependencies = { 'nvim-lua/plenary.nvim' } },
111
112   -- Fuzzy Finder Algorithm which requires local dependencies to be built.
113   -- Only load if `make` is available. Make sure you have the system
114   -- requirements installed.
115   {
116     'nvim-telescope/telescope-fzf-native.nvim',
117     -- NOTE: If you are having trouble with this installation,
118     --       refer to the README for telescope-fzf-native for more instructions.
119     build = 'make',
120     cond = function()
121       return vim.fn.executable 'make' == 1
122     end,
123   },
124
125   {
126     -- Highlight, edit, and navigate code
127     'nvim-treesitter/nvim-treesitter',
128     dependencies = {
129       'nvim-treesitter/nvim-treesitter-textobjects',
130     },
131     build = ':TSUpdate',
132   },
133
134   -- NOTE: Next Step on Your Neovim Journey: Add/Configure additional "plugins" for kickstart
135   --       These are some example plugins that I've included in the kickstart repository.
136   --       Uncomment any of the lines below to enable them.
137   -- require 'autoformat',
138   -- require 'kickstart.plugins.debug',
139
140   -- NOTE: The import below automatically adds your own plugins, configuration, etc from `lua/custom/plugins/*.lua`
141   --    You can use this folder to prevent any conflicts with this init.lua if you're interested in keeping
142   --    up-to-date with whatever is in the kickstart repo.
143   --
144   --    For additional information see: https://github.com/folke/lazy.nvim#-structuring-your-plugins
145   --
146   --    An additional note is that if you only copied in the `init.lua`, you can just comment this line
147   --    to get rid of the warning telling you that there are not plugins in `lua/custom/plugins/`.
148   -- { import = 'plugins' },
149 }, {})
150
151 -- [[ Setting options ]]
152 -- See `:help vim.o`
153
154 -- Set highlight on search
155 vim.o.hlsearch = true
156
157 -- Make line numbers default
158 vim.wo.relativenumber = true
159 vim.wo.number = true
160
161 -- Enable mouse mode
162 vim.o.mouse = 'a'
163
164 vim.opt.scrolloff = 8
165
166 -- Sync clipboard between OS and Neovim.
167 --  Remove this option if you want your OS clipboard to remain independent.
168 --  See `:help 'clipboard'`
169 vim.o.clipboard = 'unnamedplus'
170
171 -- Enable break indent
172 vim.o.breakindent = true
173
174 -- Save undo history
175 vim.o.undofile = true
176
177 -- Case insensitive searching UNLESS /C or capital in search
178 vim.o.ignorecase = true
179 vim.o.smartcase = true
180
181 -- Keep signcolumn on by default
182 vim.wo.signcolumn = 'yes'
183
184 -- Decrease update time
185 vim.o.updatetime = 250
186 vim.o.timeout = true
187 vim.o.timeoutlen = 300
188
189 -- Set completeopt to have a better completion experience
190 vim.o.completeopt = 'menuone,noselect'
191
192 -- NOTE: You should make sure your terminal supports this
193 vim.o.termguicolors = true
194
195 vim.o.background = "light" -- or "light" for light mode
196 vim.cmd([[colorscheme gruvbox]])
197
198 -- [[ Basic Keymaps ]]
199
200 -- Keymaps for better default experience
201 -- See `:help vim.keymap.set()`
202 vim.keymap.set({ 'n', 'v' }, '<Space>', '<Nop>', { silent = true })
203
204 -- Remap for dealing with word wrap
205 vim.keymap.set('n', 'k', "v:count == 0 ? 'gk' : 'k'", { expr = true, silent = true })
206 vim.keymap.set('n', 'j', "v:count == 0 ? 'gj' : 'j'", { expr = true, silent = true })
207
208 -- [[ Highlight on yank ]]
209 -- See `:help vim.highlight.on_yank()`
210 local highlight_group = vim.api.nvim_create_augroup('YankHighlight', { clear = true })
211 vim.api.nvim_create_autocmd('TextYankPost', {
212   callback = function()
213     vim.highlight.on_yank()
214   end,
215   group = highlight_group,
216   pattern = '*',
217 })
218
219 -- [[ Configure Telescope ]]
220 -- See `:help telescope` and `:help telescope.setup()`
221 require('telescope').setup {
222   defaults = {
223     mappings = {
224       i = {
225         ['<C-u>'] = false,
226         ['<C-d>'] = false,
227       },
228     },
229   },
230 }
231
232 -- Enable telescope fzf native, if installed
233 pcall(require('telescope').load_extension, 'fzf')
234
235 -- See `:help telescope.builtin`
236 vim.keymap.set('n', '<leader>?', require('telescope.builtin').oldfiles, { desc = '[?] Find recently opened files' })
237 vim.keymap.set('n', '<leader><space>', require('telescope.builtin').buffers, { desc = '[ ] Find existing buffers' })
238 vim.keymap.set('n', '<leader>/', function()
239   -- You can pass additional configuration to telescope to change theme, layout, etc.
240   require('telescope.builtin').current_buffer_fuzzy_find(require('telescope.themes').get_dropdown {
241     winblend = 10,
242     previewer = false,
243   })
244 end, { desc = '[/] Fuzzily search in current buffer' })
245
246 vim.keymap.set('n', '<leader>gf', require('telescope.builtin').git_files, { desc = 'Search [G]it [F]iles' })
247 vim.keymap.set('n', '<leader>sf', require('telescope.builtin').find_files, { desc = '[S]earch [F]iles' })
248 vim.keymap.set('n', '<leader>sh', require('telescope.builtin').help_tags, { desc = '[S]earch [H]elp' })
249 vim.keymap.set('n', '<leader>sw', require('telescope.builtin').grep_string, { desc = '[S]earch current [W]ord' })
250 vim.keymap.set('n', '<leader>sg', require('telescope.builtin').live_grep, { desc = '[S]earch by [G]rep' })
251 vim.keymap.set('n', '<leader>sd', require('telescope.builtin').diagnostics, { desc = '[S]earch [D]iagnostics' })
252
253 vim.keymap.set('i', 'kj', '<Esc>')
254 vim.keymap.set("n", "<tab>", "<cmd>bn<CR>")
255 vim.keymap.set("n", "<S-tab>", "<cmd>bp<CR>")
256
257 vim.cmd [[
258 augroup jumplast
259     autocmd BufReadPost *
260           \ if line("'\"") >= 1 && line("'\"") <= line("$") && &ft !~# 'commit'
261           \ |   exe "normal! g`\""
262           \ | endif
263 augroup END
264 ]]
265
266 -- [[ Configure Treesitter ]]
267 -- See `:help nvim-treesitter`
268 require('nvim-treesitter.configs').setup {
269   -- Add languages to be installed here that you want installed for treesitter
270   ensure_installed = { 'c', 'cpp', 'go', 'lua', 'python', 'rust', 'tsx', 'typescript', 'vimdoc', 'vim', 'html',
271     'c_sharp', 'perl', 'bash', 'python' ,'markdown' },
272
273   -- Autoinstall languages that are not installed. Defaults to false (but you can change for yourself!)
274   auto_install = false,
275
276   highlight = { enable = true },
277   indent = { enable = false, disable = { 'python' } },
278   incremental_selection = {
279     enable = true,
280     keymaps = {
281       init_selection = '<c-space>',
282       node_incremental = '<c-space>',
283       scope_incremental = '<c-s>',
284       node_decremental = '<M-space>',
285     },
286   },
287   textobjects = {
288     select = {
289       enable = true,
290       lookahead = true, -- Automatically jump forward to textobj, similar to targets.vim
291       keymaps = {
292         -- You can use the capture groups defined in textobjects.scm
293         ['aa'] = '@parameter.outer',
294         ['ia'] = '@parameter.inner',
295         ['af'] = '@function.outer',
296         ['if'] = '@function.inner',
297         ['ac'] = '@class.outer',
298         ['ic'] = '@class.inner',
299       },
300     },
301     move = {
302       enable = true,
303       set_jumps = true, -- whether to set jumps in the jumplist
304       goto_next_start = {
305         [']m'] = '@function.outer',
306         [']]'] = '@class.outer',
307       },
308       goto_next_end = {
309         [']M'] = '@function.outer',
310         [']['] = '@class.outer',
311       },
312       goto_previous_start = {
313         ['[m'] = '@function.outer',
314         ['[['] = '@class.outer',
315       },
316       goto_previous_end = {
317         ['[M'] = '@function.outer',
318         ['[]'] = '@class.outer',
319       },
320     },
321     swap = {
322       enable = true,
323       swap_previous = {
324         ['<leader>A'] = '@parameter.inner',
325       },
326       swap_next = {
327         ['<leader>a'] = '@parameter.inner',
328       },
329     },
330   },
331 }
332 -- Diagnostic keymaps
333 vim.keymap.set('n', '[d', vim.diagnostic.goto_prev, { desc = 'Go to previous diagnostic message' })
334 vim.keymap.set('n', ']d', vim.diagnostic.goto_next, { desc = 'Go to next diagnostic message' })
335 vim.keymap.set('n', '<S-F8>', vim.diagnostic.goto_prev, { desc = 'Go to previous diagnostic message' })
336 vim.keymap.set('n', '<F8>', vim.diagnostic.goto_next, { desc = 'Go to next diagnostic message' })
337 vim.keymap.set('n', '<leader>e', vim.diagnostic.open_float, { desc = 'Open floating diagnostic message' })
338 vim.keymap.set('n', '<leader>q', vim.diagnostic.setloclist, { desc = 'Open diagnostics list' })
339
340 vim.keymap.set("n", "<Leader>f", function()
341   vim.lsp.buf.format({ timeout = 10000, bufnr = vim.api.nvim_get_current_buf() })
342 end)
343 -- LSP settings.
344 --  This function gets run when an LSP connects to a particular buffer.
345 local on_attach = function(client, bufnr)
346   -- NOTE: Remember that lua is a real programming language, and as such it is possible
347   -- to define small helper and utility functions so you don't have to repeat yourself
348   -- many times.
349   -- for LSP related items. It sets the mode, buffer and description for us each time.
350   local nmap = function(keys, func, desc)
351     if desc then
352       desc = 'LSP: ' .. desc
353       vim.keymap.set('n', keys, func, { buffer = bufnr, desc = desc })
354     else
355       vim.keymap.set('n', keys, func, { buffer = bufnr })
356     end
357   end
358
359   nmap('<leader>rn', vim.lsp.buf.rename, '[R]e[n]ame')
360   nmap('<leader>ca', vim.lsp.buf.code_action, '[C]ode [A]ction')
361
362   nmap('gd', vim.lsp.buf.definition, '[G]oto [D]efinition')
363   nmap('gr', require('telescope.builtin').lsp_references, '[G]oto [R]eferences')
364   nmap('gI', vim.lsp.buf.implementation, '[G]oto [I]mplementation')
365   nmap('<leader>D', vim.lsp.buf.type_definition, 'Type [D]efinition')
366   nmap('<leader>ds', require('telescope.builtin').lsp_document_symbols, '[D]ocument [S]ymbols')
367   nmap('<leader>ws', require('telescope.builtin').lsp_dynamic_workspace_symbols, '[W]orkspace [S]ymbols')
368
369   -- See `:help K` for why this keymap
370   nmap('K', vim.lsp.buf.hover, 'Hover Documentation')
371   nmap('<C-k>', vim.lsp.buf.signature_help, 'Signature Documentation')
372
373   -- Lesser used LSP functionality
374   nmap('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration')
375   nmap('<leader>wa', vim.lsp.buf.add_workspace_folder, '[W]orkspace [A]dd Folder')
376   nmap('<leader>wr', vim.lsp.buf.remove_workspace_folder, '[W]orkspace [R]emove Folder')
377   nmap('<leader>wl', function()
378     print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
379   end, '[W]orkspace [L]ist Folders')
380
381   -- Create a command `:Format` local to the LSP buffer
382   vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_)
383     vim.lsp.buf.format()
384   end, { desc = 'Format current buffer with LSP' })
385 end
386
387 -- Enable the following language servers
388 --  Feel free to add/remove any LSPs that you want here. They will automatically be installed.
389 --
390 --  Add any additional override configuration in the following tables. They will be passed to
391 --  the `settings` field of the server config. You must look up that documentation yourself.
392 local servers = {
393   -- clangd = {},
394   -- gopls = {},
395   -- pyright = {},
396   -- rust_analyzer = {},
397   tsserver = {},
398   eslint = {},
399   tailwindcss = {},
400   lua_ls = {
401     Lua = {
402       workspace = { checkThirdParty = false },
403       telemetry = { enable = false },
404     },
405   },
406 }
407
408 -- Setup neovim lua configuration
409 require('neodev').setup()
410
411 -- nvim-cmp supports additional completion capabilities, so broadcast that to servers
412 local capabilities = vim.lsp.protocol.make_client_capabilities()
413 capabilities = require('cmp_nvim_lsp').default_capabilities(capabilities)
414
415 -- Ensure the servers above are installed
416 local mason_lspconfig = require 'mason-lspconfig'
417
418 mason_lspconfig.setup {
419   ensure_installed = vim.tbl_keys(servers),
420 }
421
422 mason_lspconfig.setup_handlers {
423   function(server_name)
424     require('lspconfig')[server_name].setup {
425       capabilities = capabilities,
426       on_attach = on_attach,
427       settings = servers[server_name],
428     }
429   end,
430 }
431
432 -- require('lspconfig').perlls.setup {
433 --   settings = {
434 --     perl = {
435 --       perlInc = {
436 --         '/home/pi/git/misterhouse/lib',
437 --         '/home/pi/git/misterhouse/bin',
438 --       },
439 --     },
440 --   },
441 --   capabilities = capabilities,
442 --   on_attach = on_attach,
443 -- }
444 --
445 require('lspconfig').perlpls.setup {
446   on_attach = on_attach,
447   settings = {
448     perl = {
449       perlcritic = {
450         enabled = false
451       },
452       syntax = {
453         enabled = true
454       },
455       inc = {
456         '/home/pi/git/misterhouse/lib',
457         '/home/pi/git/misterhouse/bin',
458       },
459     },
460   },
461   capabilities = capabilities,
462 }
463
464 -- nvim-cmp setup
465 local cmp = require 'cmp'
466 local luasnip = require 'luasnip'
467 require('luasnip.loaders.from_vscode').lazy_load()
468 luasnip.config.setup {}
469
470 cmp.setup {
471   snippet = {
472     expand = function(args)
473       luasnip.lsp_expand(args.body)
474     end,
475   },
476   mapping = cmp.mapping.preset.insert {
477     ['<C-n>'] = cmp.mapping.select_next_item(),
478     ['<C-p>'] = cmp.mapping.select_prev_item(),
479     ['<C-d>'] = cmp.mapping.scroll_docs(-4),
480     ['<C-f>'] = cmp.mapping.scroll_docs(4),
481     ['<C-Space>'] = cmp.mapping.complete {},
482     ['<CR>'] = cmp.mapping.confirm {
483       behavior = cmp.ConfirmBehavior.Replace,
484       select = true,
485     },
486     ['<Tab>'] = cmp.mapping(function(fallback)
487       if cmp.visible() then
488         cmp.select_next_item()
489       elseif luasnip.expand_or_locally_jumpable() then
490         luasnip.expand_or_jump()
491       else
492         fallback()
493       end
494     end, { 'i', 's' }),
495     ['<S-Tab>'] = cmp.mapping(function(fallback)
496       if cmp.visible() then
497         cmp.select_prev_item()
498       elseif luasnip.locally_jumpable(-1) then
499         luasnip.jump(-1)
500       else
501         fallback()
502       end
503     end, { 'i', 's' }),
504   },
505   sources = {
506     { name = 'nvim_lsp' },
507     { name = 'luasnip' },
508   },
509 }
510
511
512 local null_ls = require("null-ls")
513
514 local group = vim.api.nvim_create_augroup("lsp_format_on_save", { clear = false })
515 local event = "BufWritePre" -- or "BufWritePost"
516 local async = event == "BufWritePost"
517
518 null_ls.setup({
519   timeout = 2000,
520   on_attach = on_attach
521 })
522
523
524 local prettier = require("prettier")
525
526 prettier.setup({
527   bin = 'prettierd', -- or `'prettierd'` (v0.23.3+)
528   filetypes = {
529     "css",
530     "graphql",
531     "html",
532     "javascript",
533     "javascriptreact",
534     "json",
535     "less",
536     "perl",
537     "markdown",
538     "scss",
539     "typescript",
540     "typescriptreact",
541     "yaml",
542   },
543 })
544
545 vim.opt.diffopt = vim.opt.diffopt + "vertical"
546 vim.opt.cursorline = true
547 vim.opt.hlsearch = true
548
549 local bt =  require("btoggle");
550 bt.setup({
551   ["true"] = "false",
552   ["True"] = "False",
553   ["False"] = "True",
554   ["false"] = "true",
555   ["foo"] = "bar",
556   ["bar"] = "bor",
557   ["bor"] = "foo"
558 })
559 vim.keymap.set('n', '<leader>b', bt.toggle)
560
561 -- The line beneath this is called `modeline`. See `:help modeline`
562 -- vim: ts=2 sts=2 sw=2 et