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
|
local status_ok, lspconfig = pcall(require, "lspconfig")
if not status_ok then
return
end
-- configure lsp when attached
local function lsp_attach(client, bufnr)
-- helper functions
local function set_lsp_sign(name, text)
vim.fn.sign_define(name, { text = text, texthl = name })
end
local function map(m, lhs, rhs)
local opts = { remap = false, silent = true, buffer = bufnr }
vim.keymap.set(m, lhs, rhs, opts)
end
set_lsp_sign("DiagnosticSignError", "x")
set_lsp_sign("DiagnosticSignWarn" , "!")
set_lsp_sign("DiagnosticSignInfo" , "i")
set_lsp_sign("DiagnosticSignHint" , "h")
-- LSP actions
map('n', 'K', '<cmd>lua vim.lsp.buf.hover()<cr>')
map('n', 'gD', '<cmd>lua vim.lsp.buf.definition()<cr>')
-- map('n', 'gD', '<cmd>lua vim.lsp.buf.declaration()<cr>')
map('n', 'gI', '<cmd>lua vim.lsp.buf.implementation()<cr>')
map('n', 'gY', '<cmd>lua vim.lsp.buf.type_definition()<cr>')
map('n', 'gR', '<cmd>lua vim.lsp.buf.references()<cr>')
map('n', '<S-Tab>', '<cmd>lua vim.lsp.buf.signature_help()<cr>')
map('n', '<leader>lr', '<cmd>lua vim.lsp.buf.rename()<cr>')
map('n', '<F2>', '<cmd>lua vim.lsp.buf.rename()<cr>')
map('n', '<F4>', '<cmd>lua vim.lsp.buf.code_action()<cr>')
-- Diagnostics
map('n', '[d', '<cmd>lua vim.diagnostic.goto_prev()<cr>')
map('n', ']d', '<cmd>lua vim.diagnostic.goto_next()<cr>')
vim.api.nvim_buf_create_user_command(bufnr, 'LspFormat', function()
vim.lsp.buf.format()
end, {desc = 'Format buffer with language server'})
vim.api.nvim_buf_create_user_command('LspWorkspaceAdd', function()
vim.lsp.buf.add_workspace_folder()
end, { desc = 'Add folder to workspace' })
vim.api.nvim_buf_create_user_command('LspWorkspaceList', function()
vim.notify(vim.inspect(vim.lsp.buf.list_workspace_folders()))
end, { desc = 'List workspace folders' })
vim.api.nvim_buf_create_user_command('LspWorkspaceRemove', function()
vim.lsp.buf.remove_workspace_folder()
end, { desc = 'Remove folder from workspace' })
end
vim.diagnostic.config({
virtual_text = false,
signs = true,
update_in_insert = false,
underline = true,
severity_sort = true,
})
vim.lsp.handlers['textDocument/hover'] = vim.lsp.with(vim.lsp.handlers.hover,
{ border = 'shadow', })
vim.lsp.handlers['textDocument/signatureHelp'] = vim.lsp.with(
vim.lsp.handlers.signature_help, { border = 'shadow' })
-- get servers and attach to them
local status_ok1, mason = pcall(require, "mason")
if not status_ok1 then
return
end
mason.setup {}
local status_ok2, masonlspconfig = pcall(require, "mason-lspconfig")
if not status_ok2 then
return
end
masonlspconfig.setup {}
masonlspconfig.setup_handlers {
function (server_name)
lspconfig[server_name].setup { on_attach = lsp_attach }
end,
-- specific servers can be setup as follows:
-- ["rust_analyzer"] = function ()
-- require("rust-tools").setup {}
-- end
-- check out :help mason-lspconfig for more info
}
|