if !exists('g:lspconfig')
  finish
endif
"
" Completion configuration for native neovim + nvim-lspconfig
lua << EOF
local nvim_lsp = require'lspconfig'
--local coq = require'coq'
local cmp = require'cmp_nvim_lsp'
local clangd_extensions = require("clangd_extensions")

-- Mappings.
local opts = { noremap=true, silent=true }
vim.api.nvim_set_keymap('n', '<leader>e', '<cmd>lua vim.diagnostic.open_float()<CR>', opts)
vim.api.nvim_set_keymap('n', '[d', '<cmd>lua vim.diagnostic.goto_prev()<CR>', opts)
vim.api.nvim_set_keymap('n', ']d', '<cmd>lua vim.diagnostic.goto_next()<CR>', opts)
vim.api.nvim_set_keymap('n', '<leader>E', '<cmd>lua vim.diagnostic.set_loclist()<CR>', opts)
vim.api.nvim_set_keymap("n", "<leader>q", "<cmd>lua vim.lsp.buf.format{ async = true }<CR>", opts)

-- Use an on_attach function to only map the following keys
-- after the language server attaches to the current buffer
local on_attach = function(client, bufnr)
  --Enable completion triggered by <c-x><c-o>
  vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc')

  ---- See `:help vim.lsp.*` for documentation on any of the below functions
  vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gD', '<cmd>lua vim.lsp.buf.declaration()<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gd', '<cmd>lua vim.lsp.buf.definition()<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', 'K', '<cmd>lua vim.lsp.buf.hover()<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', '<C-k>', '<cmd>lua vim.lsp.buf.signature_help()<CR>', opts)
  --vim.api.nvim_buf_set_keymap(bufnr, 'n', '<space>wa', '<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>', opts)
  --vim.api.nvim_buf_set_keymap(bufnr, 'n', '<space>wr', '<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>', opts)
  --vim.api.nvim_buf_set_keymap(bufnr, 'n', '<space>wl', '<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>D', '<cmd>lua vim.lsp.buf.type_definition()<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>rn', '<cmd>lua vim.lsp.buf.rename()<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>ca', '<cmd>lua vim.lsp.buf.code_action()<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gr', '<cmd>lua vim.lsp.buf.references()<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', '<F6>', '<cmd>ClangdSwitchSourceHeader<cr>', opts)

  require'illuminate'.on_attach(client)
  require'virtualtypes'.on_attach()
end

local signs = {
    { name = "DiagnosticSignError", text = "" },
    { name = "DiagnosticSignWarn", text = "" },
    { name = "DiagnosticSignHint", text = "" },
    { name = "DiagnosticSignInfo", text = "" },
  }

  for _, sign in ipairs(signs) do
    vim.fn.sign_define(sign.name, { texthl = sign.name, text = sign.text, numhl = "" })
  end

vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with(
  vim.lsp.diagnostic.on_publish_diagnostics, {
    signs = {
      active = signs,
      --severity_limit = 'info',
    },
    severity_sort = true,
    underline = false,
    update_in_insert = false,
    --virtual_text = {
    --  spacing = 40,
    --  severity_limit = 'Warning',
    --},
    virtual_text = false,
  }
)

nvim_lsp.groovyls.setup ({
  cmd = { "java", "-jar", "/home/kmcr/tools/groovy-language-server/build/libs/groovy-language-server-all.jar" },
  on_attach = on_attach,
  on_publish_diagnostics = diagnostic_handler,
  capabilities = cmp.update_capabilities(vim.lsp.protocol.make_client_capabilities()),
  }
)

-- nvim_lsp.ccls.setup(
--   coq.lsp_ensure_capabilities({
--     init_options = {
--       compilationDatabaseDirectory = ".";
--       index = {
--         threads = 0;
--       },
--       clang = {
--         excludeArgs = { "-frounding-math"} ;
--       },
--     },
--     root_dir = nvim_lsp.util.root_pattern("compile_commands.json"),
--     on_attach = on_attach,
--     on_publish_diagnostics = diagnostic_handler,
--     capabilities = cmp.update_capabilities(vim.lsp.protocol.make_client_capabilities()),
--     flags = {
--       debounce_text_changes = 150,
--     }}
--   )
-- )

clangd_extensions.setup({
  server = {
    on_attach = on_attach,
    root_pattern = {
      '.clangd',
      '.clang-tidy',
      '.clang-format',
      'compile_commands.json',
      'compile_flags.txt',
      'configure.ac',
    }
  },
  inlay_hints = {
    max_len_align = true,
    max_len_align_padding = 3,
    right_align = true,
  }
})
-- nvim_lsp.clangd.setup(
--   coq.lsp_ensure_capabilities({
--     root_dir = nvim_lsp.util.root_pattern("compile_commands.json"),
--     on_attach = on_attach,
--     capabilities = cmp.update_capabilities(vim.lsp.protocol.make_client_capabilities()),
--     flags = {
--       debounce_text_changes = 150,
--     }}
--   )
-- )

nvim_lsp.pylsp.setup({
  settings = {
    pylint = {
      enabled = false
    },
    pyflakes = {
      enabled = false,
    },
  },
  flags = {
    debounce_text_changes = 150,
  },
  on_attach = on_attach,
  on_publish_diagnostics = diagnostic_handler,
  capabilities = cmp.update_capabilities(vim.lsp.protocol.make_client_capabilities()),
})

-- Use a loop to conveniently call 'setup' on multiple servers and
-- map buffer local keybindings when the language server attaches
local servers = { "cmake", "rust_analyzer", "bashls" }
for _, lsp in ipairs(servers) do
  nvim_lsp[lsp].setup({
    on_attach = on_attach,
    on_publish_diagnostics = diagnostic_handler,
    capabilities = cmp.update_capabilities(vim.lsp.protocol.make_client_capabilities()),
    flags = {
      debounce_text_changes = 150,
    }
  })
end
EOF
