-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathinit.lua
414 lines (394 loc) · 12.5 KB
/
init.lua
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
----------
-- Helpers
----------
-- Helper to define a sign with its `name` as `texthl`
---@param name string
---@param text string
---@return -1|0
function _G.define_sign(name, text)
return vim.fn.sign_define(name, { text = text, texthl = name })
end
-----------
-- Settings
-----------
-- Enable bytecode cache
vim.loader.enable()
-- Set <leader> to '\'
vim.g.mapleader = [[\]]
-- Set <localleader> to <SPACE>
vim.g.maplocalleader = [[ ]]
-- Swap
vim.opt.swapfile = false
-- Indentation
vim.opt.expandtab = true
vim.opt.shiftwidth = 0
vim.opt.tabstop = 2
-- Wrapping
vim.opt.linebreak = true
vim.opt.breakindent = true
vim.opt.breakindentopt = { "shift:2" }
vim.opt.showbreak = "↳ "
-- Folding
vim.opt.foldtext = ""
vim.opt.foldlevel = 99
-- Undo persistence
vim.opt.undofile = true
-- Enable local configuration
vim.opt.exrc = true
-- Line numbers
vim.opt.number = true
vim.opt.relativenumber = true
-- Scrolling
vim.opt.splitkeep = "screen"
vim.opt.smoothscroll = true
-- Command-line
vim.opt.showmode = false
-- Whitespace
vim.opt.list = true
vim.opt.listchars = {
trail = "·",
tab = "→ ",
nbsp = "·",
}
-- Diffs
vim.opt.fillchars:append({ diff = "/" })
-- Spell-checking
vim.opt.spell = true
vim.opt.spelllang = { "programming", "en", "es" }
vim.opt.spellfile = {
vim.fn.stdpath("config") .. "/spell/programming.utf-8.add",
vim.fn.stdpath("config") .. "/spell/en.utf-8.add",
vim.fn.stdpath("config") .. "/spell/es.utf-8.add",
}
vim.opt.spelloptions = "camel"
vim.opt.spellcapcheck = ""
-- Sign column
vim.opt.signcolumn = "yes:1"
-- Insert-mode completion
vim.opt.shortmess:append("c")
-- Grep
vim.opt.grepprg = "rg --vimgrep"
vim.opt.grepformat = "%f:%l:%c:%m"
-- Mouse
vim.opt.mousemodel = "extend"
vim.opt.mousescroll = "ver:2,hor:0"
vim.keymap.set({ "n", "v", "o", "c", "i" }, "<MiddleMouse>", "<Nop>")
vim.keymap.set({ "n", "v", "o", "c", "i" }, "<MiddleDrag>", "<Nop>")
vim.keymap.set({ "n", "v", "o", "c", "i" }, "<MiddleRelease>", "<Nop>")
-- Link identification
vim.opt.isfname:append("*")
---------------
-- Autocommands
---------------
-- Open files on the last position
vim.api.nvim_create_autocmd("BufReadPost", {
group = vim.api.nvim_create_augroup("open-file-last-position", { clear = true }),
command = [[silent! normal! g`"zv]],
})
-- Resize splits on window resize
vim.api.nvim_create_autocmd("VimResized", {
group = vim.api.nvim_create_augroup("resize-splits-on-resize", { clear = true }),
command = "wincmd =",
})
-- Set some options on the terminal
vim.api.nvim_create_autocmd("TermOpen", {
group = vim.api.nvim_create_augroup("terminal-options", { clear = true }),
callback = function()
-- Start on insert mode
vim.cmd.startinsert()
-- Disable line numbers
vim.opt_local.number = false
vim.opt_local.relativenumber = false
-- Disable spell checking
vim.opt_local.spell = false
-- Disable sign column
vim.opt_local.signcolumn = "no"
-- Disable colorcolumn
vim.opt_local.colorcolumn = {}
end,
})
-- Always disable 'spell' on some filetypes
vim.api.nvim_create_autocmd("FileType", {
group = vim.api.nvim_create_augroup("disable-spell", { clear = true }),
pattern = {
"checkhealth",
"gitignore",
"help",
"qf",
"man",
"editorconfig",
"query",
"molten_output",
},
callback = function()
vim.opt_local.spell = false
end,
})
-- Always enable 'spell' on some filetypes
vim.api.nvim_create_autocmd("FileType", {
group = vim.api.nvim_create_augroup("enable-spell", { clear = true }),
pattern = {
"markdown",
"tex",
"quarto",
},
callback = function()
vim.opt_local.spell = true
end,
})
-- Always enable 'conceallevel' on some filetypes
vim.api.nvim_create_autocmd("FileType", {
group = vim.api.nvim_create_augroup("enable-conceal", { clear = true }),
pattern = { "html" },
callback = function()
vim.opt_local.conceallevel = 2
end,
})
-----------
-- Keybinds
-----------
-- Disable highlights with <ESC>
vim.keymap.set("n", "<ESC>", "<ESC><CMD>nohlsearch<CR>")
-- Make mappings similar to TMUX mappings for Vim tabs
vim.keymap.set("n", "<C-t>n", "<CMD>tabnew<CR>")
vim.keymap.set("n", "<C-t>s", "<CMD>tabs<CR>")
vim.keymap.set("n", "<C-t>[", "<CMD>tabnext<CR>")
vim.keymap.set("n", "<C-t>]", "<CMD>tabprevious<CR>")
vim.keymap.set("n", "<C-t>w", "<CMD>tabclose<CR>")
vim.keymap.set("n", "<C-t>q", "<CMD>tabclose<CR>")
-- Close wildmenu using <SPACE>
vim.keymap.set("c", "<SPACE>", function()
return vim.fn.wildmenumode() == 1 and "<C-y>" or "<SPACE>"
end, { expr = true })
-- Make `j` work with wrapped lines
vim.keymap.set({ "n", "v" }, "j", function()
if vim.v.count == 0 then
return "gj"
else
return "j"
end
end, { expr = true })
-- Make `k` work with wrapped lines
vim.keymap.set({ "n", "v" }, "k", function()
if vim.v.count == 0 then
return "gk"
else
return "k"
end
end, { expr = true })
-- Make `<Up>` work with wrapped lines
vim.keymap.set({ "n", "v" }, "<Up>", function()
if vim.v.count == 0 then
return "gk"
else
return "<Up>"
end
end, { expr = true })
-- Make `<Down>` work with wrapped lines
vim.keymap.set({ "n", "v" }, "<Down>", function()
if vim.v.count == 0 then
return "gj"
else
return "<Down>"
end
end, { expr = true })
-- Do not jump with <S-Up> and <S-Down>
vim.keymap.set({ "i", "n", "v" }, "<S-Up>", "<Up>", { remap = true })
vim.keymap.set({ "i", "n", "v" }, "<S-Down>", "<Down>", { remap = true })
-- Use <M-Left> and <M-Right> for moving between words
vim.keymap.set({ "c", "i", "n", "v" }, "<M-Left>", "<S-Left>")
vim.keymap.set({ "c", "i", "n", "v" }, "<M-Right>", "<S-Right>")
---------------
-- Text objects
---------------
-- Line text objects
vim.keymap.set({ "x", "o" }, "iL", ":<C-u>normal! g_v^<CR>", { silent = true })
vim.keymap.set({ "x", "o" }, "aL", ":<C-u>normal! $v0<CR>", { silent = true })
-- Document text objects
vim.keymap.set({ "x", "o" }, "id", ":<C-u>normal! G$vgg0<CR>", { silent = true })
--------
-- Icons
--------
_G.icons = {
diagnostic = {
error = "",
warn = "",
info = "",
hint = "",
ok = "",
},
git = {
added = "+",
modified = "~",
removed = "-",
},
debug = {
breakpoint = "",
condition = "",
log = "",
stopped = "",
rejected = "",
},
}
_G.border = "rounded"
--------------
-- Diagnostics
--------------
-- Configure diagnostics
vim.diagnostic.config({
underline = { severity = { min = vim.diagnostic.severity.INFO } },
signs = {
severity = { min = vim.diagnostic.severity.HINT },
text = {
[vim.diagnostic.severity.ERROR] = icons.diagnostic.error,
[vim.diagnostic.severity.WARN] = icons.diagnostic.warn,
[vim.diagnostic.severity.INFO] = icons.diagnostic.info,
[vim.diagnostic.severity.HINT] = icons.diagnostic.hint,
},
},
virtual_text = {
severity = { min = vim.diagnostic.severity.INFO },
prefix = "",
format = function(diagnostic)
return ({
[vim.diagnostic.severity.ERROR] = icons.diagnostic.error,
[vim.diagnostic.severity.WARN] = icons.diagnostic.warn,
[vim.diagnostic.severity.INFO] = icons.diagnostic.info,
[vim.diagnostic.severity.HINT] = icons.diagnostic.hint,
})[diagnostic.severity] .. " " .. diagnostic.message .. " "
end,
},
float = {
show_header = false,
source = true,
border = border,
},
update_in_insert = false,
severity_sort = true,
})
-- Keybind for listing diagnostics
vim.keymap.set("n", "<C-w>D", vim.diagnostic.setqflist)
-- Keybind for toggling diagnostics
vim.keymap.set("n", "<leader>td", function()
vim.diagnostic.enable(not vim.diagnostic.is_enabled({ bufnr = 0 }), { bufnr = 0 })
end)
---------------------------------
-- LSP keybinds and configuration
---------------------------------
vim.api.nvim_create_autocmd("LspAttach", {
group = vim.api.nvim_create_augroup("lsp-attach-config", { clear = true }),
callback = function(args)
local client = assert(vim.lsp.get_client_by_id(args.data.client_id))
local bufnr = args.buf
-- Show hover documentation
vim.keymap.set("n", "K", function()
vim.lsp.buf.hover({ border = border })
end, { buffer = bufnr })
-- Show signature help
vim.keymap.set("i", "<C-s>", function()
vim.lsp.buf.signature_help({ border = border })
end, { buffer = bufnr })
-- Code actions
vim.keymap.set({ "n", "x" }, "gra", "<cmd>lua require('fastaction').code_action()<cr>", { buffer = bufnr })
-- Go to definition
vim.keymap.set("n", "gd", vim.lsp.buf.definition, { buffer = bufnr })
vim.keymap.set("n", "gD", "<cmd>Glance definitions<cr>", { buffer = bufnr })
-- Go to type definition
vim.keymap.set("n", "gt", function()
vim.lsp.buf.type_definition({
on_list = function(t)
vim.lsp.util.show_document(t.items[1].user_data, "utf-8", { focus = true })
end,
})
end, { buffer = bufnr })
vim.keymap.set("n", "gT", "<cmd>Glance type_definitions<cr>", { buffer = bufnr })
-- List all implementations
vim.keymap.set("n", "<C-w>i", "<cmd>Glance implementations<cr>", { buffer = bufnr })
vim.keymap.set("n", "<C-w>I", vim.lsp.buf.implementation, { buffer = bufnr })
-- List all references
vim.keymap.set("n", "<C-w>r", "<cmd>Glance references<cr>", { buffer = bufnr })
vim.keymap.set("n", "<C-w>R", vim.lsp.buf.references, { buffer = bufnr })
-- List all symbols
vim.keymap.set("n", "<C-w>S", vim.lsp.buf.document_symbol, { buffer = bufnr })
-- Rename symbol under cursor
vim.keymap.set("n", "grn", vim.lsp.buf.rename, { buffer = bufnr })
-- Toggle inlay hints
if client:supports_method("textDocument/inlayHint") then
vim.keymap.set("n", "<leader>th", function()
vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled({ bufnr = bufnr }), { bufnr = bufnr })
end, { buffer = bufnr })
end
-- Disable inlay hints by default
vim.lsp.inlay_hint.enable(false, { bufnr = bufnr })
-- Use LSP-provided folding if possible
if client:supports_method("textDocument/foldingRange") then
vim.wo.foldmethod = "expr"
vim.wo.foldexpr = "v:lua.vim.lsp.foldexpr()"
end
-- Disable semantic highlighting for some LSPs
for _, name in ipairs({ "dockerls" }) do
if client.name == name then
client.server_capabilities.semanticTokensProvider = nil
end
end
-- Disable hover for some LSPs
for _, name in ipairs({ "ruff_lsp" }) do
if client.name == name then
client.server_capabilities.hoverProvider = nil
end
end
-- Disable formatting for some LSPs
for _, name in ipairs({ "jsonls", "biome", "tsserver", "vtsls", "basedpyright" }) do
if client.name == name then
client.server_capabilities.documentFormattingProvider = nil
client.server_capabilities.documentRangeFormattingProvider = nil
end
end
end,
})
-------------------------
-- Filetype configuration
-------------------------
vim.filetype.add({ filename = { ["go.mod"] = "gomod" } })
vim.filetype.add({ filename = { [".ignore"] = "gitignore" } })
vim.filetype.add({ pattern = { ["queries/.*%.scm"] = "query" } })
vim.filetype.add({ pattern = { ["%.husky/.*"] = "sh" } })
vim.filetype.add({ extension = { ["gitconfig"] = "gitconfig" } })
vim.filetype.add({ pattern = { ["%.env%..*"] = "sh" } })
vim.filetype.add({ extension = { ["add"] = "conf" } })
vim.filetype.add({ pattern = { ["%.default%-%w+%-packages"] = "conf" } })
vim.filetype.add({ extension = { ["jinja"] = "jinja", ["jinja2"] = "jinja", ["j2"] = "jinja" } })
vim.filetype.add({ pattern = { [".*/%.github[%w/]+workflows[%w/]+.*%.ya?ml"] = "yaml.github" } })
--------------------------
-- lazy.nvim configuration
--------------------------
local packages_path = vim.fn.stdpath("data") .. "/lazy"
-- Helper function to install packages before lazy.nvim is loaded
---@param name string
---@param alias string?
local function install_package(name, alias)
---@type unknown, unknown, string, string
local _, _, owner, repo = name:find("(.+)/(.+)")
local path = ("%s/%s"):format(packages_path, alias or repo)
if not vim.uv.fs_stat(path) then
vim.notify(("Installing %s/%s..."):format(owner, repo), vim.log.levels.INFO)
vim
.system({
"git",
"clone",
"--filter=blob:none",
"--single-branch",
("https://github.com/%s/%s.git"):format(owner, repo),
path,
})
:wait()
end
vim.opt.runtimepath:prepend(path)
end
install_package("folke/lazy.nvim")
require("lazy").setup("packages", {
change_detection = { notify = false },
dev = { path = "~/Developer/Git/" },
ui = { backdrop = 100, border = border },
install = { colorscheme = { "catppuccin-mocha" } },
})