nvim/lua/plugins/lsp/init.lua

153 lines
5.0 KiB
Lua
Raw Normal View History

2022-12-31 17:43:31 +01:00
local M = {
'neovim/nvim-lspconfig',
name = 'lsp',
2023-01-01 19:36:00 +01:00
event = 'BufRead',
2022-12-31 17:43:31 +01:00
dependencies = { 'hrsh7th/cmp-nvim-lsp' },
}
M.tools = {
'clangd',
'gopls',
'sumneko_lua',
}
function M.config()
2023-01-01 19:43:47 +01:00
require 'mason'
2022-12-31 17:43:31 +01:00
local lspconfig = require 'lspconfig'
local opts = {}
local cmp_nvim_lsp = require 'cmp_nvim_lsp'
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities.textDocument.completion.completionItem.snippetSupport = true
capabilities = cmp_nvim_lsp.default_capabilities(capabilities)
local function lsp_keymaps(bufnr)
local opt = { noremap = true, silent = true }
local keymap = vim.api.nvim_buf_set_keymap
keymap(bufnr, 'n', 'gD', '<cmd>lua vim.lsp.buf.declaration()<CR>', opt)
keymap(bufnr, 'n', 'gd', '<cmd>lua vim.lsp.buf.definition()<CR>', opt)
keymap(bufnr, 'n', 'K', '<cmd>lua vim.lsp.buf.hover()<CR>', opt)
keymap(bufnr, 'n', 'gI', '<cmd>lua vim.lsp.buf.implementation()<CR>', opt)
keymap(bufnr, 'n', 'gr', '<cmd>TroubleToggle lsp_references<CR>', opt)
keymap(bufnr, 'n', 'gl', '<cmd>lua vim.diagnostic.open_float()<CR>', opt)
keymap(bufnr, 'n', '<leader>lf', '<cmd>lua vim.lsp.buf.format { async = true }<cr>', opt)
keymap(bufnr, 'n', '<leader>li', '<cmd>LspInfo<cr>', opt)
keymap(bufnr, 'n', '<leader>lI', '<cmd>LspInstallInfo<cr>', opt)
keymap(bufnr, 'n', '<leader>la', '<cmd>lua vim.lsp.buf.code_action()<cr>', opt)
keymap(bufnr, 'n', '<leader>lj', '<cmd>lua vim.diagnostic.goto_next({buffer=0})<cr>', opt)
keymap(bufnr, 'n', '<leader>lk', '<cmd>lua vim.diagnostic.goto_prev({buffer=0})<cr>', opt)
keymap(bufnr, 'n', '<leader>lr', '<cmd>lua vim.lsp.buf.rename()<cr>', opt)
keymap(bufnr, 'n', '<leader>ls', '<cmd>lua vim.lsp.buf.signature_help()<CR>', opt)
keymap(bufnr, 'n', '<leader>lq', '<cmd>lua vim.diagnostic.setloclist()<CR>', opt)
keymap(bufnr, 'n', '<leader>e', '<cmd>lua vim.diagnostic.open_float()<CR>', opt)
end
local on_attach = function(client, bufnr)
--[[ vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc') ]]
lsp_keymaps(bufnr)
if client.name == 'sumneko_lua' then
client.server_capabilities.documentFormattingProvider = false
end
if client.name == 'clangd' then
client.server_capabilities.documentFormattingProvider = false
end
if client.name == 'gopls' then
client.server_capabilities.documentFormattingProvider = false
end
if client.name == 'jdtls' then
client.server_capabilities.documentFormattingProvider = false
end
if client.name == 'jdt.ls' then
client.server_capabilities.documentFormattingProvider = false
vim.lsp.codelens.refresh()
if JAVA_DAP_ACTIVE then
require('jdtls').setup_dap { hotcodereplace = 'auto' }
require('jdtls.dap').setup_dap_main_class_configs()
end
end
local ft = vim.api.nvim_buf_get_option(bufnr, 'filetype')
2023-01-01 17:04:23 +01:00
local nls = require 'plugins.null-ls'
2022-12-31 17:43:31 +01:00
local enable = false
if nls.has_formatter(ft) then
enable = client.name == 'null-ls'
else
enable = not (client.name == 'null-ls')
end
if client.name == 'tsserver' then
enable = false
end
-- util.info(client.name .. " " .. (enable and "yes" or "no"), "format")
client.server_capabilities.documentFormattingProvider = enable
end
for _, tool in ipairs(M.tools) do
opts = {
on_attach = on_attach,
capabilities = capabilities,
}
local server = vim.split(tool, '@')[1]
if server == 'sumneko_lua' then
2023-01-01 17:04:23 +01:00
local sumneko_opts = require 'plugins.lsp.settings.sumneko_lua'
2022-12-31 17:43:31 +01:00
opts = vim.tbl_deep_extend('force', sumneko_opts, opts)
end
if server == 'clangd' then
local clangd_flags = {
'--all-scopes-completion',
'--suggest-missing-includes',
'--background-index',
'--pch-storage=disk',
'--cross-file-rename',
'--log=info',
'--completion-style=detailed',
'--enable-config', -- clangd 11+ supports reading from .clangd configuration file
'--clang-tidy',
-- "--clang-tidy-checks=-*,llvm-*,clang-analyzer-*,modernize-*,-modernize-use-trailing-return-type",
-- "--fallback-style=Google",
-- "--header-insertion=never",
-- "--query-driver=<list-of-white-listed-complers>"
}
local clang_opts = {
arg = { unpack(clangd_flags) },
}
opts = vim.tbl_deep_extend('force', clang_opts, opts)
end
lspconfig[server].setup(opts)
end
local config = {
virtual_text = {
prefix = '',
},
signs = false,
underline = true,
update_in_insert = true,
severity_sort = true,
float = {
focusable = true,
style = 'minimal',
border = 'rounded',
source = 'always',
header = '',
prefix = '',
},
}
vim.diagnostic.config(config)
vim.lsp.handlers['textDocument/hover'] = vim.lsp.with(vim.lsp.handlers.hover, {
border = 'rounded',
})
vim.lsp.handlers['textDocument/signatureHelp'] = vim.lsp.with(vim.lsp.handlers.signature_help, {
border = 'rounded',
})
2023-01-01 19:36:00 +01:00
local on_references = vim.lsp.handlers['textDocument/references']
vim.lsp.handlers['textDocument/references'] = vim.lsp.with(on_references, {
-- Use location list instead of quickfix list
loclist = true,
})
2022-12-31 17:43:31 +01:00
end
return M