nvim/lua/plugins/lsp/init.lua

161 lines
5.7 KiB
Lua

local M = {
'neovim/nvim-lspconfig',
name = 'lsp',
event = 'BufRead',
dependencies = { 'hrsh7th/cmp-nvim-lsp', 'williamboman/mason.nvim', 'williamboman/mason-lspconfig.nvim' },
}
function M.config()
require('mason-lspconfig').setup {
automatic_installation = false,
}
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')
local nls = require 'plugins.null-ls'
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
require('mason-lspconfig').setup_handlers {
function(server) -- default handler (optional)
opts = {
on_attach = on_attach,
capabilities = capabilities,
}
if server == 'sumneko_lua' then
local sumneko_opts = require 'plugins.lsp.settings.sumneko_lua'
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
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()"
goto continue
end
require('lspconfig')[server].setup(opts)
::continue::
end,
}
local config = {
virtual_text = {
prefix = '',
},
signs = false,
underline = true,
update_in_insert = true,
severity_sort = false,
float = {
focusable = false,
-- 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',
})
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,
})
end
return M