nvim/lua/plugins/lsp/init.lua

163 lines
5.6 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',
2023-01-17 15:05:01 +01:00
dependencies = { 'hrsh7th/cmp-nvim-lsp', 'williamboman/mason.nvim' },
2022-12-31 17:43:31 +01:00
}
2023-01-03 16:13:42 +01:00
local tools = {
2022-12-31 17:43:31 +01:00
'clangd',
'gopls',
'sumneko_lua',
2023-01-01 22:55:55 +01:00
'jdtls',
2022-12-31 17:43:31 +01:00
}
function M.config()
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
2023-01-03 16:13:42 +01:00
for _, tool in ipairs(tools) do
2022-12-31 17:43:31 +01:00
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
2023-01-01 22:55:55 +01:00
if server == 'jdtls' then
vim.cmd "command! -buffer -nargs=? -complete=custom,v:lua.require'jdtls'._complete_compile JdtCompile lua require('jdtls').compile(<f-args>)"
vim.cmd "command! -buffer -nargs=? -complete=custom,v:lua.require'jdtls'._complete_set_runtime JdtSetRuntime lua require('jdtls').set_runtime(<f-args>)"
vim.cmd "command! -buffer JdtUpdateConfig lua require('jdtls').update_project_config()"
-- vim.cmd "command! -buffer JdtJol lua require('jdtls').jol()"
vim.cmd "command! -buffer JdtBytecode lua require('jdtls').javap()"
-- vim.cmd "command! -buffer JdtJshell lua require('jdtls').jshell()"
2023-01-17 16:00:35 +01:00
goto continue
2023-01-01 22:55:55 +01:00
end
2022-12-31 17:43:31 +01:00
lspconfig[server].setup(opts)
2023-01-01 22:55:55 +01:00
::continue::
2022-12-31 17:43:31 +01:00
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