-
Notifications
You must be signed in to change notification settings - Fork 142
/
init.lua
774 lines (687 loc) · 23.9 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
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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
vim.fn.system({
"git",
"clone",
"--filter=blob:none",
"https://github.com/folke/lazy.nvim.git",
"--branch=stable", -- latest stable release
lazypath,
})
end
vim.opt.rtp:prepend(lazypath)
-- ChangeBackground changes the background mode based on macOS's `Appearance
-- setting.
local function change_background()
local m = vim.fn.system("defaults read -g AppleInterfaceStyle")
m = m:gsub("%s+", "") -- trim whitespace
if m == "Dark" then
vim.o.background = "dark"
else
vim.o.background = "light"
end
end
-- run :GoBuild or :GoTestCompile based on the go file
local function build_go_files()
if vim.endswith(vim.api.nvim_buf_get_name(0), "_test.go") then
vim.cmd("GoTestCompile")
else
vim.cmd("GoBuild")
end
end
----------------
--- plugins ---
----------------
require("lazy").setup({
-- colorscheme
{
"ellisonleao/gruvbox.nvim",
priority = 1000, -- make sure to load this before all the other start plugins
config = function ()
change_background()
require("gruvbox").setup({
contrast = "hard"
})
vim.cmd([[colorscheme gruvbox]])
end,
},
-- statusline
{
"nvim-lualine/lualine.nvim",
dependencies = { 'nvim-tree/nvim-web-devicons' },
config = function ()
require("lualine").setup({
options = { theme = 'gruvbox' },
sections = {
lualine_c = {
{
'filename',
file_status = true, -- displays file status (readonly status, modified status)
path = 2 -- 0 = just filename, 1 = relative path, 2 = absolute path
}
}
}
})
end,
},
-- you know the drill
{
"fatih/vim-go",
config = function ()
-- we disable most of these features because treesitter and nvim-lsp
-- take care of it
vim.g['go_gopls_enabled'] = 0
vim.g['go_code_completion_enabled'] = 0
vim.g['go_fmt_autosave'] = 0
vim.g['go_imports_autosave'] = 0
vim.g['go_mod_fmt_autosave'] = 0
vim.g['go_doc_keywordprg_enabled'] = 0
vim.g['go_def_mapping_enabled'] = 0
vim.g['go_textobj_enabled'] = 0
vim.g['go_list_type'] = 'quickfix'
end,
},
-- search selection via *
{ 'bronson/vim-visual-star-search' },
-- testing framework
{
"vim-test/vim-test",
config = function ()
vim.g['test#strategy'] = 'neovim'
vim.g['test#neovim#start_normal'] = '1'
vim.g['test#neovim#term_position'] = 'vert'
end,
},
{
'dinhhuy258/git.nvim',
config = function ()
require("git").setup()
end,
},
-- file explorer
{
"nvim-tree/nvim-tree.lua",
version = "*",
dependencies = { 'nvim-tree/nvim-web-devicons' },
config = function()
require("nvim-tree").setup({
sort_by = "case_sensitive",
filters = {
dotfiles = true,
},
on_attach = function(bufnr)
local api = require('nvim-tree.api')
local function opts(desc)
return {
desc = 'nvim-tree: ' .. desc,
buffer = bufnr,
noremap = true,
silent = true,
nowait = true,
}
end
api.config.mappings.default_on_attach(bufnr)
vim.keymap.set('n', 's', api.node.open.vertical, opts('Open: Vertical Split'))
vim.keymap.set('n', 'i', api.node.open.horizontal, opts('Open: Horizontal Split'))
vim.keymap.set('n', 'u', api.tree.change_root_to_parent, opts('Up'))
end
})
end,
},
-- save my last cursor position
{
"ethanholz/nvim-lastplace",
config = function()
require("nvim-lastplace").setup({
lastplace_ignore_buftype = {"quickfix", "nofile", "help"},
lastplace_ignore_filetype = {"gitcommit", "gitrebase", "svn", "hgcommit"},
lastplace_open_folds = true
})
end,
},
-- markdown
{
"iamcco/markdown-preview.nvim",
dependencies = {
"zhaozg/vim-diagram",
"aklt/plantuml-syntax",
},
build = function()
vim.fn["mkdp#util#install"]()
end,
ft = "markdown",
cmd = { "MarkdownPreview" },
},
-- commenting out lines
{
"numToStr/Comment.nvim",
config = function()
require('Comment').setup({
opleader = {
---Block-comment keymap
block = '<Nop>',
},
})
end
},
{
"AndrewRadev/splitjoin.vim"
},
-- fzf extension for telescope with better speed
{
"nvim-telescope/telescope-fzf-native.nvim", run = 'make'
},
{'nvim-telescope/telescope-ui-select.nvim' },
-- fuzzy finder framework
{
"nvim-telescope/telescope.nvim",
tag = '0.1.4',
dependencies = {
"nvim-lua/plenary.nvim" ,
"nvim-treesitter/nvim-treesitter",
"nvim-tree/nvim-web-devicons",
},
config = function ()
require("telescope").setup({
extensions = {
fzf = {
fuzzy = true, -- false will only do exact matching
override_generic_sorter = true, -- override the generic sorter
override_file_sorter = true, -- override the file sorter
case_mode = "smart_case", -- or "ignore_case" or "respect_case"
-- the default case_mode is "smart_case"
}
}
})
-- To get fzf loaded and working with telescope, you need to call
-- load_extension, somewhere after setup function:
require('telescope').load_extension('fzf')
-- To get ui-select loaded and working with telescope, you need to call
-- load_extension, somewhere after setup function:
require("telescope").load_extension("ui-select")
end,
},
-- lsp-config
{
"neovim/nvim-lspconfig",
config = function ()
util = require "lspconfig/util"
local capabilities = require('cmp_nvim_lsp').default_capabilities(capabilities)
capabilities.textDocument.completion.completionItem.snippetSupport = true
require("lspconfig").gopls.setup({
capabilities = capabilities,
flags = { debounce_text_changes = 200 },
settings = {
gopls = {
usePlaceholders = true,
gofumpt = true,
analyses = {
nilness = true,
unusedparams = true,
unusedwrite = true,
useany = true,
},
codelenses = {
gc_details = false,
generate = true,
regenerate_cgo = true,
run_govulncheck = true,
test = true,
tidy = true,
upgrade_dependency = true,
vendor = true,
},
experimentalPostfixCompletions = true,
completeUnimported = true,
staticcheck = true,
directoryFilters = { "-.git", "-node_modules" },
semanticTokens = true,
hints = {
assignVariableTypes = true,
compositeLiteralFields = true,
compositeLiteralTypes = true,
constantValues = true,
functionTypeParameters = true,
parameterNames = true,
rangeVariableTypes = true,
},
},
},
})
end,
},
-- Alternate between files, such as foo.go and foo_test.go
{
"rgroli/other.nvim",
config = function ()
require("other-nvim").setup({
rememberBuffers = false,
mappings = {
"rails",
"golang",
},
})
vim.api.nvim_create_user_command('A', function(opts)
require('other-nvim').open(opts.fargs[1])
end, {nargs = '*'})
vim.api.nvim_create_user_command('AV', function(opts)
require('other-nvim').openVSplit(opts.fargs[1])
end, {nargs = '*'})
vim.api.nvim_create_user_command('AS', function(opts)
require('other-nvim').openSplit(opts.fargs[1])
end, {nargs = '*'})
end,
},
-- Highlight, edit, and navigate code
{
'nvim-treesitter/nvim-treesitter',
dependencies = {
'nvim-treesitter/nvim-treesitter-textobjects',
},
build = ":TSUpdate",
config = function()
require('nvim-treesitter.configs').setup({
ensure_installed = {
'go',
'gomod',
'proto',
'lua',
'ruby',
'vimdoc',
'vim',
'bash',
'fish',
'json',
'markdown',
'markdown_inline',
'mermaid',
},
indent = { enable = true },
incremental_selection = {
enable = true,
keymaps = {
init_selection = "<space>", -- maps in normal mode to init the node/scope selection with space
node_incremental = "<space>", -- increment to the upper named parent
node_decremental = "<bs>", -- decrement to the previous node
scope_incremental = "<tab>", -- increment to the upper scope (as defined in locals.scm)
},
},
autopairs = {
enable = true,
},
highlight = {
enable = true,
-- Disable slow treesitter highlight for large files
disable = function(lang, buf)
local max_filesize = 100 * 1024 -- 100 KB
local ok, stats = pcall(vim.loop.fs_stat, vim.api.nvim_buf_get_name(buf))
if ok and stats and stats.size > max_filesize then
return true
end
end,
-- Setting this to true will run `:h syntax` and tree-sitter at the same time.
-- Set this to `true` if you depend on 'syntax' being enabled (like for indentation).
-- Using this option may slow down your editor, and you may see some duplicate highlights.
-- Instead of true it can also be a list of languages
additional_vim_regex_highlighting = false,
},
textobjects = {
select = {
enable = true,
lookahead = true, -- Automatically jump forward to textobj, similar to targets.vim
keymaps = {
-- You can use the capture groups defined in textobjects.scm
['aa'] = '@parameter.outer',
['ia'] = '@parameter.inner',
['af'] = '@function.outer',
['if'] = '@function.inner',
['ac'] = '@class.outer',
['ic'] = '@class.inner',
["iB"] = "@block.inner",
["aB"] = "@block.outer",
},
},
move = {
enable = true,
set_jumps = true, -- whether to set jumps in the jumplist
goto_next_start = {
[']]'] = '@function.outer',
},
goto_next_end = {
[']['] = '@function.outer',
},
goto_previous_start = {
['[['] = '@function.outer',
},
goto_previous_end = {
['[]'] = '@function.outer',
},
},
swap = {
enable = true,
swap_next = {
['<leader>sn'] = '@parameter.inner',
},
swap_previous = {
['<leader>sp'] = '@parameter.inner',
},
},
},
})
end,
},
{
"windwp/nvim-autopairs",
config = function()
require("nvim-autopairs").setup {
check_ts = true,
}
end
},
{
"L3MON4D3/LuaSnip",
dependencies = { "rafamadriz/friendly-snippets" },
config = function()
require("luasnip.loaders.from_vscode").lazy_load()
end
},
-- autocompletion
{
"hrsh7th/nvim-cmp",
dependencies = {
"hrsh7th/cmp-nvim-lsp",
"hrsh7th/cmp-buffer",
"L3MON4D3/LuaSnip",
"saadparwaiz1/cmp_luasnip",
"onsails/lspkind-nvim",
},
config = function()
local cmp = require("cmp")
local luasnip = require("luasnip")
local lspkind = require("lspkind")
local cmp_autopairs = require("nvim-autopairs.completion.cmp")
cmp.event:on("confirm_done", cmp_autopairs.on_confirm_done())
luasnip.config.setup {}
local has_words_before = function()
unpack = unpack or table.unpack
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
end
require('cmp').setup({
snippet = {
expand = function(args)
luasnip.lsp_expand(args.body)
end,
},
formatting = {
format = lspkind.cmp_format {
with_text = true,
menu = {
buffer = "[Buffer]",
nvim_lsp = "[LSP]",
nvim_lua = "[Lua]",
},
},
},
mapping = cmp.mapping.preset.insert {
['<C-n>'] = cmp.mapping.select_next_item(),
['<C-p>'] = cmp.mapping.select_prev_item(),
['<C-d>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
['<CR>'] = cmp.mapping.confirm { select = true },
-- ['<Tab>'] = cmp.mapping(function(fallback)
-- if cmp.visible() then
-- cmp.select_next_item()
-- elseif luasnip.expand_or_locally_jumpable() then
-- luasnip.expand_or_jump()
-- elseif has_words_before() then
-- cmp.complete()
-- else
-- fallback()
-- end
['<Tab>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif luasnip.jumpable(1) then
luasnip.jump()
elseif luasnip.expand() then
luasnip.expand()
else
fallback()
end
end, { 'i', 's' }),
['<S-Tab>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif luasnip.jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end, { 'i', 's' }),
},
-- don't auto select item
preselect = cmp.PreselectMode.None,
window = {
documentation = cmp.config.window.bordered(),
},
view = {
entries = {
name = "custom",
selection_order = "near_cursor",
},
},
confirm_opts = {
behavior = cmp.ConfirmBehavior.Insert,
},
sources = {
{ name = 'nvim_lsp' },
{ name = "luasnip", keyword_length = 2},
{ name = "buffer", keyword_length = 5},
},
})
end,
},
})
----------------
--- SETTINGS ---
----------------
-- disable netrw at the very start of our init.lua, because we use nvim-tree
vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1
vim.opt.termguicolors = true -- Enable 24-bit RGB colors
vim.opt.number = true -- Show line numbers
vim.opt.showmatch = true -- Highlight matching parenthesis
vim.opt.splitright = true -- Split windows right to the current windows
vim.opt.splitbelow = true -- Split windows below to the current windows
vim.opt.autowrite = true -- Automatically save before :next, :make etc.
vim.opt.autochdir = true -- Change CWD when I open a file
vim.opt.mouse = 'a' -- Enable mouse support
vim.opt.clipboard = 'unnamedplus' -- Copy/paste to system clipboard
vim.opt.swapfile = false -- Don't use swapfile
vim.opt.ignorecase = true -- Search case insensitive...
vim.opt.smartcase = true -- ... but not it begins with upper case
vim.opt.completeopt = 'menuone,noinsert,noselect' -- Autocomplete options
vim.opt.undofile = true
vim.opt.undodir = vim.fn.stdpath("data") .. "undo"
-- Indent Settings
-- I'm in the Spaces camp (sorry Tabs folks), so I'm using a combination of
-- settings to insert spaces all the time.
vim.opt.expandtab = true -- expand tabs into spaces
vim.opt.shiftwidth = 2 -- number of spaces to use for each step of indent.
vim.opt.tabstop = 2 -- number of spaces a TAB counts for
vim.opt.autoindent = true -- copy indent from current line when starting a new line
vim.opt.wrap = true
-- This comes first, because we have mappings that depend on leader
-- With a map leader it's possible to do extra key combinations
-- i.e: <leader>w saves the current file
vim.g.mapleader = ','
-- Fast saving
vim.keymap.set('n', '<Leader>w', ':write!<CR>')
vim.keymap.set('n', '<Leader>q', ':q!<CR>', { silent = true })
-- Some useful quickfix shortcuts for quickfix
vim.keymap.set('n', '<C-n>', '<cmd>cnext<CR>zz')
vim.keymap.set('n', '<C-m>', '<cmd>cprev<CR>zz')
vim.keymap.set('n', '<leader>a', '<cmd>cclose<CR>')
-- Exit on jj and jk
vim.keymap.set('n', 'j', 'gj')
vim.keymap.set('n', 'k', 'gk')
-- Exit on jj and jk
vim.keymap.set('i', 'jj', '<ESC>')
vim.keymap.set('i', 'jk', '<ESC>')
-- Remove search highlight
vim.keymap.set('n', '<Leader><space>', ':nohlsearch<CR>')
-- Search mappings: These will make it so that going to the next one in a
-- search will center on the line it's found in.
vim.keymap.set('n', 'n', 'nzzzv', {noremap = true})
vim.keymap.set('n', 'N', 'Nzzzv', {noremap = true})
-- Don't jump forward if I higlight and search for a word
local function stay_star()
local sview = vim.fn.winsaveview()
local args = string.format("keepjumps keeppatterns execute %q", "sil normal! *")
vim.api.nvim_command(args)
vim.fn.winrestview(sview)
end
vim.keymap.set('n', '*', stay_star, {noremap = true, silent = true})
-- We don't need this keymap, but here we are. If I do a ctrl-v and select
-- lines vertically, insert stuff, they get lost for all lines if we use
-- ctrl-c, but not if we use ESC. So just let's assume Ctrl-c is ESC.
vim.keymap.set('i', '<C-c>', '<ESC>')
-- If I visually select words and paste from clipboard, don't replace my
-- clipboard with the selected word, instead keep my old word in the
-- clipboard
vim.keymap.set("x", "p", "\"_dP")
-- rename the word under the cursor
vim.keymap.set("n", "<leader>rw", [[:%s/\<<C-r><C-w>\>/<C-r><C-w>/gI<Left><Left><Left>]])
-- Better split switching
vim.keymap.set('', '<C-j>', '<C-W>j')
vim.keymap.set('', '<C-k>', '<C-W>k')
vim.keymap.set('', '<C-h>', '<C-W>h')
vim.keymap.set('', '<C-l>', '<C-W>l')
-- Visual linewise up and down by default (and use gj gk to go quicker)
vim.keymap.set('n', '<Up>', 'gk')
vim.keymap.set('n', '<Down>', 'gj')
-- Yanking a line should act like D and C
vim.keymap.set('n', 'Y', 'y$')
-- we don't use netrw (because of nvim-tree), hence re-implement gx to open
-- links in browser
vim.keymap.set("n", "gx", '<Cmd>call jobstart(["open", expand("<cfile>")], {"detach": v:true})<CR>')
-- Open help window in a vertical split to the right.
vim.api.nvim_create_autocmd("BufWinEnter", {
group = vim.api.nvim_create_augroup("help_window_right", {}),
pattern = { "*.txt" },
callback = function()
if vim.o.filetype == 'help' then vim.cmd.wincmd("L") end
end
})
-- git.nvim
vim.keymap.set('n', '<leader>gb', '<CMD>lua require("git.blame").blame()<CR>')
vim.keymap.set('n', '<leader>go', "<CMD>lua require('git.browse').open(false)<CR>")
vim.keymap.set('x', '<leader>go', ":<C-u> lua require('git.browse').open(true)<CR>")
-- old habits
vim.api.nvim_create_user_command("GBrowse", 'lua require("git.browse").open(true)<CR>', {
range = true,
bang = true,
nargs = "*",
})
-- other.nvim
vim.keymap.set("n", "<leader>ta", ":Other<CR>", { noremap = true, silent = true })
vim.keymap.set("n", "<leader>tv", ":OtherVSplit<CR>", { noremap = true, silent = true })
vim.keymap.set("n", "<leader>ts", ":OtherSplit<CR>", { noremap = true, silent = true })
vim.keymap.set("n", "<leader>tc", ":OtherClear<CR>", { noremap = true, silent = true })
-- File-tree mappings
vim.keymap.set('n', '<leader>n', ':NvimTreeToggle<CR>', { noremap = true })
vim.keymap.set('n', '<leader>f', ':NvimTreeFindFileToggle!<CR>', { noremap = true })
-- vim-test
vim.keymap.set('n', '<leader>tt', ':TestNearest -v<CR>', { noremap = true, silent = true })
vim.keymap.set('n', '<leader>tf', ':TestFile -v<CR>', { noremap = true, silent = true })
-- telescope
local builtin = require('telescope.builtin')
vim.keymap.set('n', '<C-p>', builtin.git_files, {})
vim.keymap.set('n', '<C-b>', builtin.find_files, {})
vim.keymap.set('n', '<C-g>', builtin.lsp_document_symbols, {})
vim.keymap.set('n', '<leader>td', builtin.diagnostics, {})
vim.keymap.set('n', '<leader>gs', builtin.grep_string, {})
vim.keymap.set('n', '<leader>gg', builtin.live_grep, {})
-- diagnostics
vim.keymap.set('n', '<leader>do', vim.diagnostic.open_float)
vim.keymap.set('n', '<leader>dp', vim.diagnostic.goto_prev)
vim.keymap.set('n', '<leader>dn', vim.diagnostic.goto_next)
vim.keymap.set('n', '<leader>ds', vim.diagnostic.setqflist)
-- vim-go
vim.keymap.set('n', '<leader>b', build_go_files)
-- disable diagnostics, I didn't like them
vim.lsp.handlers["textDocument/publishDiagnostics"] = function() end
-- Go uses gofmt, which uses tabs for indentation and spaces for aligment.
-- Hence override our indentation rules.
vim.api.nvim_create_autocmd('Filetype', {
group = vim.api.nvim_create_augroup('setIndent', { clear = true }),
pattern = { 'go' },
command = 'setlocal noexpandtab tabstop=4 shiftwidth=4'
})
-- Run gofmt/gofmpt, import packages automatically on save
vim.api.nvim_create_autocmd('BufWritePre', {
group = vim.api.nvim_create_augroup('setGoFormatting', { clear = true }),
pattern = '*.go',
callback = function()
local params = vim.lsp.util.make_range_params()
params.context = { only = { "source.organizeImports" } }
local result = vim.lsp.buf_request_sync(0, "textDocument/codeAction", params, 2000)
for _, res in pairs(result or {}) do
for _, r in pairs(res.result or {}) do
if r.edit then
vim.lsp.util.apply_workspace_edit(r.edit, "utf-16")
else
vim.lsp.buf.execute_command(r.command)
end
end
end
vim.lsp.buf.format()
end
})
-- Use LspAttach autocommand to only map the following keys
-- after the language server attaches to the current buffer
vim.api.nvim_create_autocmd('LspAttach', {
group = vim.api.nvim_create_augroup('UserLspConfig', {}),
callback = function(ev)
-- Buffer local mappings.
-- See `:help vim.lsp.*` for documentation on any of the below functions
local opts = { buffer = ev.buf }
vim.keymap.set('n', 'gd', "<cmd>lua vim.lsp.buf.definition()<CR>", opts)
vim.keymap.set('n', '<leader>v', "<cmd>vsplit | lua vim.lsp.buf.definition()<CR>", opts)
vim.keymap.set('n', '<leader>s', "<cmd>belowright split | lua vim.lsp.buf.definition()<CR>", opts)
vim.keymap.set('n', 'gr', vim.lsp.buf.references, opts)
vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, opts)
vim.keymap.set('n', 'K', vim.lsp.buf.hover, opts)
vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, opts)
vim.keymap.set('n', '<leader>cl', vim.lsp.codelens.run, opts)
vim.keymap.set('n', '<leader>rn', vim.lsp.buf.rename, opts)
vim.keymap.set({'n', 'v'}, '<leader>ca', vim.lsp.buf.code_action, opts)
end,
})
-- automatically resize all vim buffers if I resize the terminal window
vim.api.nvim_command('autocmd VimResized * wincmd =')
-- https://github.com/neovim/neovim/issues/21771
local exitgroup = vim.api.nvim_create_augroup('setDir', { clear = true })
vim.api.nvim_create_autocmd('DirChanged', {
group = exitgroup,
pattern = { '*' },
command = [[call chansend(v:stderr, printf("\033]7;file://%s\033\\", v:event.cwd))]],
})
vim.api.nvim_create_autocmd('VimLeave', {
group = exitgroup,
pattern = { '*' },
command = [[call chansend(v:stderr, "\033]7;\033\\")]],
})
-- put quickfix window always to the bottom
local qfgroup = vim.api.nvim_create_augroup('changeQuickfix', { clear = true })
vim.api.nvim_create_autocmd('FileType', {
pattern = 'qf',
group = qfgroup,
command = 'wincmd J',
})
vim.api.nvim_create_autocmd('FileType', {
pattern = 'qf',
group = qfgroup,
command = 'setlocal wrap',
})