summaryrefslogtreecommitdiffstats
path: root/dotfiles/vim/lua/myConfig.lua
blob: 65753ea87b6981b97354466038aa0b68d71e4813 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
-- Enables byte-compiled, cached Lua module loading
vim.loader.enable()

-- Options
----------

vim.o.undofile = true
vim.o.backup = true
vim.opt.backupdir:remove "."

vim.opt.shortmess:append "A"
vim.opt.shortmess:append "c"

vim.opt.diffopt:append "linematch:60"

vim.o.mouse = "a"

vim.o.ignorecase = true
vim.o.smartcase = true

vim.o.smartindent = true
-- TODO: check that
vim.o.cindent = true

-- tabstop and shiftwidth are also set locally by individual filetypes

vim.o.tabstop = 4
vim.o.shiftwidth = 4

vim.o.inccommand = "split"

vim.o.scrolloff = 1
vim.o.sidescrolloff = 5

vim.o.colorcolumn = "80"
vim.o.cursorline = true

vim.o.title = true

vim.opt.wildmode = { "longest:full", "full" }
vim.opt.completeopt = { "menu", "menuone", "preview", "noinsert", "noselect" }

-- Use ripgrep
vim.o.grepprg = vim.g.ripgrep_path .. " --vimgrep --smart-case"
vim.o.grepformat = "%f:%l:%c:%m," .. vim.o.grepformat

vim.o.termguicolors = true
vim.o.background = "dark"

vim.o.updatetime = 1000

-- Mode already shown by the status line
vim.o.showmode = false

vim.opt.listchars = {
	extends = ">",
	nbsp = "+",
	precedes = "<",
	tab = "  ",
	trail = "-",
}
vim.wo.list = true

-- Polyglot
-----------

vim.g.polyglot_disabled = {
	"ftdetect",
	"autoindent",
	"sensible",
}

-- Leaders
----------

vim.g.maplocalleader = ","
vim.g.mapleader = ";"

-- Misc
-------

-- From neovim#14420
-- Restores the position of previously opened files

-- Inspired by: https://github.com/ethanholz/nvim-lastplace

