-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
625 lines (583 loc) · 17.1 KB
/
init.lua
File metadata and controls
625 lines (583 loc) · 17.1 KB
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
-------------------------------------------------------------------------------
-- Enable byte-code caching
-------------------------------------------------------------------------------
if vim.loader then
vim.loader.enable()
end
-------------------------------------------------------------------------------
-- Plugins (vim.pack)
-------------------------------------------------------------------------------
local plugins = {
-- Core
{ src = "https://github.com/saghen/blink.cmp", version = "v1" }, -- auto completion
"https://github.com/ibhagwan/fzf-lua", -- fuzzy finding, requires fzf
"https://github.com/nvim-lualine/lualine.nvim", -- status line
"https://github.com/christoomey/vim-tmux-navigator", -- seamless navigation between Neovim and tmux panes
"https://github.com/folke/which-key.nvim", -- keymap helper (shows available mappings in a popup)
-- Colour scheme
"https://github.com/rebelot/kanagawa.nvim", -- colour scheme
-- LSP
"https://github.com/mason-org/mason.nvim", -- installs formatters and linters, requires Node.js
"https://github.com/mason-org/mason-lspconfig.nvim", -- installs and enables language servers
"https://github.com/neovim/nvim-lspconfig", -- auto-configures language servers
-- Formatting and linting
"https://github.com/stevearc/conform.nvim", -- formatter
"https://github.com/mfussenegger/nvim-lint", -- linter
-- Misc.
"https://github.com/lewis6991/gitsigns.nvim", -- inline git integration: hunks, blame, and gutter signs
"https://github.com/windwp/nvim-autopairs", -- auto-closes and manages paired characters
-- Enhances Neovim config development (Lua LSP, Neovim types, etc.)
-- "https://github.com/folke/lazydev.nvim",
}
-------------------------------------------------------------------------------
-- Options
-------------------------------------------------------------------------------
vim.opt.background = "dark"
vim.opt.cursorline = true
vim.opt.expandtab = true
vim.opt.foldlevel = 99
vim.opt.foldlevelstart = 99
vim.opt.foldnestmax = 10
vim.opt.hlsearch = true
vim.opt.inccommand = "split"
vim.opt.listchars = { -- special characters
-- eol = "⏎",
-- space = "␣",
tab = ">-",
trail = "⋅",
nbsp = "~",
extends = ">",
precedes = "<",
}
vim.opt.list = true
vim.opt.number = true
vim.opt.relativenumber = true
vim.opt.scrolloff = 8
vim.opt.shiftwidth = 4
vim.opt.signcolumn = "yes:1"
vim.opt.spell = true
vim.opt.spelllang = "en_ca"
vim.opt.splitbelow = true
vim.opt.splitright = true
vim.opt.termguicolors = true
vim.opt.undofile = true
vim.opt.virtualedit = "block"
vim.opt.winborder = "rounded"
vim.opt_local.foldmethod = "expr"
vim.opt_local.foldexpr = "v:lua.vim.treesitter.foldexpr()"
vim.g.mapleader = " "
vim.g.maplocalleader = "\\"
vim.g.vimtex_view_method = "sioyek"
vim.diagnostic.config({
virtual_text = {
current_line = true,
},
})
-------------------------------------------------------------------------------
-- vim.pack install/update hooks
-- https://neovim.io/doc/user/pack/#vim.pack-events
-------------------------------------------------------------------------------
---@type table<string, fun(ev: vim.api.keyset.create_autocmd.callback_args)>
local hooks = {
["blink.cmp"] = function(ev)
-- Check if cargo is available
if vim.fn.executable("cargo") ~= 1 then
vim.notify(
"[blink.cmp] cargo not found -> Lua fallback (no native fuzzy matching)",
vim.log.levels.INFO,
{ title = "blink.cmp" }
)
return
end
vim.notify(
"[blink.cmp] Compiling Rust binary...",
vim.log.levels.INFO,
{ title = "blink.cmp" }
)
-- Run the build command inside the plugin directory
vim.system(
{ "cargo", "build", "--release" },
{ cwd = ev.data.path },
function(obj)
-- Callback runs off the main thread → schedule back to UI
vim.schedule(function()
if obj.code == 0 then
vim.notify(
"[blink.cmp] Build complete",
vim.log.levels.INFO,
{ title = "blink.cmp" }
)
else
vim.notify(
("[blink.cmp] Build failed:\n%s"):format(
obj.stderr or "No error output"
),
vim.log.levels.ERROR,
{ title = "blink.cmp" }
)
end
end)
end
)
end,
-- Example: Add more hooks easily
-- ["fzf-lua"] = function(ev) ... end,
}
---@param ev vim.api.keyset.create_autocmd.callback_args
local function run_pack_hooks(ev)
local data = ev.data
if not data then
return
end
-- Only care about install/update
local kind = data.kind
if kind ~= "install" and kind ~= "update" then
return
end
-- Safe access (no deep chaining)
local spec = data.spec
if not spec or not spec.name then
return
end
local hook = hooks[spec.name]
if hook then
hook(ev)
end
end
-- If hooks need to run on install, run this before `vim.pack.add()`
-- To act on install from lockfile, run before very first `vim.pack.add()`
local pack_grp = vim.api.nvim_create_augroup("PackHooks", { clear = true })
vim.api.nvim_create_autocmd("PackChanged", {
group = pack_grp,
desc = "Execute plugin-specific post-install/update hooks",
callback = run_pack_hooks,
})
-- Install plugins
vim.pack.add(plugins, { confirm = false })
-- Enable native Undotree
vim.cmd.packadd("nvim.undotree")
-------------------------------------------------------------------------------
-- vim.pack custom commands
-------------------------------------------------------------------------------
vim.api.nvim_create_user_command("PackClean", function()
local inactive_plugins = vim.iter(vim.pack.get())
:filter(function(x)
return not x.active
end)
:map(function(x)
return x.spec.name
end)
:totable()
if #inactive_plugins > 0 then
vim.pack.del(inactive_plugins)
vim.notify(
("Removed inactive plugins: %s"):format(
table.concat(inactive_plugins, ", ")
),
vim.log.levels.INFO,
{ title = "PackClean" }
)
else
vim.notify(
"No inactive plugins to remove",
vim.log.levels.INFO,
{ title = "PackClean" }
)
end
end, {
desc = "Remove inactive vim.pack plugins",
})
vim.api.nvim_create_user_command("PackSync", function()
vim.pack.update(nil, { target = "lockfile" })
end, {
desc = "Sync vim.pack plugins to lockfile (nvim-pack-lock.json)",
})
vim.api.nvim_create_user_command("PackUpdate", function()
vim.pack.update()
end, {
desc = "Update vim.pack plugins",
})
-------------------------------------------------------------------------------
-- Plugin configurations (in alphabetical order)
-------------------------------------------------------------------------------
-- blink.cmp
require("blink.cmp").setup({
completion = {
menu = {
border = "rounded",
draw = {
columns = {
{ "label", "label_description", gap = 1 },
{ "kind_icon", "kind", "source_name", gap = 1 },
},
},
},
documentation = {
auto_show = true,
window = {
border = "rounded",
scrollbar = true,
winhighlight = table.concat({
"Normal:BlinkCmpMenu",
"NormalFloat:BlinkCmpMenu",
"FloatBorder:BlinkCmpMenuBorder",
"CursorLine:BlinkCmpDocCursorLine",
"Search:None",
}, ","),
},
},
},
fuzzy = {
implementation = vim.fn.executable("cargo") == 1 and "prefer_rust"
or "lua",
},
keymap = { ["<CR>"] = { "accept", "fallback" } },
signature = {
enabled = true,
window = { border = "rounded" },
},
cmdline = { completion = { menu = { auto_show = true } } },
})
vim.keymap.set("n", "<leader>bc", function()
vim.b.completion = not (vim.b.completion ~= false)
vim.notify(
("Blink completion (buffer): %s"):format(vim.b.completion),
vim.log.levels.INFO,
{ title = "Blink completion toggle (buffer)" }
)
end, {
desc = "Blink completion toggle (buffer)",
silent = true,
})
-- fzf-lua
local fzf = require("fzf-lua")
fzf.setup({
keymap = {
fzf = {
-- Map <C-q> to select all items and press enter
["ctrl-q"] = "select-all+accept",
},
},
})
fzf.register_ui_select()
vim.keymap.set(
"n",
"<leader>co",
"<cmd>FzfLua colorschemes<CR>",
{ desc = "Color schemes", silent = true }
)
vim.keymap.set(
"n",
"<leader>fb",
"<cmd>FzfLua buffers<CR>",
{ desc = "Find buffers", silent = true }
)
vim.keymap.set(
"n",
"<leader>ff",
"<cmd>FzfLua files<CR>",
{ desc = "Find files", silent = true }
)
vim.keymap.set(
"n",
"<leader>fg",
"<cmd>FzfLua grep_project<CR>",
{ desc = "Grep project", silent = true }
)
vim.keymap.set(
"n",
"<leader>fo",
"<cmd>FzfLua oldfiles<CR>",
{ desc = "File history", silent = true }
)
vim.keymap.set(
"n",
"<leader>fi",
"<cmd>FzfLua git_files<CR>",
{ desc = "Find git files", silent = true }
)
vim.keymap.set(
"n",
"<leader>fr",
"<cmd>FzfLua lsp_references<CR>",
{ desc = "LSP references", silent = true }
)
vim.keymap.set(
"n",
"<leader>xX",
"<cmd>FzfLua diagnostics_document<CR>",
{ desc = "Diagnostics document", silent = true }
)
vim.keymap.set(
"n",
"<leader>xx",
"<cmd>FzfLua diagnostics_workspace<CR>",
{ desc = "Diagnostics project", silent = true }
)
-- kanagawa.nvim
require("kanagawa").setup({
-- Remove the background of LineNr, {Sign,Fold}Column and friends
colors = { theme = { all = { ui = { bg_gutter = "none" } } } },
})
-- lualine.nvim
require("lualine").setup({})
-- mason.nvim
local formatters = {
"black", -- Python
"gofumpt", -- Go
"goimports", -- Go
"golines", -- Go
"latexindent", -- Latex
"prettier", -- Multiple languages
"shfmt", -- Bash/sh
"stylua", -- Lua
}
local linters = {
"cmakelang", -- CMake
"cmakelint", -- CMake
"mypy", -- Python
"shellcheck", -- Bash/sh
}
--- Ensures the given Mason packages are installed, installing any missing ones
--- asynchronously
---@param tools string[] -- List of Mason package names to ensure installed
local function ensure_mason_tools_installed(tools)
local mr = require("mason-registry")
mr.refresh(function()
for _, tool in ipairs(tools) do
local ok, pkg = pcall(mr.get_package, tool)
if ok and not pkg:is_installed() then
pkg:install()
end
end
end)
end
require("mason").setup({
ui = { border = "rounded" },
})
ensure_mason_tools_installed(formatters)
ensure_mason_tools_installed(linters)
-- mason-lspconfig.nvim
local language_servers = {
"basedpyright",
"bashls",
"clangd",
"cmake",
"eslint",
"gopls",
"julials",
"lua_ls",
"marksman",
"perlnavigator",
"ruff",
"rust_analyzer",
"texlab",
"tinymist",
"tombi",
"ts_ls",
"vimls",
"yamlls",
"zls",
}
require("mason-lspconfig").setup({
ensure_installed = language_servers,
automatic_enable = {
exclude = { "julials" },
},
})
vim.lsp.enable("julials")
-- conform.nvim
require("conform").setup({
formatters_by_ft = {
cmake = { "cmakelang" },
go = { "golines", "gofumpt", "goimports" },
javascript = { "prettier" },
json = { "prettier" },
lua = { "stylua" },
markdown = { "prettier" },
python = { "black" },
sh = { "shfmt" },
typescript = { "prettier" },
},
format_on_save = {
timeout_ms = 500,
lsp_fallback = true, -- use LSP if no formatter
},
})
vim.keymap.set("n", "<leader>fm", function()
require("conform").format()
end, { desc = "Format buffer (conform.nvim)", silent = true })
-- nvim-lint
require("lint").linters_by_ft = {
bash = { "shellcheck" },
cmake = { "cmakelint" },
python = { "mypy" },
}
local nvim_lint_grp = vim.api.nvim_create_augroup("Nvim-lint", { clear = true })
vim.api.nvim_create_autocmd({ "BufWritePost", "InsertLeave" }, {
group = nvim_lint_grp,
desc = "nvim-lint hooks",
callback = function()
-- try_lint without arguments runs the linters defined in `linters_by_ft`
-- for the current filetype
require("lint").try_lint()
-- You can call `try_lint` with a linter name or a list of names to
-- always run specific linters, independent of the `linters_by_ft`
-- configuration
-- require("lint").try_lint("cspell")
end,
})
-- nvim-autopairs
require("nvim-autopairs").setup({
fast_wrap = {},
disable_filetype = { "vim" },
})
-------------------------------------------------------------------------------
-- Auto commands
-------------------------------------------------------------------------------
local generic_grp = vim.api.nvim_create_augroup("Generic", { clear = true })
-- Set SignColumn colour to background colour, aesthetically nicer
vim.api.nvim_create_autocmd({ "ColorScheme", "VimEnter" }, {
group = generic_grp,
pattern = { "*" },
desc = "Set SignColumn color to background color",
command = "hi! link SignColumn Normal",
})
-- Disable spell in terminal buffers
vim.api.nvim_create_autocmd("TermOpen", {
group = generic_grp,
desc = "Disable spell in terminal buffers",
callback = function()
vim.opt_local.spell = false
end,
})
-- Highlights yanked text
vim.api.nvim_create_autocmd("TextYankPost", {
group = generic_grp,
pattern = { "*" },
desc = "Highlights yanked text",
callback = function()
vim.highlight.on_yank({ timeout = 200 })
end,
})
-------------------------------------------------------------------------------
-- Generic keymaps
-------------------------------------------------------------------------------
vim.keymap.set("n", "<ESC>", "<cmd>nohlsearch<CR>", { silent = true })
vim.keymap.set(
{ "n", "x" },
"<leader>d",
'"+d',
{ desc = "Delete (cut) to system clipboard", silent = true }
)
vim.keymap.set(
{ "n", "x" },
"<leader>p",
'"+p',
{ desc = "Paste from system clipboard", silent = true }
)
vim.keymap.set(
{ "n", "x" },
"<leader>y",
'"+y',
{ desc = "Yank to system clipboard", silent = true }
)
vim.keymap.set(
{ "n", "x" },
"<leader>_",
'"_d',
{ desc = "Delete without yanking (black hole)", silent = true }
)
vim.keymap.set(
"v",
"<",
"<gv",
{ desc = "Indent left (keep selection)", silent = true }
)
vim.keymap.set(
"v",
">",
">gv",
{ desc = "Indent right (keep selection)", silent = true }
)
vim.keymap.set(
"x",
"p",
'"_dP',
{ desc = "Paste without overwriting register", silent = true }
)
vim.keymap.set(
"n",
"<leader>e",
"<cmd>Lexplore 25<CR>",
{ desc = "Netrw file explorer", silent = true }
)
vim.keymap.set("n", "<leader>u", function()
require("undotree").open({
command = "topleft 30vnew",
})
end, { desc = "Undotree toggle", silent = true })
local lsp_grp = vim.api.nvim_create_augroup("LspKeymaps", { clear = true })
vim.api.nvim_create_autocmd("LspAttach", {
group = lsp_grp,
desc = "LSP keymaps",
callback = function(ev)
vim.keymap.set(
"n",
"gd",
vim.lsp.buf.definition,
{ desc = "LSP go to definition", silent = true }
)
vim.keymap.set(
"n",
"gD",
vim.lsp.buf.declaration,
{ desc = "LSP go to declaration", silent = true }
)
vim.keymap.set("n", "<leader>ih", function()
local enabled = vim.lsp.inlay_hint.is_enabled()
local new = not enabled
vim.lsp.inlay_hint.enable(new)
vim.notify(
("Inlay hints (global): %s"):format(new),
vim.log.levels.INFO,
{ title = "Inlay hints toggle (global)" }
)
end, { desc = "Inlay hints toggle (global)", silent = true })
vim.keymap.set("n", "<leader>iH", function()
local enabled = vim.lsp.inlay_hint.is_enabled({ bufnr = ev.buf })
local new = not enabled
vim.lsp.inlay_hint.enable(new, { bufnr = ev.buf })
vim.notify(
("Inlay hints (buffer): %s"):format(new),
vim.log.levels.INFO,
{ title = "Inlay hints toggle (buffer)" }
)
end, {
desc = "Inlay hints toggle (buffer)",
silent = true,
buffer = ev.buf,
})
end,
})
-------------------------------------------------------------------------------
-- UI
-------------------------------------------------------------------------------
-- Enable new experimental UI2
require("vim._core.ui2").enable({
enable = true,
msg = {
-- This redirect messages to the new system
targets = {
confirm = "cmd", -- Confirm prompts (e.g., :quit with unsaved changes)
[""] = "msg", -- General messages (echo)
bufwrite = "msg", -- Buffer write messages
echo = "msg", -- :echo output
echoerr = "msg", -- :echoerr output
echomsg = "msg", -- :echomsg output
emsg = "msg", -- Error messages (goes to the new pager buffer)
},
},
})
-- Set colour scheme
vim.cmd.colorscheme("kanagawa")