-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
505 lines (428 loc) · 15.4 KB
/
init.lua
File metadata and controls
505 lines (428 loc) · 15.4 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
local o = vim.o
local g = vim.g
local v = vim.v
local bo = vim.bo
local wo = vim.wo
local opt = vim.opt
local map = vim.keymap.set
local au = vim.api.nvim_create_autocmd
local ucmd = vim.api.nvim_create_user_command
vim.cmd.colorscheme("dook")
-- ___ PLUGINS ________________________________________________
g.smoothie_remapped_commands = { "<C-D>", "<C-U>" }
local pkgs = {
{ src = "tpope/vim-sleuth" }, -- automatic indentation mode detection
{ src = "psliwka/vim-smoothie" }, -- smooth scrolling
{ src = "stevearc/conform.nvim" }, -- ebin meta formatter thingy
{ src = "kylechui/nvim-surround" }, -- (un)surround stuff
{ src = "nvim-mini/mini.extra" }, -- extra pickers for mini.pick
{ src = "nvim-mini/mini.pick" }, -- general pickers
{ src = "shirosaki/tabular", version = "fix_leading_spaces" }, -- multiline alignment
{ src = "nvim-treesitter/nvim-treesitter" }, -- a lot of functionality with ASTs
{ src = "nvim-treesitter/nvim-treesitter-textobjects" }, -- define bindings for actions with AST text objects
{ src = "nvim-treesitter/nvim-treesitter-context" }, -- show current function name when scrolling
}
for _, pkg in ipairs(pkgs) do pkg.src = "https://github.com/" .. pkg.src end
vim.pack.add(pkgs)
-- netrw settings
g.netrw_liststyle = 3
-- g.netrw_banner = 0
require("nvim-treesitter").install({
"c", "cpp", "go", "javascript", "json", "python", "comment",
"typescript", "c_sharp", "haskell", "markdown", "markdown_inline",
"make", "html", "gitignore", "gitcommit", "arduino", "yaml",
"sql", "css", "dockerfile", "bash", "rust", "query", "lua",
})
au("PackChanged", { command = "TSUpdate" })
-- highlighting
au("FileType", {
callback = function() pcall(vim.treesitter.start) end,
})
require("nvim-treesitter-textobjects").setup({
select = { lookahead = true, set_jumps = true },
})
-- for some langs use external formatters with range formatting provided by conform
require("conform").setup({
formatters_by_ft = {
sh = { "shfmt", "shellcheck" },
zsh = { "shfmt", "shellcheck" },
bash = { "shfmt", "shellcheck" },
go = { "gofmt" },
cs = { "clang-format" },
js = { "clang-format" },
java = { "clang-format" },
json = { "jq" },
lua = { "stylua" },
},
})
-- use conform's formatexpr for filetypes that have a formatter set
au("FileType", {
pattern = vim.tbl_keys(require("conform").formatters_by_ft),
callback = function()
bo.formatexpr = 'v:lua.require("conform").formatexpr()'
end,
})
-- boilerplate
require("nvim-surround").setup()
require("mini.extra").setup()
-- mini.pick without gay icons
local pick = require("mini.pick")
pick.setup({ source = { show = pick.default_show }, mappings = { choose_marked = "<C-q>" } })
vim.cmd([[
packadd cfilter " :Cfilter to filter quickfix list items
packadd nvim.difftool " :DiffTool over directories using quickfix list
]])
-- ___ KEYBINDS _______________________________________________
g.mapleader = " "
-- alternate way to save
map("n", "<leader>w", ":up<cr>")
-- make some backward-jumping operators inclusive
map("o", "F", "vF")
map("o", "T", "vT")
map("o", "b", "vb")
map("o", "B", "vB")
map("o", "^", "v^")
map("o", "0", "v0")
-- smart quit
map("n", "<leader>q", function()
if vim.fn.winnr("$") == 1 and vim.fn.tabpagenr("$") == 1 then
vim.cmd("qa!")
return
end
local win_count = 0
local current_buf = vim.api.nvim_get_current_buf()
for _, win in ipairs(vim.api.nvim_list_wins()) do
if vim.api.nvim_win_get_buf(win) == current_buf then
win_count = win_count + 1
end
end
if win_count > 1 then
vim.cmd("close!")
return
end
vim.cmd("bdelete!")
end)
-- kill all buffers
map("n", "<leader>Q", ":qa!<cr>")
-- format
map("n", "Q", "gqq")
map("v", "Q", "gq")
-- better tabbing
map("v", "<", "<gv")
map("v", ">", ">gv")
-- vertical split shortcut
map("n", "<c-w>v", ":vsplit<cr>")
-- copy to system clipboard
map({ "n", "v", "o" }, "Y", '"+y')
map("n", "YY", '"+yy')
-- netrw (file explorer)
map("n", "<leader>n", ":Explore<cr>")
-- paste over a selection without changing contents of the unnamed register
map("v", "<leader>p", '"_dP')
-- save file as sudo on files that require root permission
vim.cmd.cabbr([[w!! execute 'silent! write !sudo tee %' <bar> edit!]])
-- tabular plugin shortcut
map({ "n", "v" }, "<c-t>", ":Tab /\\v")
-- extended regex in searches
map({ "n", "v", "o" }, "/", "/\\v")
-- picker
map("n", "<leader>ff", ":Pick files<cr>")
map("n", "<leader>fb", ":Pick buffers<cr>")
map("n", "<leader>fg", ":Pick grep_live<cr>")
map("n", "<leader>fm", ":Pick manpages<cr>")
map("n", "<leader>fh", ":Pick help<cr>")
-- LSP
map("n", "grd", vim.lsp.buf.definition)
map("n", "grD", vim.lsp.buf.declaration)
map("n", "<c-w>[", "<cmd>vsplit<cr><cmd>lua vim.lsp.buf.definition()<cr>") -- counterpart to <c-w>]
map("n", "gre", "<cmd>Pick diagnostic<cr>")
map("n", "grr", [[<cmd>Pick lsp scope="references"<cr>]])
-- comment below/above/at the end of current line
local function comment(move)
-- get current commenstring based on treesitter; stolen from neovim runtime
local lhs, rhs = (function()
local ref_position = vim.api.nvim_win_get_cursor(0)
local buf_cs = vim.bo.commentstring
local ts_parser = vim.treesitter.get_parser(0, "", { error = false })
if not ts_parser then
return buf_cs
end
local row, col = ref_position[1] - 1, ref_position[2]
local ref_range = { row, col, row, col + 1 }
local caps = vim.treesitter.get_captures_at_pos(0, row, col)
for i = #caps, 1, -1 do
local id, metadata = caps[i].id, caps[i].metadata
local md_cms = metadata["bo.commentstring"] or metadata[id] and metadata[id]["bo.commentstring"]
if md_cms then
return md_cms
end
end
local ts_cs, res_level = nil, 0
local function traverse(lang_tree, level)
if not lang_tree:contains(ref_range) then
return
end
local lang = lang_tree:lang()
local filetypes = vim.treesitter.language.get_filetypes(lang)
for _, ft in ipairs(filetypes) do
local cur_cs = vim.filetype.get_option(ft, "commentstring")
if cur_cs ~= "" and level > res_level then
ts_cs = cur_cs
end
end
for _, child_lang_tree in pairs(lang_tree:children()) do
traverse(child_lang_tree, level + 1)
end
end
traverse(ts_parser, 1)
return ts_cs or buf_cs
end)():match("^(.-)%%s(.*)$")
local shiftstr = string.rep(vim.keycode("<Left>"), #rhs)
vim.fn.feedkeys(move .. lhs .. rhs .. shiftstr, "n")
end
map("n", "gco", function() comment("o") end)
map("n", "gcO", function() comment("O") end)
map("n", "gcA", function() comment("A ") end)
-- keep cursor in place when joining lines
map("n", "J", "mzJ`z:delmarks z<cr>")
-- update hugo content dates
vim.cmd([[
function! HugoTimeUpdate_f()
let currPos = getpos(".")
if search('^draft = true$')
/\v^date \= '\zs([^']*)\ze'$/s//\=substitute(system('date -I'), '\n', '', 'g')/
endif
if search('^lastmod = ')
/\v^lastmod \= '\zs([^']*)\ze'$/s//\=substitute(system('date -I'), '\n', '', 'g')/
endif
call cursor(currPos[1], currPos[2])
endfunction
command! HugoTimeUpdate call HugoTimeUpdate_f()
]])
-- treesitter textobjects mappings
local tssel = require("nvim-treesitter-textobjects.select").select_textobject
local tsmov = require("nvim-treesitter-textobjects.move")
local tsswp = require("nvim-treesitter-textobjects.swap")
map("n", "<leader>s", function() tsswp.swap_next("@parameter.inner") end)
map("n", "<leader>a", function() tsswp.swap_previous("@parameter.inner") end)
map({ "v", "o" }, "if", function() tssel("@function.inner", "textobjects") end)
map({ "v", "o" }, "af", function() tssel("@function.outer", "textobjects") end)
map({ "v", "o" }, "ic", function() tssel("@class.inner", "textobjects") end)
map({ "v", "o" }, "ac", function() tssel("@class.outer", "textobjects") end)
map({ "v", "o" }, "ia", function() tssel("@parameter.inner", "textobjects") end)
map({ "v", "o" }, "aa", function() tssel("@parameter.outer", "textobjects") end)
map({ "v", "o" }, "iC", function() tssel("@comment.inner", "textobjects") end)
map({ "v", "o" }, "aC", function() tssel("@comment.outer", "textobjects") end)
map({ "n", "v", "o" }, "]f", function() tsmov.goto_next_start("@function.outer") end)
map({ "n", "v", "o" }, "]F", function() tsmov.goto_next_end("@function.outer") end)
map({ "n", "v", "o" }, "[f", function() tsmov.goto_previous_start("@function.outer") end)
map({ "n", "v", "o" }, "[F", function() tsmov.goto_previous_end("@function.outer") end)
-- hide autocompletion when trying to view signature help
map("i", "<c-s>", function()
vim.cmd.call([[feedkeys("\<c-e>", "n")]])
vim.lsp.buf.signature_help()
end)
-- show git blame for current line(s)
map({"n", "v"}, "<leader>gb", function()
local start_line, end_line
if vim.fn.mode() == "v" or vim.fn.mode() == "V" then
start_line = vim.fn.line("v")
end_line = vim.fn.line(".")
else
start_line = vim.fn.line(".")
end_line = start_line
end
local file = vim.fn.expand("%")
local cmd = string.format("git blame -c -L %d,%d -- %s |& tr '\t' ' '", start_line, end_line, file)
local out = vim.fn.systemlist(cmd)
if start_line == end_line then
vim.api.nvim_echo({{out[1], "Normal"}}, false, {})
else
for _, line in ipairs(out) do
vim.api.nvim_echo({{line, "Normal"}}, false, {})
end
end
end)
ucmd("Grep", function(opts)
vim.cmd("silent! cclose")
vim.cmd("silent grep! " .. opts.args)
vim.cmd("copen")
end, { nargs = "+" })
-- take current command prefix into account when using <c-p>/<c-n>
map("c", "<c-p>", "<up>")
map("c", "<c-n>", "<down>")
-- easier completion accept
map({ "i", "c" }, "<c-j>", "<c-y>")
-- ___ GENERAL OPTIONS ________________________________________
o.smartindent = true
-- set window title
o.title = true
-- when to show status line
o.ruler = false
o.laststatus = 1
au("LspAttach", { command = "set laststatus=2" })
-- keep some space betwwen cursor and window edges
o.scrolloff = 2
o.sidescrolloff = 5
-- don't wrap long lines
o.wrap = false
-- splits open at the bottom and right
o.splitbelow = true
o.splitright = true
-- persistent undo
o.undofile = true
-- search is case insensitive unless upper case character is in the query
o.ignorecase = true
o.smartcase = true
-- lazy redraw - screen will not be redrawn while executing macros etc
o.lz = true
-- time to wait for a mapped sequence to complete
o.timeoutlen = 750
-- hack to put cursor at the beggining of a tab instead of the end
o.list = true
o.lcs = "tab: "
-- disable right click popup menu
o.mousemodel = "extend"
-- floating window border style
o.winborder = "rounded"
-- insert mode completion options
o.autocomplete = true
o.complete = "o,.,w,f"
o.completeopt = "fuzzy,menuone,noselect,popup"
o.pumheight = 7
o.pummaxwidth = 80
opt.shortmess:prepend("c") -- avoid having to press enter on snippet completion
-- indentation settings
opt.cinoptions:append({ ":0", "g0", "N-s" })
opt.cinkeys:remove("0#")
-- hide end-of-buffer tildes
opt.fillchars:append({ eob = " " })
-- automatically deletes all trailing whitespace and newlines at end of file on save
au("BufWritePre", {
callback = function()
local curpos = vim.fn.getpos(".")
vim.cmd([[%s/\v(\s+$|\n+%$)//e]])
vim.fn.setpos(".", curpos)
end,
})
-- remove line highlighting on defocus
o.cursorline = false -- to prevent cursorline showing in multiple splits when started with -o/-O
au({ "BufEnter", "WinEnter", "FocusGained" }, { command = "setlocal cursorline" })
au({ "BufLeave", "WinLeave", "FocusLost" }, { command = "setlocal nocursorline" })
-- automatically normalize window sizes when neovim gets resized
au("VimResized", { command = "wincmd =" })
-- hide context lines when in cmdline/search so that we have a clear view of
-- what we're doing
au({ "CmdlineEnter", "CmdlineLeave" }, { command = "TSContext toggle" })
-- don't save empty windows on :mksession
opt.sessionoptions:remove("blank")
-- briefly highlight yanked region
au("TextYankPost", { callback = function() vim.highlight.on_yank() end, })
-- disable dumb bloat
o.signcolumn = "no"
-- .h files are C not C++
g.c_syntax_for_h = 1
-- hybrid numbers - relative in normal mode, absolute in insert mode
o.nu = true
o.rnu = false -- to prevent relative numbers in multiple splits when started with -o/-O
au({ "BufEnter", "InsertLeave", "WinEnter", "FocusGained" }, { command = "if &nu | setlocal rnu" })
au({ "BufLeave", "InsertEnter", "WinLeave", "FocusLost" }, { command = "setlocal nornu" })
au("TermOpen", { command = "setlocal nonu nornu" })
-- fallback commentstring
au("BufEnter", { command = [[if empty(&cms) | setlocal cms=#\ %s]] })
-- lsp diagnostic text
vim.diagnostic.config({
virtual_text = {
severity = { min = vim.diagnostic.severity.WARN },
},
signs = {
numhl = {
[vim.diagnostic.severity.ERROR] = "DiagnosticLineNrError",
[vim.diagnostic.severity.WARN] = "DiagnosticLineNrWarn",
},
},
})
-- load lsp servers' confs
vim.lsp.config("*", { root_markers = { ".git" }})
for name, _ in vim.fs.dir(vim.fn.stdpath("config") .. "/lsp/") do
vim.lsp.enable({ name:match("(.+)%.lua$") })
end
-- enable lsp completion (snippets etc)
au("LspAttach", {
callback = function(ev)
vim.lsp.completion.enable(true, ev.data.client_id, ev.buf)
end,
})
-- cmdline autocompletion
au("CmdlineChanged", { pattern = ":", command = "call wildtrigger()" })
o.wildmode = "noselect:lastused,full"
o.wildoptions = "pum"
-- <c-x><c-f> complete menu stays open as long as you accept tokens
au("CompleteDone", {
callback = function()
local e = v.event
if e.complete_type == "files" and e.reason == "accept" then
vim.cmd.call([[feedkeys("\<c-x>\<c-f>", "n")]])
end
end,
})
-- experimental new command-line features
require("vim._core.ui2").enable({})
-- tab line
function safari_tabline()
local tab_count = vim.fn.tabpagenr("$")
local tab_width = math.floor(o.columns / tab_count)
local s = ""
for i = 1, tab_count do
if i == vim.fn.tabpagenr() then
s = s .. "%#TabLineSel#"
else
s = s .. "%#TabLine#"
end
local bufname = vim.fn.bufname(vim.fn.tabpagebuflist(i)[vim.fn.tabpagewinnr(i)])
if bufname == "" then
bufname = "[No Name]"
else
bufname = vim.fn.fnamemodify(bufname, ":t")
end
local label = i .. ":" .. bufname
label = " " .. label .. " "
if #label < tab_width then
label = label .. string.rep(" ", tab_width - #label)
else
label = string.sub(label, 1, tab_width)
end
s = s .. label
end
return s
end
o.tabline = "%!v:lua.safari_tabline()"
-- fold appearence
function my_fold_text()
local bufnr = vim.api.nvim_get_current_buf()
local line = vim.api.nvim_buf_get_lines(bufnr, v.foldstart - 1, v.foldstart, false)[1]
local ts = bo[bufnr].tabstop or 8
local expanded = line:gsub("\t", string.rep(" ", ts))
return expanded .. " ⋅⋅⋅ (" .. v.foldend - v.foldstart + 1 .. " lines) "
end
o.foldtext = "v:lua.my_fold_text()"
-- quickfix list appearence
function my_qftf(info)
local out = {}
local items = vim.fn.getqflist()
for i, item in ipairs(items) do
local filename = item.filename
if (not filename or filename == '') and item.bufnr and tonumber(item.bufnr) ~= 0 then
filename = vim.fn.bufname(item.bufnr)
end
filename = filename ~= '' and filename or "[NoFile]"
local lnum = item.lnum or 0
local msg = item.text or item.pattern or ''
table.insert(out, string.format("%s|%d| %s", filename, tonumber(lnum), msg))
end
return out
end
o.qftf = "v:lua.my_qftf"
o.statusline = " %<%f %h%w%m%r "
.. "%{% luaeval('vim.diagnostic.status()') %} "
.. "%=%{ &ft } %{ &ff } "