aboutsummaryrefslogtreecommitdiff
path: root/store/config/nvim/lua/setup/lsp.lua
blob: 472cdaf3a34fbef6337f7ed49b92c2e707e6b740 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
-- spellchecker: words denols luals

vim.lsp.config("*", {
    capabilities = vim.tbl_extend("force",
        vim.lsp.protocol.make_client_capabilities(),
        require("cmp_nvim_lsp").default_capabilities()
    )
})

local function setup_clangd()
    local clangd = "clangd"
    local brew_clangd_path = "/usr/local/opt/llvm/bin/clangd"

    if vim.uv.fs_stat(brew_clangd_path) ~= nil then
        clangd = brew_clangd_path
    end

    vim.lsp.config("clangd", { cmd = { clangd } })

    vim.api.nvim_create_autocmd('LspAttach', {
        callback = function(ev)
            local client = vim.lsp.get_client_by_id(ev.data.client_id)
            if client and client.name == "clangd" then
                vim.keymap.set('n', 'grs', "<cmd>ClangdSwitchSourceHeader<cr>", {
                    buffer = ev.buf
                })
            end
        end
    })
end

local function setup_lua_ls()
    vim.lsp.config("lua_ls", {
        settings = {
            Lua = {
                runtime = {
                    version = "LuaJIT"
                },
                diagnostics = {
                    globals = { "vim" },
                },
                workspace = {
                    library = {
                        [vim.fn.expand "$VIMRUNTIME/lua"] = true,
                        [vim.fn.expand "$VIMRUNTIME/lua/vim/lsp"] = true,
                        [vim.fn.stdpath "data" .. "/lazy/lazy.nvim/lua/lazy"] = true,
                    },
                },
            },
        },
    })
end

local function setup_denols()
    vim.lsp.config("denols", {
        root_dir = function(bufnr, on_dir)
            local deno_configs = vim.fs.find({ "deno.json", "deno.jsonc" }, {
                path = vim.api.nvim_buf_get_name(bufnr), upward = true, limit = math.huge })
            if 0 ~= #deno_configs then
                local deno_config = deno_configs[#deno_configs]
                on_dir(vim.fs.dirname(deno_config))
            end
        end,
    })
end


local function setup()
    setup_clangd()
    setup_lua_ls()
    setup_denols()

    function _G.crupest_no_range_format()
        vim.notify("Range format is no supported by the lsp.", vim.log.levels.ERROR, {})
    end

    vim.api.nvim_create_autocmd('LspAttach', {
        callback = function(ev)
            local client = vim.lsp.get_client_by_id(ev.data.client_id)
            vim.keymap.set('n', 'gqa', vim.lsp.buf.format, { buffer = ev.buf })
            if client and not client:supports_method('textDocument/rangeFormatting') then
                vim.bo[ev.buf].formatexpr = "v:lua.crupest_no_range_format()"
            end
        end
    })

    vim.lsp.enable({ "clangd", "lua_ls", "denols" })
end

return {
    setup = setup
}