local M = { 'neovim/nvim-lspconfig', name = 'lsp', event = 'BufRead', dependencies = { 'hrsh7th/cmp-nvim-lsp' }, } M.tools = { 'clangd', 'gopls', 'sumneko_lua', } function M.config() require 'mason' 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', 'lua vim.lsp.buf.declaration()', opt) keymap(bufnr, 'n', 'gd', 'lua vim.lsp.buf.definition()', opt) keymap(bufnr, 'n', 'K', 'lua vim.lsp.buf.hover()', opt) keymap(bufnr, 'n', 'gI', 'lua vim.lsp.buf.implementation()', opt) keymap(bufnr, 'n', 'gr', 'TroubleToggle lsp_references', opt) keymap(bufnr, 'n', 'gl', 'lua vim.diagnostic.open_float()', opt) keymap(bufnr, 'n', 'lf', 'lua vim.lsp.buf.format { async = true }', opt) keymap(bufnr, 'n', 'li', 'LspInfo', opt) keymap(bufnr, 'n', 'lI', 'LspInstallInfo', opt) keymap(bufnr, 'n', 'la', 'lua vim.lsp.buf.code_action()', opt) keymap(bufnr, 'n', 'lj', 'lua vim.diagnostic.goto_next({buffer=0})', opt) keymap(bufnr, 'n', 'lk', 'lua vim.diagnostic.goto_prev({buffer=0})', opt) keymap(bufnr, 'n', 'lr', 'lua vim.lsp.buf.rename()', opt) keymap(bufnr, 'n', 'ls', 'lua vim.lsp.buf.signature_help()', opt) keymap(bufnr, 'n', 'lq', 'lua vim.diagnostic.setloclist()', opt) keymap(bufnr, 'n', 'e', 'lua vim.diagnostic.open_float()', 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 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 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=" } 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', }) 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