dotfiles/nvim/.config/nvim/lua/plug/_lsp.lua

235 lines
8.6 KiB
Lua

local lsp = require("lsp-zero")
vim.diagnostic.config { virtual_text = true }
vim.fn.sign_define("DiagnosticSignError", { text = "", texthl = "DiagnosticSignError" })
vim.fn.sign_define("DiagnosticSignWarn", { text = "", texthl = "DiagnosticSignWarn" })
vim.fn.sign_define("DiagnosticSignInfo", { text = "", texthl = "DiagnosticSignInfo" })
vim.fn.sign_define("DiagnosticSignHint", { text = "", texthl = "DiagnosticSignHint" })
lsp.ensure_installed({
'arduino_language_server',
'bashls',
'beancount',
'clangd',
'dockerls',
'docker_compose_language_service',
'lua_ls',
'pyright',
'ruff_lsp',
'taplo',
'yamlls',
})
lsp.preset({ name = "recommended", set_lsp_keymaps = false })
lsp.on_attach(function(client, bufnr)
require("lsp-format").on_attach(client, bufnr)
local map = vim.keymap.set
map('n', '[d', '<cmd>lua vim.diagnostic.goto_prev()<cr>',
{ buffer = bufnr, desc = 'Previous diagnostic' })
map('n', ']d', '<cmd>lua vim.diagnostic.goto_next()<cr>',
{ buffer = bufnr, desc = 'Next diagnostic' })
map('n', '[e',
'<cmd>lua vim.diagnostic.goto_prev({severity = vim.diagnostic.severity.ERROR})<cr>',
{ buffer = bufnr, desc = 'Previous error' })
map('n', ']e',
'<cmd>lua vim.diagnostic.goto_next({severity = vim.diagnostic.severity.ERROR})<cr>',
{ buffer = bufnr, desc = 'Next error' })
local prefix = require('which-key').register
prefix({ ['<localleader>l'] = { name = "+lsp" } })
map('n', '<localleader>li', '<cmd>LspInfo<cr>',
{ buffer = bufnr, desc = 'Lsp Info' })
map('n', '<localleader>ld', '<cmd>lua vim.diagnostic.open_float()<cr>',
{ buffer = bufnr, desc = 'Line diagnostics' })
map('n', '<localleader>la', '<cmd>lua vim.lsp.buf.code_action()<cr>',
{ buffer = bufnr, desc = 'Codeactions' })
map('n', '<localleader>ln', '<cmd>lua vim.lsp.buf.rename()<cr>',
{ buffer = bufnr, desc = 'Rename element' })
if vim.fn.exists(':Telescope') then
map('n', '<localleader>lr', '<cmd>Telescope lsp_references()<cr>',
{ buffer = bufnr, desc = 'References' })
map('n', '<localleader>lf', '<cmd>Telescope lsp_definitions<cr>',
{ buffer = bufnr, desc = 'Definition' })
map('n', '<localleader>lt', '<cmd>Telescope lsp_type_definitions<cr>',
{ buffer = bufnr, desc = 'Type definition' })
map('n', '<localleader>lm', '<cmd>Telescope lsp_implementations<cr>',
{ buffer = bufnr, desc = 'Implementation' })
else
map('n', '<localleader>lr', '<cmd>lua vim.lsp.buf.references()<cr>',
{ buffer = bufnr, desc = 'References' })
map('n', '<localleader>lf', '<cmd>lua vim.lsp.buf.definition()<cr>',
{ buffer = bufnr, desc = 'Definition' })
map('n', '<localleader>lt', '<cmd>lua vim.lsp.buf.type_definition()<cr>',
{ buffer = bufnr, desc = 'Type definition' })
map('n', '<localleader>lm', '<cmd>lua vim.lsp.buf.implementation()<cr>',
{ buffer = bufnr, desc = 'Implementation' })
end
if client.server_capabilities.document_formatting then
map('n', '<localleader>lf', "<cmd>lua vim.lsp.buf.formatting()<CR>",
{ buffer = bufnr, desc = 'Format document' })
end
map('n', 'K', '<cmd>lua vim.lsp.buf.hover()<cr>',
{ buffer = bufnr, desc = 'Hover definition' })
map('n', '<localleader>lc', '<cmd>lua vim.lsp.buf.declaration()<cr>',
{ buffer = bufnr, desc = 'Declaration' })
map('n', '<localleader>ls', '<cmd>lua vim.lsp.buf.signature_help()<cr>',
{ buffer = bufnr, desc = 'Signature help' })
end)
lsp.nvim_workspace()
-- ensure python virtualenv is determined automatically on lsp start
lsp.configure("pyright", {
on_attach = function(client, _)
local python_path, msg = require('util.pyenv').get_path(client.config
.root_dir)
vim.notify(string.format('%s\n%s', msg, python_path))
client.config.settings.python.pythonPath = python_path
end
})
-- set up arduino with the help of arduino.nvim plugin
require('lspconfig').arduino_language_server.setup({
on_new_config = require('arduino').on_new_config
})
require('lspconfig').lua_ls.setup(lsp.nvim_lua_ls())
lsp.setup()
local luasnip = require("luasnip")
local has_words_before = function()
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
local kind_icons = {
Text = "",
Method = "",
Function = "",
Constructor = "",
Field = "",
Variable = "",
Class = "",
Interface = "",
Module = "",
Property = "",
Unit = "",
Value = "",
Enum = "",
Keyword = "",
Snippet = "",
Color = "",
File = "",
Reference = "",
Folder = "",
EnumMember = "",
Constant = "",
Struct = "",
Event = "",
Operator = "",
TypeParameter = ""
}
local cmp = require 'cmp'
cmp.setup({
window = {
documentation = cmp.config.window.bordered()
},
snippet = {
expand = function(args)
require('luasnip').lsp_expand(args.body)
end
},
sources = {
{ name = 'nvim_lsp' },
{ name = 'otter' },
{ name = 'luasnip', keyword_length = 2 },
{ name = 'pandoc_references' },
{ name = 'nvim_lua' },
{
name = 'beancount',
option = {
account = vim.env["HOME"] .. '/documents/records/budget/main.beancount' -- TODO implement dynamically
}
},
{ name = 'calc' },
{ name = 'path' },
{ name = 'buffer', keyword_length = 3 },
{ name = 'digraphs' },
{ name = 'latex_symbols' },
{ name = 'spell', keyword_length = 3 },
{ name = 'tmux' },
--{ name = 'rg', keyword_length = 5 },
{ name = 'vCard' },
},
mapping = cmp.mapping.preset.insert({
['<C-b>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
["<CR>"] = cmp.mapping({
i = function(fallback)
if cmp.visible() and cmp.get_active_entry() then
cmp.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = false })
else
fallback()
end
end,
s = cmp.mapping.confirm({ select = true }),
c = cmp.mapping.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = false }), -- disable selection in cmd mode
}),
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
-- You could replace the expand_or_jumpable() calls with expand_or_locally_jumpable()
-- they way you will only jump inside the snippet region
elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
elseif has_words_before() then
cmp.complete()
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" }),
}),
formatting = {
fields = { "kind", "abbr", "menu" },
format = function(entry, vim_item)
-- Kind icons, removing kind text leaving only icon
-- vim_item.kind = string.format('%s %s', kind_icons[vim_item.kind], vim_item.kind)
vim_item.kind = string.format('%s', kind_icons[vim_item.kind])
-- Source
vim_item.menu = ({
buffer = "[Buf]",
calc = "[Cal]",
digraphs = "[Dig]",
latex_symbols = "[LaTeX]",
luasnip = "[Snip]",
nvim_lsp = "[Lsp]",
nvim_lua = "[Lua]",
pandoc_references = "[Bib]",
spell = "[Spl]",
vCard = "[vCrd]",
})[entry.source.name]
return vim_item
end
},
})
-- `/` cmdline setup.
cmp.setup.cmdline('/', {
mapping = cmp.mapping.preset.cmdline(),
sources = { { name = 'buffer' } }
})
-- `:` cmdline setup.
cmp.setup.cmdline(':', {
mapping = cmp.mapping.preset.cmdline(),
sources = cmp.config.sources({ { name = 'path' } }, {
{ name = 'cmdline', option = { ignore_cmds = { 'Man', '!' } } }
})
})