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