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