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