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
|
--------------------------------------------------------------------------------
-- LSP configuration. nvim-cmp related stuff taken from:
-- https://github.com/neovim/nvim-lspconfig/wiki/Autocompletion/217feffc675a17d8ab95259ed9d4c6d62e1cd2e1
--------------------------------------------------------------------------------
local lspconfig = require("lspconfig")
local capabilities = require("cmp_nvim_lsp").default_capabilities()
-- https://github.com/hrsh7th/nvim-cmp/issues/373#issuecomment-1434284568
capabilities.textDocument.completion.completionItem.snippetSupport = false
lspconfig.gopls.setup {
capabilities = capabilities,
}
lspconfig.rust_analyzer.setup {
capabilities = capabilities,
settings = {
['rust-analyzer'] = {
diagnostics = {
disabled = { "unlinked-file" },
},
},
},
}
local cmp = require("cmp")
cmp.setup {
mapping = cmp.mapping.preset.insert({
['<C-u>'] = cmp.mapping.scroll_docs(-4), -- Up
['<C-d>'] = cmp.mapping.scroll_docs(4), -- Down
-- C-b (back) C-f (forward) for snippet placeholder navigation.
['<C-Space>'] = cmp.mapping.complete(),
['<CR>'] = cmp.mapping.confirm {
behavior = cmp.ConfirmBehavior.Replace,
select = true,
},
['<Tab>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
else
fallback()
end
end, { 'i', 's' }),
['<S-Tab>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
else
fallback()
end
end, { 'i', 's' }),
}),
sources = {
{ name = 'nvim_lsp' },
},
}
-- Format files using LSP on save
vim.api.nvim_create_autocmd('BufWritePre', {
callback = function(args)
vim.lsp.buf.format()
end,
})
-- Open LSP Diagnostics in quickfix list on save
vim.api.nvim_create_autocmd('BufWritePost', {
callback = function(args)
vim.diagnostic.setqflist({
open = false,
severity = vim.diagnostic.severity.ERROR,
})
end,
})
--------------------------------------------------------------------------------
-- Run 'organizeImports' on save for go files. Apparently organizing imports is
-- considered a separate task than formatting, so it's implemented separately
-- (in a much more cumbersome way, for some reason).
-- https://github.com/golang/tools/blob/0b989c812dcb662451db0b70a8267fe7e211886e/gopls/doc/vim.md#neovim-imports
-- https://github.com/golang/go/issues/33587
--------------------------------------------------------------------------------
vim.api.nvim_create_autocmd("BufWritePre", {
pattern = "*.go",
callback = function()
local params = vim.lsp.util.make_range_params()
params.context = {only = {"source.organizeImports"}}
-- buf_request_sync defaults to a 1000ms timeout. Depending on your
-- machine and codebase, you may want longer. Add an additional
-- argument after params if you find that you have to write the file
-- twice for changes to be saved.
-- E.g., vim.lsp.buf_request_sync(0, "textDocument/codeAction", params, 3000)
local result = vim.lsp.buf_request_sync(0, "textDocument/codeAction", params)
for cid, res in pairs(result or {}) do
for _, r in pairs(res.result or {}) do
if r.edit then
local enc = (vim.lsp.get_client_by_id(cid) or {}).offset_encoding or "utf-16"
vim.lsp.util.apply_workspace_edit(r.edit, enc)
end
end
end
vim.lsp.buf.format({async = false})
end
})
|