local last_cursor_pos_augroup = vim.api.nvim_create_augroup("LastCursorPos", {})
vim.api.nvim_create_autocmd("BufReadPost", {
	group = last_cursor_pos_augroup,
	desc = "Restore the position of previously opened files",
	callback = function(opts)
		if vim.tbl_contains({ "quickfix", "nofile", "help" }, vim.bo.buftype) then
			return
		end

		if vim.tbl_contains({ "gitcommit", "gitrebase", "svn", "hgcommit" }, vim.bo.filetype) then
			return
		end

		-- If a line has already been specified on the command line, we are done
		--   nvim file +num
		if vim.fn.line "." > 1 then
			return
		end

		-- If the last line is set and the less than the last line in the buffer
		if vim.fn.line [['"]] > 0 and vim.fn.line [['"]] <= vim.fn.line "$" then
			if vim.fn.line "w$" == vim.fn.line "$" then
				-- if the last line in the current buffer is also the last line visible
				-- in this window
				vim.cmd [[normal! g`"]]
			elseif vim.fn.line "$" - vim.fn.line [['"]] > ((vim.fn.line "w$" - vim.fn.line "w0") / 2) - 1 then
				-- if we're not at the bottom of the file, center the cursor on the
				-- screen after we make the jump
				vim.cmd [[normal! g`"zz]]
			else
				-- otherwise, show as much context as we can by jumping to the end of
				-- the file and then to the mark. If we pressed zz here, there would be
				-- blank lines at the bottom of the screen. We intentionally leave the
				-- last line blank by pressing <c-e> so the user has a clue that they
				-- are near the end of the file.
				vim.cmd [[normal! G'"<c-e>]]
			end
		end
	end,
})

vim.api.nvim_create_autocmd("TextYankPost", {
	desc = "Highlight yanked text",
	callback = function(opts)
		vim.highlight.on_yank()
	end,
})

vim.g.tex_flavor = "latex"

vim.g.gruvbox_italic = 1
vim.cmd "colorscheme gruvbox"

-- Mappings
-----------

local mapopts = { noremap = true, silent = true }

vim.fn["camelcasemotion#CreateMotionMappings"] "<LocalLeader>"

vim.keymap.set("n", "yof", function()
	if vim.opt_local.formatoptions:get().a then
		vim.opt_local.formatoptions:remove "a"
		print ":setlocal formatoptions-=a"
	else
		vim.opt_local.formatoptions:append "a"
		print ":setlocal formatoptions+=a"
	end
end, mapopts)

-- Plugins
----------

-- Gitsigns

require("gitsigns").setup {
	on_attach = function(bufnr)
		local gs = package.loaded.gitsigns

		local function map(mode, l, r, opts)
			opts = vim.tbl_extend("force", {
				noremap = true,
				buffer = buffnr,
				silent = true,
			}, opts or {})
			vim.keymap.set(mode, l, r, opts)
		end

		-- Navigation
		map('n', ']c', function()
			if vim.wo.diff then return ']c' end
			vim.schedule(function() gs.next_hunk() end)
			return '<Ignore>'
		end, { expr = true })

		map('n', '[c', function()
			if vim.wo.diff then return '[c' end
			vim.schedule(function() gs.prev_hunk() end)
			return '<Ignore>'
		end, { expr = true })

		-- Actions
		map('n', '<leader>hs', gs.stage_hunk, { desc = "Stage Hunk" })
		map('n', '<leader>hr', gs.reset_hunk, { desc = "reset Hunk" })
		map('v', '<leader>hs', function() gs.stage_hunk { vim.fn.line('.'), vim.fn.line('v') } end, { desc = "Stage hunk" })
		map('v', '<leader>hr', function() gs.reset_hunk { vim.fn.line('.'), vim.fn.line('v') } end, { desc = "Reset hunk" })
		map('n', '<leader>hS', gs.stage_buffer, { desc = "Stage buffer" })
		map('n', '<leader>hu', gs.undo_stage_hunk, { desc = "Undo stage hunk" })
		map('n', '<leader>hR', gs.reset_buffer, { desc = "Reset buffer" })
		map('n', '<leader>hp', gs.preview_hunk, { desc = "Preview hunk" })
		map('n', '<leader>hb', function() gs.blame_line { full = true } end, { desc = "Blame line" })
		map('n', '<leader>tb', gs.toggle_current_line_blame, { desc = "Toggle current line blame" })
		map('n', '<leader>hd', gs.diffthis, { desc = "Diff this" })
		map('n', '<leader>hD', function() gs.diffthis('~') end, { desc = "Diff this against ~" })
		map('n', '<leader>td', gs.toggle_deleted, { desc = "Toggle deleted" })

		-- Text object
		map({ 'o', 'x' }, 'ih', ':<C-U>Gitsigns select_hunk<CR>', { desc = "Select hunk" })
	end,
}

-- Treesitter

require("nvim-treesitter.configs").setup {
	parser_install_dir = vim.fn.stdpath("data") .. "/site",
	highlight = {
		enable = true,
	},
	incremental_selection = {
		enable = true,
		keymaps = {
			init_selection = "gnn",
			node_incremental = "grn",
			scope_incremental = "grc",
			node_decremental = "grm",
		},
	},
	indent = {
		enable = true,
		disable = { "rust" },
	},
	matchup = {
		enable = true,
	},
	refactor = {
		highlight_definitions = { enable = true },
		navigation = { enable = true },
	},
	textobjects = {
		lsp_interop = {
			enable = true,
			border = "none",
			peek_definition_code = {
				["<leader>df"] = "@function.outer",
				["<leader>dF"] = "@class.outer",
			},
		},

		move = {
			enable = true,
			goto_next_start = {
				["]m"] = "@function.outer",
				["]]"] = "@class.outer",
			},
			goto_next_end = {
				["]M"] = "@function.outer",
				["]["] = "@class.outer",
			},
			goto_previous_start = {
				["[m"] = "@function.outer",
				["[["] = "@class.outer",
			},
			goto_previous_end = {
				["[M"] = "@function.outer",
				["[]"] = "@class.outer",
			},
		},

		select = {
			enable = true,
			keymaps = {
				-- You can use the capture groups defined in textobjects.scm
				["af"] = "@function.outer",
				["if"] = "@function.inner",

				["aF"] = "@call.outer",
				["iF"] = "@call.inner",

				["ac"] = "@class.outer",
				["ic"] = "@class.inner",

				["aC"] = "@comment.outer",
				["iC"] = "@comment.inner",

				["ab"] = "@block.outer",
				["ib"] = "@block.inner",

				["aa"] = "@parameter.outer",
				["ia"] = "@parameter.inner",
			},
		},

		swap = {
			enable = true,
			swap_next = {
				["<leader>a"] = "@parameter.inner",
			},
			swap_previous = {
				["<leader>A"] = "@parameter.inner",
			},
		},
	},
}

vim.o.foldmethod = "expr"
vim.o.foldexpr = "nvim_treesitter#foldexpr()"
vim.o.foldlevel = 99

vim.api.nvim_set_hl(0, "TSCurrentScope", {
	bg = vim.g.current_gruvbox_colors.dark0_soft[1],
})

vim.api.nvim_set_hl(0, "TSDefinition", {
	bg = vim.g.current_gruvbox_colors.faded_blue[1],
})

vim.api.nvim_set_hl(0, "TSDefinitionUsage", {
	bg = vim.g.current_gruvbox_colors.faded_aqua[1],
})

-- Treesitter highlight groups

vim.api.nvim_set_hl(0, "@attribute", { link = "Macro" })
vim.api.nvim_set_hl(0, "@error", { link = "ErrorMsg" })

vim.api.nvim_set_hl(0, "@text.diff.add", { link = "diffAdded" })
vim.api.nvim_set_hl(0, "@text.diff.delete", { link = "diffRemoved" })

vim.api.nvim_set_hl(0, "@text.strong", { bold = true })
vim.api.nvim_set_hl(0, "@text.emphasis", { italic = true })
vim.api.nvim_set_hl(0, "@text.underline", { underline = true })
vim.api.nvim_set_hl(0, "@text.strike", { strikethrough = true })
vim.api.nvim_set_hl(0, "@text.environment", { link = "Macro" })

vim.api.nvim_set_hl(0, "@text.note", { link = "ModeMsg" })
vim.api.nvim_set_hl(0, "@text.warning", { link = "WarningMsg" })
vim.api.nvim_set_hl(0, "@text.warning", { link = "ErrorMsg" })

vim.api.nvim_set_hl(0, "@tag.attribute", { link = "@attribute" })
vim.api.nvim_set_hl(0, "@tag.delimiter", { link = "@punctuation.delimiter" })

-- nvim-cmp

local cmp = require "cmp"

cmp.setup {
	snippet = {
		expand = function(args)
			vim.fn["vsnip#anonymous"](args.body)
		end,
	},
	mapping = cmp.mapping.preset.insert {
		["<CR>"] = cmp.mapping.confirm { select = false },
	},
	sources = cmp.config.sources({
		{ name = "nvim_lsp" },
		{ name = "vsnip" },
		{ name = "latex_symbols" },
		{ name = "calc" },
	}, {
		{ name = "path" },
		{ name = "treesitter" },
	}, {
		{ name = "tmux" },
		{ name = "spell" },
		-- Use \k for iskeyword because of UTF-8
		{ name = "buffer", option = { keyword_pattern = [[\k\+]] } },
	}),
}

-- cmp.setup.cmdline("/", {
-- 	sources = {
-- 		{ name = "buffer", option = { keyword_pattern = [[\k\+]] } },
-- 	},
-- 	mapping = cmp.mapping.preset.cmdline {},
-- })

-- cmp.setup.cmdline(":", {
-- 	sources = cmp.config.sources {
-- 		{ name = "cmdline" },
-- 	},
-- 	mapping = cmp.mapping.preset.cmdline {},
-- })

-- Telescope

require("telescope").setup {
	extensions = {
		["ui-select"] = {
			require("telescope.themes").get_dropdown(),
		},
	},
}

require("telescope").load_extension "ui-select"

local telescope_builtin = require("telescope.builtin")

vim.keymap.set("n", "<leader>fb", telescope_builtin.buffers, mapopts)
vim.keymap.set("n", "<leader>ff", telescope_builtin.find_files, mapopts)
vim.keymap.set("n", "<leader>fg", telescope_builtin.live_grep, mapopts)
vim.keymap.set("n", "<leader>fh", telescope_builtin.help_tags, mapopts)
vim.keymap.set("n", "<leader>fo", telescope_builtin.oldfiles, mapopts)
vim.keymap.set("n", "<leader>fs", telescope_builtin.spell_suggest, mapopts)
vim.keymap.set("n", "<leader>ft", telescope_builtin.treesitter, mapopts)

-- Oil.nvim

require("oil").setup()

vim.keymap.set("n", "-", require("oil").open, { desc = "Open parent directory" })

-- Disable netrw
vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1

-- Lualine

require("lualine").setup {
	options = {
		component_separators = "",
		icons_enabled = false,
		section_separators = "",
	},
	sections = {
		lualine_c = {
			"filename",
			{
				"lsp_progress",
				display_components = { "lsp_client_name", { "title", "percentage", "message" } },
			},
		},
	},
}

-- VSnip

vim.keymap.set("i", "<Tab>", "vsnip#jumpable(1) ? '<Plug>(vsnip-jump-next)' : '<Tab>'", { silent = true, expr = true })
vim.keymap.set("s", "<Tab>", "vsnip#jumpable(1) ? '<Plug>(vsnip-jump-next)' : '<Tab>'", { silent = true, expr = true })

vim.keymap.set(
	"i",
	"<S-Tab>",
	"vsnip#jumpable(-1) ? '<Plug>(vsnip-jump-prev)' : '<Tab>'",
	{ silent = true, expr = true }
)
vim.keymap.set(
	"s",
	"<S-Tab>",
	"vsnip#jumpable(-1) ? '<Plug>(vsnip-jump-prev)' : '<Tab>'",
	{ silent = true, expr = true }
)

-- OSCyank

-- Text yanked into the "t register gets copied using OSC52 escape sequences
-- (e.g. goes through SSH)
vim.api.nvim_create_autocmd("TextYankPost", {
	desc = "Setup for OSCYank",
	callback = function(opts)
		if vim.v.event.regname == "t" then
			vim.cmd [[OSCYankReg t]]
		end
	end,
})

-- Diffview

require("diffview").setup {
	use_icons = false,
	enhanced_diff_hl = true,
}

-- Notify

vim.cmd [[highlight link NotifyERRORBorder DiagnosticError]]
vim.cmd [[highlight link NotifyERRORIcon DiagnosticError]]
vim.cmd [[highlight link NotifyERRORTitle DiagnosticError]]

vim.cmd [[highlight link NotifyWARNBorder DiagnosticWarn]]
vim.cmd [[highlight link NotifyWARNIcon DiagnosticWarn]]
vim.cmd [[highlight link NotifyWARNTitle DiagnosticWarn]]

vim.cmd [[highlight link NotifyINFOBorder DiagnosticInfo]]
vim.cmd [[highlight link NotifyINFOIcon DiagnosticInfo]]
vim.cmd [[highlight link NotifyINFOTitle DiagnosticInfo]]

vim.cmd [[highlight link NotifyDEBUGBorder DiagnosticHint]]
vim.cmd [[highlight link NotifyDEBUGIcon DiagnosticHint]]
vim.cmd [[highlight link NotifyDEBUGTitle DiagnosticHint]]

vim.cmd [[highlight link NotifyDEBUGBorder GruvboxPurple]]
vim.cmd [[highlight link NotifyDEBUGIcon GruvboxPurple]]
vim.cmd [[highlight link NotifyDEBUGTitle GruvboxPurple]]

require("notify").setup { stages = "static" }

vim.notify = require("notify")

-- Comment

require("Comment").setup {}

-- Indent Blankline

vim.api.nvim_set_hl(0, "IndentBlanklineContextChar", {
	fg = vim.g.current_gruvbox_colors.aqua[1],
})

require("indent_blankline").setup {
	show_current_context = true,
	show_current_context_start = true,
}

-- Local config

local function isModuleAvailable(name)
	if package.loaded[name] then
		return true
	else
		for _, searcher in ipairs(package.searchers or package.loaders) do
			local loader = searcher(name)
			if type(loader) == "function" then
				package.preload[name] = loader
				return true
			end
		end
		return false
	end
end

vim.opt.runtimepath:append "~/.config/nvim"

if isModuleAvailable "local_config" then
	require "local_config"
end