local servers = {
    ansiblels = {},
    bashls = {},
    clangd = {},
    cmake = {},
    csharp_ls = {},
    eslint = {},
    gopls = {},
    hls = {},
    jdtls = {},
    jsonls = {},
    kotlin_language_server = {},
    lua_ls = require('lsp.lua_ls'),
    pyright = {},
    ruby_lsp = {},
    rust_analyzer = {},
    svls = {},
    ts_ls = {},
    yamlls = require('lsp.yamlls'),
}

-- vim.lsp.set_log_level('info')

local on_attach = function(_, bufnr)
    local nmap = function(keys, func)
        vim.keymap.set('n', keys, func, { buffer = bufnr })
    end

    nmap('<leader>rn', vim.lsp.buf.rename)
    nmap('<leader>ca', vim.lsp.buf.code_action)

    nmap('gd', require('telescope.builtin').lsp_definitions)
    nmap('gr', require('telescope.builtin').lsp_references)
    nmap('gt', require('telescope.builtin').lsp_type_definitions)

    -- nmap('gd', vim.lsp.buf.definition)
    -- nmap('gr', vim.lsp.buf.references)
    -- nmap('gt', vim.lsp.buf.type_definition)

    nmap('<leader>sd', require('telescope.builtin').diagnostics)
    nmap('<leader>ds', require('telescope.builtin').lsp_document_symbols)
    nmap('<leader>as', require('telescope.builtin').lsp_dynamic_workspace_symbols)

    nmap("<leader>i", vim.diagnostic.open_float)
    nmap('K', vim.lsp.buf.hover)
    nmap('<C-k>', vim.lsp.buf.signature_help)

    nmap('gI', require('telescope.builtin').lsp_implementations)
    -- nmap('gI', vim.lsp.buf.implementation)
    nmap('gD', vim.lsp.buf.declaration)

    nmap('<leader>F', vim.lsp.buf.format)
end

local capabilities = require('cmp_nvim_lsp').default_capabilities()
capabilities.textDocument.foldingRange = {
    dynamicRegistration = false,
    lineFoldingOnly = true
}

for server_name, config in pairs(servers) do
    vim.lsp.config(server_name, vim.tbl_deep_extend('keep', {
        capabilities = capabilities,
        on_attach = on_attach,
    }, config))
    vim.lsp.enable(server_name)
end
