summaryrefslogtreecommitdiff
path: root/.config/nvim/lua/custom/config/lsp.lua
blob: 817b087e10d5cb230130ac8a1bb81694495662b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
local lspconfig = require('lspconfig')
local capabilities = require('cmp_nvim_lsp').default_capabilities()

local servers = require('custom.config.language-servers')
for name, config in pairs(servers) do
    if config == false then
        goto continue
    end
    if config == true then
        ---@diagnostic disable-next-line: cast-local-type
        config = {}
    end

    -- NOTE: there must be a better way to do it, but it will do for now
    if name == 'markdown_oxide' then
        ---@diagnostic disable-next-line: cast-local-type
        config = vim.tbl_deep_extend('force', {}, {
            capabilities = vim.tbl_deep_extend('force', capabilities, {
                workspace = {
                    didChangeWatchedFiles = {
                        dynamicRegistration = true,
                    },
                },
            }),
        }, config)
    else
        ---@diagnostic disable-next-line: cast-local-type
        config = vim.tbl_deep_extend('force', {}, {
            capabilities = capabilities,
        }, config)
    end

    lspconfig[name].setup(config)
    ::continue::
end

vim.api.nvim_create_autocmd('LspAttach', {
    callback = function(args)
        local bufnr = args.buf
        local client = assert(vim.lsp.get_client_by_id(args.data.client_id), 'must have valid client')

        local settings = servers[client.name]
        if type(settings) ~= 'table' then
            ---@diagnostic disable-next-line: cast-local-type
            settings = {}
        end

        -- Override server capabilities
        if settings.server_capabilities then
            for k, v in pairs(settings.server_capabilities) do
                if v == vim.NIL then
                    v = nil
                end
                client.server_capabilities[k] = v
            end
        end

        -- Register buffer local LSP keymaps
        require('custom.config.lsp-keymaps').setup(bufnr)
    end,
})