caio.co/de/dotfiles


Move home_cache_dir helper to custom mod by Caio a year ago (log)

Blob config/nvim/lua/config/lsp.lua

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
vim.api.nvim_create_autocmd('LspAttach', {
    group = vim.api.nvim_create_augroup('my.lsp', {}),
    callback = function(args)
        -- Run cursor+timer triggers faster
        vim.o.updatetime = 300
        -- So that the window doesn't shift around in the presence of diagnostics
        vim.wo.signcolumn = "yes"

        -- ^]: definition (^T: jump back)
        -- gri: implementation
        -- grr: references
        -- grn: rename
        -- gra: code_action
        local function nmap(seq, action)
            local opts = { silent = true, noremap = true}
            vim.api.nvim_buf_set_keymap(args.buf, 'n', seq, string.format("<cmd>%s<CR>", action), opts)
        end
        nmap('gy', 'lua vim.lsp.buf.type_definition()')
        nmap('<leader>s', 'Telescope lsp_document_symbols')
        nmap('<leader>S', 'Telescope lsp_dynamic_workspace_symbols')

        local client = assert(vim.lsp.get_client_by_id(args.data.client_id))

        if client:supports_method('textDocument/completion') then
            vim.lsp.completion.enable(true, client.id, args.buf, {
                autotrigger = true
            })
        end

        if client:supports_method('textDocument/foldingRange') then
            vim.o.foldlevel = 99
            vim.o.foldmethod = "expr"
            vim.o.foldexpr = "v:lua.vim.lsp.foldexpr()"
        end

        local group = vim.api.nvim_create_augroup('my.lsp', {clear=false})

        if not client:supports_method('textDocument/willSaveWaitUntil')
            and client:supports_method('textDocument/formatting') then
            vim.api.nvim_create_autocmd('BufWritePre', {
                group = group,
                buffer = args.buf,
                callback = function()
                    vim.lsp.buf.format({ bufnr = args.buf, id = client.id, timeout_ms = 1000 })
                end,
            })
        end

        if client:supports_method('textDocument/documentHighlight') then
            vim.api.nvim_create_autocmd('CursorHold', {
                group = group,
                buffer = args.buf,
                callback = function()
                    vim.lsp.buf.document_highlight()
                end,
            })
            vim.api.nvim_create_autocmd('CursorMoved', {
                group = group,
                buffer = args.buf,
                callback = function()
                    vim.lsp.buf.clear_references()
                end,
            })
        end

    end,
})

vim.lsp.config('*', {
    root_markers = { ".git" },
    capabilities = {
        textDocument = {
            completion = {
                completionItem = {
                    snippetSupport = false
                }
            }
        }
    }
})

vim.lsp.config("rust_analyzer", {
    cmd = {"rustup", "run", "stable", "rust-analyzer"},
    filetypes = { "rust" },
    settings = {
        ["rust-analyzer"] = {
            checkOnSave = {
                command = "clippy",
            },
            cargo = {
                allFeatures = true,
            },
        },
    }
})

vim.lsp.config("gopls", {
    cmd = {
        "gopls",
        "-remote=auto",
    },
    root_markers = { "go.mod" },
    filetypes = { "go" },
    settings = {
        gopls = {
            analyses = {
                unusedparams = true,
                nilness = true,
                shadow = true,
                unusedwrite = true,
            },
            staticcheck = true,
        },
    }
})

vim.lsp.config("ccls", {
    cmd = { "ccls" },
    filetypes = { "c", "cpp", "objc", "objcpp", "cuda" },
    root_markers = { "compile_commands.json" },
    init_options = {
        cache = {
            directory = require("custom").home_cache_dir('ccls'),
        },
    },
})

vim.api.nvim_create_user_command("LspStart", function()
    vim.lsp.enable({"rust_analyzer", "gopls", "ccls"})
    vim.cmd("edit") -- reload buffer
end, {})